tags:

views:

42

answers:

1

I am pretty new to PHP and I am trying to make an inventory database. I have been trying to make it so that a user can enter a card ID and then amount the want to add to the inventory and have it update the inventory. For example someone could type in test and 2342 and it would update test. Here is what I have been trying with no success:

add.html

<body>
  <form action="add.php" method="post">
    Card ID: <input type="text" name="CardID" />
    Amount to Add: <input type="text" name="Add" />
    <input type="submit" />
  </form>
</body>
</html>

add.php

<?php
$link = mysql_connect('host', 'username', 'password');
 if (!$link){
    die('Could not connect: ' . mysql_error());
   }
 mysql_select_db("tdm_inventory", $link);
 $add = $_POST[Add]
 mysql_query("UPDATE cardLists SET AmountLeft = '$add' WHERE cardID = 'Test'");
 echo "test successful";
 mysql_close($link);
?>
+1  A: 

I think you are missing quotes around your POST value for one. You are also committing one of the cardinal sins of PHP development putting the variables right in your SQL string like that. Try this instead:

<?php
 $link = mysql_connect('host', 'username', 'password');
 if (!$link)
 {
   die('Could not connect: ' . mysql_error());
 }
 mysql_select_db("tdm_inventory", $link);
 if (mysql_errno()) 
 { 
   echo mysql_errno($link) . ": " . mysql_error($link) . "\n";
 }
 $add = $_POST["Add"]
 $query = sprintf("UPDATE cardLists SET AmountLeft = AmountLeft + %s WHERE cardID = 'Test'", mysql_real_escape_string($add));
 mysql_query($query);
 if (mysql_errno()) 
 { 
   echo mysql_errno($link) . ": " . mysql_error($link) . "\n";
 }
 echo "test successful";
 mysql_close($link);
?>
carson
Sorry, I did not actually leave the variable there originally. I was just trying something out and forgot to change it back. And thanks to whoever changed my database information out. I cant believe I forgot to remove that myself
shinjuo
@shinjuo: It's still available in the revision log. Please change your db user / pass.
jasonbar
I changed the login info now from that screwup
shinjuo
carson, you also commit the cardinal sin of assuming the query will be successful, even if it is syntactically valid. Slap in a mysql_error() check in there as well.
Marc B
Where would I put that at to test?
shinjuo
Also I noticed that this does not add. I want to add the new amount to the original amount in the database
shinjuo
@marc-b Yeah but that isn't nearly as bad, was trying to keep it as close to the original as I could. Anyway, I added some error checking now.
carson
@shinjuo See if the revision gives you more insight into what is going on...
carson
Everything works great except the mysql_errno. If I take that part out everything works. If I leave it in it does nothing.
shinjuo
Also while I have all this put up there, does anyone know an easy way to make it populate a drop down menu with all entries in one column. That would make it easier than having to enter it in everytime
shinjuo