943,948 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 21877
  • PHP RSS
Dec 19th, 2007
0

Using php and html forms to update a mysql database

Expand Post »
Hi,

Got a bit of a problem with form, i need to be able to update a number in a mysql database using an html form. so basically i need to increment or decrement a number using a text box and a submit button.

heres the code i got:

==============================================

<form name="input" method="get" action="select.php">
Add Stock : <input type="text" name="number1">
<input type="submit" value="+">
</form>
</pre>

<?php
$add = $_REQUEST['number1'];

// update table "goods" contents
$sql =
"UPDATE goods SET stock = stock + $add WHERE id = 1";
"UPDATE stock";
if(!($result = mysql_query($sql,$dblink))) {
print "Fail: query";
exit;
}

?>

===============================================

the code half works but you need to refresh the page and it not updating the database correctly.

also if i made a copy of this code to do the stubtraction it all goes a bit tits up

im guessing there must be a better way for me to do this ?

Someone please help! Merry christmas!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
djcritch is offline Offline
4 posts
since Dec 2007
Dec 19th, 2007
0

Re: Using php and html forms to update a mysql database

php Syntax (Toggle Plain Text)
  1. <html>
  2. <body>
  3. <form name="form" method="post" action="test.php">
  4. Number: <input type="text" name="number"><br />
  5. <input type="submit" name="add" value="+"><input type="submit" name="subtract" value="-">
  6. </form>
  7. </body>
  8. </html>
  9. <?php
  10. $conn=mysql_connect("localhost","username","password");
  11. mysql_select_db("dbname");
  12. if(isset($_POST['add'])){
  13. $add=$_POST['number'];
  14. $query="update goods set stock=stock+$add where id=1";
  15. mysql_query($query) or die("Cannot update");
  16. }
  17. if(isset($_POST['subtract'])){
  18. $subtract=$_POST['number'];
  19. $query="update goods set stock=stock-$subtract where id=1";
  20. mysql_query($query) or die("Cannot subtract");
  21. }
  22. ?>

Umm.. you forgot to put your code in [ c o d e ] [ / c o d e ] tags. Anyways, I hope this helps.
Last edited by nav33n; Dec 19th, 2007 at 8:15 am.
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Dec 19th, 2007
0

Re: Using php and html forms to update a mysql database

Thanks mate that sorted it for me!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
djcritch is offline Offline
4 posts
since Dec 2007
Dec 19th, 2007
1

Re: Using php and html forms to update a mysql database

you are welcome!
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Parse CSV file header
Next Thread in PHP Forum Timeline: new to PHP





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC