cs.punk Posted December 22, 2008 Share Posted December 22, 2008 Ok I got a form that adds contents to a MYSQL table. But now I want one that updates/changes within the table. I used a HTML form and got something like change 'amount?'' WHERE '$_POST' = '$_POST' ... I just confused myself now... Nevermind that coding... But it does not work where I try to retrieve the forms (posts?)? But using a form is a bad thing right? How else can I change the contents within a MYSQL table?... Quote Link to comment Share on other sites More sharing options...
Adam Posted December 22, 2008 Share Posted December 22, 2008 HTML form's aren't necessarily bad if you want to control what's modified, but you need to be careful with security. What you need to look into are MySQL queries and PHP. A simple UPDATE query performed in PHP: <?php // assuming you've connected to the database $query = mysql_query("UPDATE numbers SET num + 1"); ?> I can't ever think of a reason why that query would be helpful but, illustrates the point. "numbers" is the table name, "num" is the field. http://dev.mysql.com/doc/refman/5.0/en/update.html EDIT: Forgot to mention that when you see values in the URL such as "?this=xx&that=yy" .. You return their value in PHP with: echo $_GET['this']; echo $_GET['that']; Lemme' know if you get stuck! A Quote Link to comment Share on other sites More sharing options...
cs.punk Posted December 22, 2008 Author Share Posted December 22, 2008 Uhm here the form is: <html> <body> <form action="update.php" method="post"> Item: <input type="text" name="item" /> Amount: <input type="text" name="amount" /> <input type="submit" /> </form> </body> </html> Now on the receivng end <?php $con = mysql_connect("localhost","root","sorry removed lol"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("stock", $con); mysql_query("UPDATE stock SET amount = ''$_POST[amount]'' WHERE item = '$_POST[item); mysql_close($con); ?> Quote Link to comment Share on other sites More sharing options...
grissom Posted December 22, 2008 Share Posted December 22, 2008 Some of your inverted commas seem to be a bit awry, try this and see if it works any better, pay special attention to single versus double commas. mysql_query("UPDATE stock SET amount = '$_POST[amount]' WHERE item = '$_POST[item]' "); Let me know if it's any warmer .. Quote Link to comment Share on other sites More sharing options...
cs.punk Posted December 23, 2008 Author Share Posted December 23, 2008 it did not work either strange?... Although if i remove the post things and add a item and a number it works perfectly! Quote Link to comment Share on other sites More sharing options...
Adam Posted December 23, 2008 Share Posted December 23, 2008 Check the values of the two $_POST vars.. die('amount: ' . $_POST['amount'] . ' item: ' . $_POST['item']); .. Make sure they're what you expect! A Quote Link to comment Share on other sites More sharing options...
cs.punk Posted December 30, 2008 Author Share Posted December 30, 2008 LoL check it over and over. And I finally figured out the problem. <?php $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("mydatabase", $con); mysql_query(" UPDATE stuff SET Amount = ('$_POST[amount]') WHERE Item = ('$_POST[item]') "); mysql_close($con); ?> All it needed was () brackets for the '$_POST[example]' . Thanks to everyone! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.