query problem..


#1

Hi there, i’m trying to make a query, that i could put product into a database, but it does nothing and i can’t understand where is the problem. What i’m doing wrong here?

[php] function nameTaken($pavadinimas){
if(!get_magic_quotes_gpc()){
$pavadinimas = addslashes($pavadinimas);
}
$q = “SELECT pavadinimas FROM “.TBL_PREKES.” WHERE pavadinimas = ‘$pavadinimas’”;
$result = mysql_query($q, $this->connection);
return (mysql_numrows($result) > 0);
}

  function addNewPrduct($pavadinimas, $kiekis, $vieneto_kaina){
  $time = time();
  $q = "INSERT INTO ".TBL_PREKES." VALUES ('','$pavadinimas','$kiekis','$vieneto_kaina')";
  return mysql_query($q, $this->connection);
  return (mysql_numrows($result) > 0);

}[/php]


#2

??? $q = “SELECT pavadinimas FROM “.TBL_PREKES.” WHERE pavadinimas = ‘$pavadinimas’”;

Do you have a field name inside the table pavadiniamas called pavadinmas???

Normally, you SELECT data-list FROM table-name WHERE field-name = value…
So, the “data-list” would be * for all data or just the field you want to pull from the table.
The table-name would be obviously the table.
The field-name would be where you are looking for data in the table in that col…

So, my guess is that you want to change the “SELECT pavadinimas” part to "SELECT * "…


#3

Yes there is there is one.

CREATE TABLE IF NOT EXISTS `prekes` ( `preke_id` int(3) NOT NULL AUTO_INCREMENT, `pavadinimas` varchar(30) NOT NULL, `kiekis` int(5) NOT NULL, `vieneto_kaina` int(5) NOT NULL, `data` datetime NOT NULL, PRIMARY KEY (`preke_id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;

With first function “nameTaken” i check that if the name is taken or not. If its not taken i put it into databe using second function “addNewProduct”.


#4

into database*


#5

You must EITHER place the to-be-inserted data in this format:
values (1,2,3,4,5) Needs to be all of the possible inputs…
OR this way:
(field1,2,3,4) values (1,2,3,4) Only the fields listed…
So, you need to either put ALL FIVE fields in the values or…

Try it this way:

[php]
$q = “INSERT INTO prekes (preke_id, pavadinimas, kiekis, vieneto_kaina) VALUES (’’, ‘$pavadinimas’,’$kiekis’,’$vieneto_kaina’)”;
[/php]
Note that the “data” field is not listed and will be filled with the default you have set up in the database.

Hope that helps…

Oh, ALSO, the query you do for reading is useless!
$q = “SELECT pavadinimas FROM “.TBL_PREKES.” WHERE pavadinimas = ‘$pavadinimas’”;
This query will PULL ONLY the pavadinimas where it equals $pavadinimas !!!
So, don’t do the query, just use $pavadinimas. Waste of a query.
UNLESS you change it to:
$q = “SELECT * FROM “.TBL_PREKES.” WHERE pavadinimas = ‘$pavadinimas’”;
This will PULL ALL of the data for the pavadinmas that equals $pavadinimas variable…

Hope that helps, too…


#6

Thanx!!! it helped, now everything is fine :slight_smile:


#7

Great! Glad we could help…