Forum Moderators: coopster

Message Too Old, No Replies

Simple Update Script Not Working

         

oceanwave

1:49 am on Jun 3, 2007 (gmt 0)

10+ Year Member



There are 2 pages to this tutorial on updating a database. Fields of userid, lastname, email, and phone display correctly after a userid row is selected for updating. After updating a field and clicking Submit, the database is not updated. Why?

edit.php

<?php
include("security.php");
include("config.php");

$result=mysql_query("select * from members");

while($row=mysql_fetch_assoc($result)){

echo "ID : $row[userid] &nbsp;&nbsp;&nbsp;&nbsp;";
echo "Name : $row[lastname] &nbsp;&nbsp;&nbsp;&nbsp;";
echo "Email : $row[email] &nbsp;&nbsp;&nbsp;&nbsp;";
echo "Tel : $row[phone] &nbsp;&nbsp;&nbsp;&nbsp;";

echo '<a href="update.php?userid='.$row[userid].'">Update</a><hr>';

}

mysql_close();
?>

----

update.php

<?php
include("security.php");
include("config.php");

if($_POST['Submit']){

$userid=$_POST['userid'];
$lastname=$_POST['lastname'];
$email=$_POST['email'];
$phone=$_POST['phone'];

mysql_query("update members set lastname='$lastname', email='$email', phone='$phone', where userid='$userid'");

header("location:edit.php");
exit;
}
$userid=$_GET[userid];

$result=mysql_query("select * from members where userid='$userid'");

$row=mysql_fetch_assoc($result);

mysql_close();
?>

<html>
<body>
<form id="form1" name="form1" method="post" action="<?php echo $PHP_SELF;?>">

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

<p>Name :
<input name="name" type="text" id="lastname" value="<?php echo $row['lastname'];?>"/>
<br />
Email :
<input name="email" type="text" id="email" value="<?php echo $row['email'];?>"/>
<br />
Tel :
<input name="tel" type="text" id="phone" value="<?php echo $row['phone'];?>"/>
</p>
<p>
<input type="submit" name="Submit" value="Submit" />
</p>
</form>
</body>
</html>

Habtom

5:03 am on Jun 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



//mysql_query("update members set lastname='$lastname', email='$email', phone='$phone', where userid='$userid'");

you have one comma there which you do not need to start with:
//mysql_query("update members set lastname='$lastname', email='$email', phone='$phone' where userid='$userid'");

And this is how I could have done it:
mysql_query("UPDATE members SET lastname='". $lastname ."', email='". $email ."', phone='". $phone ."' WHERE userid='". $userid ."'");

Habtom

oceanwave

1:46 pm on Jun 4, 2007 (gmt 0)

10+ Year Member



Hi Habtom,

Thanks for responding.

When I removed the extra comma, the update would leave the fields of lastname and phone completely blank.

Next, I tried your code. The same thing happened. I then realized I was able to update the email field, which again showed me there was something wrong with the lastname and phone fields. I then changed the input names in the form to lastname and phone (instead of name and tel) and everything now works.

Thank you so much for helping me!