Forum Moderators: coopster

Message Too Old, No Replies

Help with mysql query update form

mysql, php, form, update

         

beebob1

9:30 am on Aug 13, 2008 (gmt 0)

10+ Year Member



Hi im setting up my first news system with php/mysql, it all works except i cant update ( i can delete though), if someone could look at my update script i would be grateful. Ive just used the field name and age to get started.

I think the code im having trouble with is:
mysql_query("update example set name='$name', age='$age' where id='$id'");

// PHP update script

mysql_connect("localhost", "root", "root") or die(mysql_error());
mysql_select_db("pet") or die(mysql_error());;

if($_POST['Submit']){

$name=$_POST['name'];
$age=$_POST['age'];

mysql_query("update example set name='$name', age='$age' where id='$id'");

header("location:select.php");
exit;
}

// END PHP update script

The form and script are on the same page:

<form id="form1" name="form1" method="post" action="<? echo $PHP_SELF; ?>">
ID: <? echo $row['id']; ?><br />

Name :
<input name="name" type="text" id="name" value="<? echo $row['name']; ?>"/>

Age :
<input name="age" type="text" id="age" value="<? echo $row['age']; ?>"/>

<input type="submit" name="Submit" value="Submit" />

</form>

beebob1

9:55 am on Aug 13, 2008 (gmt 0)

10+ Year Member



dont worry, i realised there was no posting of the ID...doh!

elitebomber

8:08 pm on Aug 13, 2008 (gmt 0)

10+ Year Member



In the code you posted, the variables $id and $row have not been defined. When the mysql query is ran and $id is not defined, nothing will be updated because the WHERE clause won't return anything. Maybe you've defined it somewhere else?

The update syntax is fine.