Jump to content

PHP/MySQL Update (Solved)


Bonmot

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();?>

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

If you want thing to go in $_POST, the form needs a method of "post". A form without a method defaults to using get.
That was exactly the problem. I never would have caught that.Good eye, justsomeguy and thanks for your time!
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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...