Using php and html forms to update a mysql database

Thread Solved

Join Date: Dec 2007
Posts: 4
Reputation: djcritch is an unknown quantity at this point 
Solved Threads: 0
djcritch djcritch is offline Offline
Newbie Poster

Using php and html forms to update a mysql database

 
0
  #1
Dec 19th, 2007
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!
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,760
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 332
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

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

 
0
  #2
Dec 19th, 2007
  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.
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 4
Reputation: djcritch is an unknown quantity at this point 
Solved Threads: 0
djcritch djcritch is offline Offline
Newbie Poster

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

 
0
  #3
Dec 19th, 2007
Thanks mate that sorted it for me!
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,760
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 332
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

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

 
1
  #4
Dec 19th, 2007
you are welcome!
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC