lanceox Posted May 13, 2011 Share Posted May 13, 2011 hi guys hope some1 can help. im creating a system that will allow stock to be added to a database and modifed. However on the add section, nothing adds it only inputs a new id row in the database and the other fields are blank. Can anyone have a look for me This sections is the form handler once the user has input a stockname and stock qty <?php session_start(); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>stock</title> </head> <body> <?php $stockname = &$_POST['stockname']; $stockqty = &$_POST['stockqty']; $connect = mysql_connect("localhost","root", "") or die ("Couldn't Connect!"); mysql_select_db("stock", $connect) or die("Couldn't find db"); // select database $query = mysql_query("SELECT * FROM stocks WHERE stockname=$stockname, stockqty=$stockqty"); $numrows = mysql_num_rows($query); if ($numrows!=0) { echo("Item already exists! Try Updating the items instead!"); } else { $queryreg = mysql_query("INSERT INTO `stock` . `stocks` (`id`, `stockname`, `stockqty`) VALUES (NULL,'$stockname','$stockqty')"); echo("stock added!"); } ?> <center><table> <tr> <td> Stock_id </td> <td> Stock_name </td> <td> Stock_Qty </td> </tr> </table></center> </body> </html> Thanks Lance Link to comment https://forums.phpfreaks.com/topic/236295-values-wont-add-to-db/ Share on other sites More sharing options...
phppaper Posted May 13, 2011 Share Posted May 13, 2011 $queryreg = mysql_query("INSERT INTO `stock` . `stocks` (`id`, `stockname`, `stockqty`) VALUES (NULL,'$stockname','$stockqty')"); how about try this: $queryreg = mysql_query("INSERT INTO stocks(stockname, stockqty) VALUES ('$stockname','$stockqty')"); Link to comment https://forums.phpfreaks.com/topic/236295-values-wont-add-to-db/#findComment-1214872 Share on other sites More sharing options...
lanceox Posted May 13, 2011 Author Share Posted May 13, 2011 thanks it worked better than the way i done it. Thanks Is there a way i can check if stock exists then print message otherwise add to db. This is the kind of thing i mean.... where am i going wrong ? $connect = mysql_connect("localhost","root", "") or die ("Couldn't Connect!"); mysql_select_db("stock", $connect) or die("Couldn't find db"); // select database $query = mysql_query("SELECT * FROM stocks WHERE stockname=$stockname, stockqty=$stockqty"); $numrows = mysql_num_rows($query); if ($numrows!=0) { echo("Item already exists! Try Updating the items instead!"); } else { $queryreg = mysql_query("INSERT INTO `stocks` (stockname, stockqty) VALUES ('$stockname','$stockqty')"); echo("stock added!"); } Link to comment https://forums.phpfreaks.com/topic/236295-values-wont-add-to-db/#findComment-1214875 Share on other sites More sharing options...
phppaper Posted May 13, 2011 Share Posted May 13, 2011 you have already check: $numrows = mysql_num_rows($query); What exactly do you want? Link to comment https://forums.phpfreaks.com/topic/236295-values-wont-add-to-db/#findComment-1214876 Share on other sites More sharing options...
lanceox Posted May 13, 2011 Author Share Posted May 13, 2011 ive checked to see if it exists but for example when i got to add exactly the same stock name it just adds a new one rather than echoing an error message saying item already exists, please updata existing values. Thanks Lance Link to comment https://forums.phpfreaks.com/topic/236295-values-wont-add-to-db/#findComment-1214878 Share on other sites More sharing options...
phppaper Posted May 13, 2011 Share Posted May 13, 2011 $query = mysql_query("SELECT * FROM stocks WHERE stockname='$stockname'"); $numrows = mysql_num_rows($query); if ($numrows>0) { mysql_query("UPDATE stocks SET stockqty='$stockqty' WHERE stockname = '$stockname'"); echo("Item already exists! Its updated"); } else .................... Link to comment https://forums.phpfreaks.com/topic/236295-values-wont-add-to-db/#findComment-1214881 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.