| | |
Using php and html forms to update a mysql database
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Dec 2007
Posts: 4
Reputation:
Solved Threads: 0
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!
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!
php Syntax (Toggle Plain Text)
<html> <body> <form name="form" method="post" action="test.php"> Number: <input type="text" name="number"><br /> <input type="submit" name="add" value="+"><input type="submit" name="subtract" value="-"> </form> </body> </html> <?php $conn=mysql_connect("localhost","username","password"); mysql_select_db("dbname"); if(isset($_POST['add'])){ $add=$_POST['number']; $query="update goods set stock=stock+$add where id=1"; mysql_query($query) or die("Cannot update"); } if(isset($_POST['subtract'])){ $subtract=$_POST['number']; $query="update goods set stock=stock-$subtract where id=1"; mysql_query($query) or die("Cannot subtract"); } ?>
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*
*PM asking for help will be ignored*
![]() |
Similar Threads
- Using php and html forms to update a mysql database (Community Introductions)
Other Threads in the PHP Forum
- Previous Thread: Parse CSV file header
- Next Thread: new to PHP
| Thread Tools | Search this Thread |
# 5.2.10 alexa apache api array beginner binary broken cakephp checkbox class clean clients cms code cron curl database date directory display dissertation dynamic echo echo$_get[x]changingitintovariable... email encode error fairness file files folder form forms function functions google href htaccess html image images include indentedsubcategory insert ip javascript joomla legislation limit link local login mail memberships menu mlm multiple multipletables mysql mysqlquery newsletters oop open paypal pdf persist php problem provider query radio random recursion remote rss script search server sessions simple sms sockets source space spam sql syntax system table tutorial update upload url validator variable video web youtube






you are welcome! 