grk101 Posted July 24, 2010 Share Posted July 24, 2010 i have be looking at google for a while and this was my last result. i can't figure out what i am doing wrong if someone can land a hand? basically the information has been written to the db fine and dandy i see it there, i just can't get the update below to work so it populates the form and also shows it! it just gives me a blank page <? $id=$_GET['id']; mysql_connect("mysql.xxx.com", "usre", "pass") or die('Unable to connect to database' .mysql_error()); mysql_select_db("contactsfind"); $query="SELECT * FROM contacts WHERE id='$id'"; $result=mysql_query($query); $num=mysql_num_rows($result); mysql_close(); $i=0; while ($i < $num) { $first=mysql_result($result,$i,"first"); $last=mysql_result($result,$i,"last"); $phone=mysql_result($result,$i,"phone"); $mobile=mysql_result($result,$i,"mobile"); $fax=mysql_result($result,$i,"fax"); $email=mysql_result($result,$i,"email"); $web=mysql_result($result,$i,"web"); ?> <form action="updated.php"> <input type="hidden" name="ud_id" value="<? echo "$id"; ?>"> First Name: <input type="text" name="ud_first" value="<?=$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> Mobile Number: <input type="text" name="ud_mobile" value="<? echo "$mobile"?>"><br> Fax Number: <input type="text" name="ud_fax" value="<? echo "$fax"?>"><br> E-mail Address: <input type="text" name="ud_email" value="<? echo "$email"?>"><br> Web Address: <input type="text" name="ud_web" value="<? echo "$web"?>"><br> <input type="Submit" value="Update"> </form> <? ++$i; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/208729-what-am-i-doing-wrong-php-sql-update/ Share on other sites More sharing options...
trq Posted July 24, 2010 Share Posted July 24, 2010 Your code is terrible. Never use short open tags, always check your queries succeed before using there result, make sure variables are set before executing code that relies on them, always sanitize data coming from outside & why loop when your only expecting one row? <?php if (isset($_GET['id'])) { mysql_connect("mysql.xxx.com", "usre", "pass") or die('Unable to connect to database' .mysql_error()); mysql_select_db("contactsfind"); $id = mysql_real_escape_string($_GET['id']); $query = "SELECT * FROM contacts WHERE id='$id' LIMIT 1"; if ($result = mysql_query($query)) { if (mysql_num_rows($result)) { $row = mysql_fetch_assoc($result); extract($row); // form goes here. } else { // no results found, handle error } } else { // query failed, handle error } } else { // id not set, handle error } ?> Quote Link to comment https://forums.phpfreaks.com/topic/208729-what-am-i-doing-wrong-php-sql-update/#findComment-1090479 Share on other sites More sharing options...
trq Posted July 24, 2010 Share Posted July 24, 2010 Actually, I see you might actually want the loop after all. Just seems odd creating multiple forms but anyway.... <?php if (isset($_GET['id'])) { mysql_connect("mysql.xxx.com", "usre", "pass") or die('Unable to connect to database' .mysql_error()); mysql_select_db("contactsfind"); $id = mysql_real_escape_string($_GET['id']); $query = "SELECT * FROM contacts WHERE id='$id'"; if ($result = mysql_query($query)) { if (mysql_num_rows($result)) { while ($row = mysql_fetch_assoc($result)) { extract($row); // form goes here. } } else { // no results found, handle error } } else { // query failed, handle error } } else { // id not set, handle error } ?> Quote Link to comment https://forums.phpfreaks.com/topic/208729-what-am-i-doing-wrong-php-sql-update/#findComment-1090480 Share on other sites More sharing options...
grk101 Posted July 24, 2010 Author Share Posted July 24, 2010 to be honest this code i found on a tutorial , i copied some of it, fixed it up a bit, things i didn't like and bam didn't work either way so the following is not being used? $i=0; while ($i < $num) { $first=mysql_result($result,$i,"first"); $last=mysql_result($result,$i,"last"); $phone=mysql_result($result,$i,"phone"); $mobile=mysql_result($result,$i,"mobile"); $fax=mysql_result($result,$i,"fax"); $email=mysql_result($result,$i,"email"); $web=mysql_result($result,$i,"web"); doesn't the above just loop through the results to be updated? Quote Link to comment https://forums.phpfreaks.com/topic/208729-what-am-i-doing-wrong-php-sql-update/#findComment-1090481 Share on other sites More sharing options...
trq Posted July 24, 2010 Share Posted July 24, 2010 Nothing in your code updates anything. The snippet in your last reply simply assigns values from the database columns, to there own respective variables. This.... while ($row = mysql_fetch_assoc($result)) { extract($row); } Does exactly the same thing. Quote Link to comment https://forums.phpfreaks.com/topic/208729-what-am-i-doing-wrong-php-sql-update/#findComment-1090484 Share on other sites More sharing options...
grk101 Posted July 24, 2010 Author Share Posted July 24, 2010 I have to admit i getting stupid this late at this.. i have done the following <? if (isset($_GET['id'])) { mysql_connect("****", "****", "****") or die('Unable to connect to database' .mysql_error()); mysql_select_db("contactsfind"); $id = mysql_real_escape_string($_GET['id']); $query = "SELECT * FROM contacts WHERE id='$id'"; if ($result = mysql_query($query)) { if (mysql_num_rows($result)) { while ($row = mysql_fetch_assoc($result)) { extract($row); ?> <form action="updated.php"> <input type="hidden" name="ud_id" value="<? echo "$id"; ?>"> First Name: <input type="text" name="ud_first" value="<?=$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> Mobile Number: <input type="text" name="ud_mobile" value="<? echo "$mobile"?>"><br> Fax Number: <input type="text" name="ud_fax" value="<? echo "$fax"?>"><br> E-mail Address: <input type="text" name="ud_email" value="<? echo "$email"?>"><br> Web Address: <input type="text" name="ud_web" value="<? echo "$web"?>"><br> <input type="Submit" value="Update"> </form> <? } } else { // no results found, handle error echo ' no results found' } } else { // query failed, handle error echo ' query failed' } } else { // id not set, handle error} echo ' the id is not set' } ?> ugh i don't know what i am doing the form wont show at all. Quote Link to comment https://forums.phpfreaks.com/topic/208729-what-am-i-doing-wrong-php-sql-update/#findComment-1090487 Share on other sites More sharing options...
trq Posted July 24, 2010 Share Posted July 24, 2010 Use the proper <?php tags. Quote Link to comment https://forums.phpfreaks.com/topic/208729-what-am-i-doing-wrong-php-sql-update/#findComment-1090499 Share on other sites More sharing options...
grk101 Posted July 24, 2010 Author Share Posted July 24, 2010 I changed this and stil the form doesn't appear for the love of me! I see it on dreamweaver fine, but when i upload it and load the update.php file.. the form is not there. <?php if (isset($_GET['id'])) { mysql_connect("xxxxxx", "tsxxxxx1", "xxxxxxx") or die('Unable to connect to database' .mysql_error()); mysql_select_db("contactsfind"); $id = mysql_real_escape_string($_GET['id']); $query = "SELECT * FROM contacts WHERE id='$id'"; if ($result = mysql_query($query)) { if (mysql_num_rows($result)) { while ($row = mysql_fetch_assoc($result)) { extract($row); ?> <form action="updated.php"> <input type="hidden" name="ud_id" value="<? echo "$id"; ?>"> First Name: <input type="text" name="ud_first" value="<?=$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> Mobile Number: <input type="text" name="ud_mobile" value="<? echo "$mobile"?>"><br> Fax Number: <input type="text" name="ud_fax" value="<? echo "$fax"?>"><br> E-mail Address: <input type="text" name="ud_email" value="<? echo "$email"?>"><br> Web Address: <input type="text" name="ud_web" value="<? echo "$web"?>"><br> <input type="Submit" value="Update"> </form> <?php } } else { // no results found, handle error } } else { // query failed, handle error } } else { // id not set, handle error} } ?> Quote Link to comment https://forums.phpfreaks.com/topic/208729-what-am-i-doing-wrong-php-sql-update/#findComment-1090509 Share on other sites More sharing options...
Pikachu2000 Posted July 24, 2010 Share Posted July 24, 2010 Perhaps you should replace the comments at the end of the script with some echos so you can see what's going on. Quote Link to comment https://forums.phpfreaks.com/topic/208729-what-am-i-doing-wrong-php-sql-update/#findComment-1090527 Share on other sites More sharing options...
grk101 Posted July 24, 2010 Author Share Posted July 24, 2010 hmmm, it says the id is not set, bascally the handle error that is commented. though aren't'we defining the id with the GET? Quote Link to comment https://forums.phpfreaks.com/topic/208729-what-am-i-doing-wrong-php-sql-update/#findComment-1090531 Share on other sites More sharing options...
PFMaBiSmAd Posted July 24, 2010 Share Posted July 24, 2010 You need to do this EVERYWHERE in your code - Use the proper <?php tags. Also, your hidden form field is not named id (assuming the form in the code you posted is what you expect to be submitting to the code in question.) Quote Link to comment https://forums.phpfreaks.com/topic/208729-what-am-i-doing-wrong-php-sql-update/#findComment-1090534 Share on other sites More sharing options...
grk101 Posted July 24, 2010 Author Share Posted July 24, 2010 well, initially the code was different int he first post, therefore i changed it up according to the suggestion from thorpe ( thanks btw) i have changed the form to the correct variable. what i wanted this form to do is populate the entries from the database so i can edit it and submit the changes. though , i can't get the form to display i have added the <?php tags but the echo said the id is not set Quote Link to comment https://forums.phpfreaks.com/topic/208729-what-am-i-doing-wrong-php-sql-update/#findComment-1090539 Share on other sites More sharing options...
trq Posted July 24, 2010 Share Posted July 24, 2010 Are you passing the id param along with the request for this page? Quote Link to comment https://forums.phpfreaks.com/topic/208729-what-am-i-doing-wrong-php-sql-update/#findComment-1090540 Share on other sites More sharing options...
grk101 Posted July 24, 2010 Author Share Posted July 24, 2010 from what i understand, i can't just land on update.php and have the form populate right? that would be useless right? But if i click Update on a link and pass the variable for the ID - so get can work then i can populate the form right? Quote Link to comment https://forums.phpfreaks.com/topic/208729-what-am-i-doing-wrong-php-sql-update/#findComment-1090545 Share on other sites More sharing options...
trq Posted July 24, 2010 Share Posted July 24, 2010 You need to pass an id to this script via the url so that your query has something to look up. Quote Link to comment https://forums.phpfreaks.com/topic/208729-what-am-i-doing-wrong-php-sql-update/#findComment-1090550 Share on other sites More sharing options...
grk101 Posted July 24, 2010 Author Share Posted July 24, 2010 so even though there are a few entries in the db, unless the URL passes an ID=1 or whatever then populating the form is useless without that unless i hard code it in the script? still dumb founded as to why the form doesn't show but what can u do Quote Link to comment https://forums.phpfreaks.com/topic/208729-what-am-i-doing-wrong-php-sql-update/#findComment-1090552 Share on other sites More sharing options...
trq Posted July 24, 2010 Share Posted July 24, 2010 The form doesn't show up because the very first line checks to see if the script has been passed an id. if (isset($_GET['id'])) { Quote Link to comment https://forums.phpfreaks.com/topic/208729-what-am-i-doing-wrong-php-sql-update/#findComment-1090564 Share on other sites More sharing options...
PFMaBiSmAd Posted July 24, 2010 Share Posted July 24, 2010 grk101, we only see the information you supply in your posts. Your code is retrieving the records that correspond to a specific id. Your code is getting that id from $_GET['id'], a GET parameter on the end of the URL. Since that does make sense as a method to display records to allow them to be edited (though if this was a real application, you would need to check if the current visitor is logged in and is permitted to access the records with that id) we must assume that you intended your code to work using this method or you would not have written it that way. It is up to you to provide a way in your application of visiting the page with the code you are showing us so that the expected GET parameter is present on the end of the URL or you need to change the way your application works if you intended to supply the id using some other method. Quote Link to comment https://forums.phpfreaks.com/topic/208729-what-am-i-doing-wrong-php-sql-update/#findComment-1090661 Share on other sites More sharing options...
grk101 Posted July 24, 2010 Author Share Posted July 24, 2010 well i would assume even with no data loaded the form would still show but with no values no? Quote Link to comment https://forums.phpfreaks.com/topic/208729-what-am-i-doing-wrong-php-sql-update/#findComment-1090698 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.