Jump to content

PHP/MySQL Update (Solved)


Bonmot
 Share

Recommended Posts

Hi all,I have this tutorial script for updating a database. Problem is the database doesn't update. Can anyone see what's missing here?Many thanks.update.php

<?include 'dbLogin.php';mysql_connect($db_hostName,$db_user,$db_password);mysql_select_db($db_database);$query="SELECT * FROM contacts";$result=mysql_query($query);$num=mysql_numrows($result); mysql_close();$i=0;while ($i < $num) {$id=mysql_result($result,$i,"id");$first=mysql_result($result,$i,"first");$last=mysql_result($result,$i,"last");$phone=mysql_result($result,$i,"phone");?><form action="updated.php"><input type="text" name="ud_id" value="<? echo "$id"; ?>"><br />First Name: <input type="text" name="ud_first" value="<? echo "$first"?>"><br>Last Name: <input type="text" name="ud_last" value="<? echo "$last"?>"><br>Phone Number: <input type="text" name="ud_phone" value="<? echo "$phone"?>"><br><input type="Submit" value="Update"></form><?++$i;} ?>

updated.php

<?include 'dbLogin.php';mysql_connect($db_hostName,$db_user,$db_password);mysql_select_db($db_database);$ud_id=$_POST['ud_id'];$ud_first=$_POST['ud_first'];$ud_last=$_POST['ud_last'];$ud_phone=$_POST['ud_phone'];mysql_query("UPDATE contacts SET first='$ud_first', last='$ud_last', phone='$ud_phone' WHERE id='$ud_id'");echo "Record Updated";mysql_close();?>

Edited by Bonmot
Link to comment
Share on other sites

Add error reporting:

<?include 'dbLogin.php';mysql_connect($db_hostName,$db_user,$db_password) or exit(mysql_error());mysql_select_db($db_database) or exit(mysql_error());$ud_id=$_POST['ud_id'];$ud_first=$_POST['ud_first'];$ud_last=$_POST['ud_last'];$ud_phone=$_POST['ud_phone'];mysql_query("UPDATE contacts SET first='$ud_first', last='$ud_last', phone='$ud_phone' WHERE id='$ud_id'") or exit(mysql_error());echo "Record Updated";mysql_close();?>

Link to comment
Share on other sites

Thanks for the info on error reporting. I'll be able to use that a lot!In this case, there are no errors reported. What happens is, update.php creates the a form pre-filled with data from thetable ("contacts"). I change one or more fields and hit the update button. The next screen says "Record updated". I refreshthe database in phpMyAdmin and the record hasn't changed. If I browse back to the form, the update is in the form untilI refresh the page when it reloads the original unchanged data.Was just hoping there might be some clear flaw in the code I missed.

Link to comment
Share on other sites

There's nothing obvious, print out the query you're sending to MySQL to see exactly what you're asking.
Results are blank. It would seem the data is not being sent from the form. I amnow most suspicious of the form istself especially given the mix of php and htmlin the value parameters. I've tried adding/deleting semicolons but nothing seemsto help.update.php (form only):
<form action="updated.php"><input type="text" name="ud_id" value="<? echo "$id" ?>"><br />First Name: <input type="text" name="ud_first" value="<? echo "$first"?>"><br>Last Name: <input type="text" name="ud_last" value="<? echo "$last"?>"><br>Phone Number: <input type="text" name="ud_phone" value="<? echo "$phone"?>"><br><input type="Submit" value="Update"></form>

Link to comment
Share on other sites

Results are blank.
Did you print out the actual query string like this:
echo "UPDATE contacts SET first='$ud_first', last='$ud_last', phone='$ud_phone' WHERE id='$ud_id'"

Also try printing out the contents of $_POST to see if anything is amiss there:

echo nl2br(print_r($_POST, true));

Link to comment
Share on other sites

So if anyone else is trying to learn this, here is the code that works:update.php

<?include 'dbLogin.php';mysql_connect($db_hostName,$db_user,$db_password);mysql_select_db($db_database);$query="SELECT * FROM contacts";$result=mysql_query($query);$num=mysql_numrows($result); mysql_close();$i=0;while ($i < $num) {$id=mysql_result($result,$i,"id");$first=mysql_result($result,$i,"first");$last=mysql_result($result,$i,"last");$phone=mysql_result($result,$i,"phone");?><form action="updated.php" method="post"><input type="text" name="ud_id" value="<? echo "$id" ?>"><br />First Name: <input type="text" name="ud_first" value="<? echo "$first"?>"><br>Last Name: <input type="text" name="ud_last" value="<? echo "$last"?>"><br>Phone Number: <input type="text" name="ud_phone" value="<? echo "$phone"?>"><br><input type="Submit" value="Update"></form><?++$i;} ?>

updated.php

<?phpinclude 'dbLogin.php';mysql_connect($db_hostName,$db_user,$db_password) or exit(mysql_error());mysql_select_db($db_database) or exit(mysql_error());$ud_id=$_POST['ud_id'];$ud_first=$_POST['ud_first'];$ud_last=$_POST['ud_last'];$ud_phone=$_POST['ud_phone'];mysql_query("UPDATE contacts SET first='$ud_first', last='$ud_last', phone='$ud_phone' WHERE id='$ud_id'") or exit(mysql_error());echo "Record Updated";mysql_close();?>

BTW, I found this tutorial at http://www.freewebmasterhelp.com/tutorials/phpmysql/1 which, despite some setbacks, is actually a fairly good learning resource. It helped a lot despite some errors that needed figuring out. Thanks again for the help in getting this properly tweaked.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...