Eiolon Posted February 18, 2009 Share Posted February 18, 2009 I called up the record, which it did fine. When I make a change to any of the fields, it says its successful and directs me to the confirmation page instead of erroring out. However, the information is not saved. Any ideas? <?php session_start(); require_once('../mysql.php'); $id = (int) $_GET['id']; function escape_data ($data) { global $dbc; if (ini_get('magic_quotes_gpc')) { $data = stripslashes($data); } return mysql_real_escape_string (htmlspecialchars(trim(strip_tags($data))), $dbc); } $query_users = "SELECT username, first_name, last_name, phone1, phone2, email, admin FROM users WHERE id = '$id'"; $users = mysql_query($query_users) OR die ('Cannot retrieve a list of users.'); $row_users = mysql_fetch_assoc($users); if (isset($_POST['submit'])) { $errors = array(); if (empty($_POST['first_name'])) { $errors[] = 'You must enter a first name.'; } if ($_POST['first_name']) { $fn = escape_data($_POST['first_name']); } if (empty($_POST['last_name'])) { $errors[] = 'You must enter a last name.'; } if ($_POST['last_name']) { $ln = escape_data($_POST['last_name']); } if (empty($_POST['phone1'])) { $errors[] = 'You must enter a phone number.'; } if ($_POST['phone1']) { $p1 = escape_data($_POST['phone1']); } if ($_POST['phone2']) { $p2 = escape_data($_POST['phone2']); } if (empty($_POST['email'])) { $errors[] = 'You must enter an e-mail address.'; } if ($_POST['email']) { $e = escape_data($_POST['email']); } if ($_POST['admin']) { $a = escape_data($_POST['admin']); } if (empty($errors)) { $update = "UPDATE users SET first_name='$fn', last_name='$ln', phone1='$p1', phone2='$p2', email='$e', admin='$a' WHERE id = '$id'"; $result = mysql_query($update) OR die ('Could not update the user.'); if ($update) { header('Location: user_edited.php'); exit; } } } ?> <form id="edit_user" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <table border="0" cellpadding="2" cellspacing="2" width="100%"> <tr> <td width="125">First Name:</td> <td><input type="text" name="first_name" style="width:200px" maxlength="20" value="<?php echo $row_users['first_name']; ?>" /></td> </tr> <tr> <td>Last Name:</td> <td><input type="text" name="last_name" style="width:200px" maxlength="20" value="<?php echo $row_users['last_name']; ?>" /></td> </tr> <tr> <td>Phone:</td> <td><input type="text" name="phone1" style="width:200px" maxlength="20" value="<?php echo $row_users['phone1']; ?>" /></td> </tr> <tr> <td>Additional Phone:</td> <td><input type="text" name="phone2" style="width:200px" maxlength="20" value="<?php echo $row_users['phone2']; ?>" /> (optional)</td> </tr> <tr> <td>E-mail:</td> <td><input type="text" name="email" style="width:200px" maxlength="20" value="<?php echo $row_users['email']; ?>" /></td> </tr> </table> <p><input <?php if (!(strcmp($row_users['admin'],1))) {echo "checked=\"checked\"";} ?> name="admin" type="checkbox" id="admin" value="1" /> Check to make this user an administrator.</p> <input type="submit" name="submit" value="Save Changes" /> </form> Quote Link to comment https://forums.phpfreaks.com/topic/145833-solved-data-not-updating-even-though-it-was-successful/ Share on other sites More sharing options...
Q695 Posted February 18, 2009 Share Posted February 18, 2009 Are you overwriting the data, because I would do something like $statement in there if we're following your logic? Quote Link to comment https://forums.phpfreaks.com/topic/145833-solved-data-not-updating-even-though-it-was-successful/#findComment-765679 Share on other sites More sharing options...
tjohnson_nb Posted February 18, 2009 Share Posted February 18, 2009 if ($update) { header('Location: user_edited.php'); exit; } Wouldn't this always be true after you assigned a value to '$update'? Quote Link to comment https://forums.phpfreaks.com/topic/145833-solved-data-not-updating-even-though-it-was-successful/#findComment-765681 Share on other sites More sharing options...
Eiolon Posted February 19, 2009 Author Share Posted February 19, 2009 I am updating data. It is a user profile. When I pull up the info and make a change, it goes through but the data is not actually changed. The user should only be directed to user_edited.php if $update has been successfully. And since it did not DIE, then it should have updated the info. Instead, it is saying that it updated and redirecting, even though didn't actually change any of the data. Quote Link to comment https://forums.phpfreaks.com/topic/145833-solved-data-not-updating-even-though-it-was-successful/#findComment-765749 Share on other sites More sharing options...
tjohnson_nb Posted February 19, 2009 Share Posted February 19, 2009 Is it possible you mean to use $result and not $update in your if statement? if ($result) { header('Location: user_edited.php'); exit; } Quote Link to comment https://forums.phpfreaks.com/topic/145833-solved-data-not-updating-even-though-it-was-successful/#findComment-765767 Share on other sites More sharing options...
Q695 Posted February 19, 2009 Share Posted February 19, 2009 Change this line $result=@mysql_query($sql,$con) or die(mysql_error()); Kill the header for now to see if there is something going wrong on that site. Quote Link to comment https://forums.phpfreaks.com/topic/145833-solved-data-not-updating-even-though-it-was-successful/#findComment-765777 Share on other sites More sharing options...
Eiolon Posted February 19, 2009 Author Share Posted February 19, 2009 I replaced the line and took out the header. Now when I update the info it gives me a blank page with no errors. Quote Link to comment https://forums.phpfreaks.com/topic/145833-solved-data-not-updating-even-though-it-was-successful/#findComment-766207 Share on other sites More sharing options...
Q695 Posted February 19, 2009 Share Posted February 19, 2009 Does the data get changed? if ($answer=yes){ $solved='true'; } else { $solved='false'; } Quote Link to comment https://forums.phpfreaks.com/topic/145833-solved-data-not-updating-even-though-it-was-successful/#findComment-766369 Share on other sites More sharing options...
Eiolon Posted February 20, 2009 Author Share Posted February 20, 2009 Okay, here is what I have done. I have cut almost all the unneccessary code to make this work. I also limited it so only 1 field is being called up and updated. When I press the submit button, it gives me the error: Cannot retrieve user information. That is the error that is given if it cannot query the user information but it is clearly doing that. If it is not updating, then it should give this error: Could not edit the user. So here is the barebones code so if you can review and see why it's not saving the changes: <?php require_once('../mysql.php'); $query_users = "SELECT id, phone1 FROM users WHERE id = ".$_GET['id'].""; $users = mysql_query($query_users) OR die ('Cannot retrieve user information.'); $row_users = mysql_fetch_assoc($users); if (isset($_POST['submit'])) { $update = "UPDATE users SET phone1='".$_POST['phone1']."' WHERE id = ".$_GET['id'].""; $result = mysql_query($update) OR die ('Could not edit the user.'); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Edit Contact Information</title> </head> <body> <h2>Edit Contact Information</h2> <form id="edit" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> Phone Number: <td><input type="text" name="phone1" style="width:200px" maxlength="40" value="<?php echo $row_users['phone1']; ?>" /></td> <input type="submit" name="submit" value="Save Changes" /> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/145833-solved-data-not-updating-even-though-it-was-successful/#findComment-767227 Share on other sites More sharing options...
Q695 Posted February 20, 2009 Share Posted February 20, 2009 You're doing it the long way lol Quote Link to comment https://forums.phpfreaks.com/topic/145833-solved-data-not-updating-even-though-it-was-successful/#findComment-767378 Share on other sites More sharing options...
Eiolon Posted February 21, 2009 Author Share Posted February 21, 2009 What is the short way then? Quote Link to comment https://forums.phpfreaks.com/topic/145833-solved-data-not-updating-even-though-it-was-successful/#findComment-767514 Share on other sites More sharing options...
Q695 Posted February 21, 2009 Share Posted February 21, 2009 Yes: if ($_POST['submit']){ Quote Link to comment https://forums.phpfreaks.com/topic/145833-solved-data-not-updating-even-though-it-was-successful/#findComment-767533 Share on other sites More sharing options...
Eiolon Posted February 21, 2009 Author Share Posted February 21, 2009 Okay, I changed to what you recommended and still get the error: Cannot retrieve user information. I don't understand how I am getting the QUERY error when I am UPDATING. Quote Link to comment https://forums.phpfreaks.com/topic/145833-solved-data-not-updating-even-though-it-was-successful/#findComment-767787 Share on other sites More sharing options...
Cal Posted February 21, 2009 Share Posted February 21, 2009 It doesn't matter whether it's selecting, inserting or updating, they are all queries. and use or die(mysql_error()); instead of what you used. Quote Link to comment https://forums.phpfreaks.com/topic/145833-solved-data-not-updating-even-though-it-was-successful/#findComment-767789 Share on other sites More sharing options...
Eiolon Posted February 21, 2009 Author Share Posted February 21, 2009 This is weird, I use or die(mysql_error()); and it still is saying: "Cannot retrieve user information" even though it's nowhere to be found in the page! Is it possible something could be cached? Quote Link to comment https://forums.phpfreaks.com/topic/145833-solved-data-not-updating-even-though-it-was-successful/#findComment-767804 Share on other sites More sharing options...
PFMaBiSmAd Posted February 21, 2009 Share Posted February 21, 2009 What do you mean that error is no where to be found on that page, it is right in the following line for your SELECT query - $users = mysql_query($query_users) OR die ('Cannot retrieve user information.'); If you add a mysql_error() statement to that die() statement, it will tell you why the SELECT query is not working. Quote Link to comment https://forums.phpfreaks.com/topic/145833-solved-data-not-updating-even-though-it-was-successful/#findComment-767811 Share on other sites More sharing options...
Eiolon Posted February 21, 2009 Author Share Posted February 21, 2009 I found the problem. It was: <?php echo $_SERVER['PHP_SELF']; ?> in the form. I took that out and it works. Quote Link to comment https://forums.phpfreaks.com/topic/145833-solved-data-not-updating-even-though-it-was-successful/#findComment-767816 Share on other sites More sharing options...
Q695 Posted February 22, 2009 Share Posted February 22, 2009 You may want to write a death function that echos both the SQL statement, and the SQL error. I find it quite useful for when there's errors during development. Quote Link to comment https://forums.phpfreaks.com/topic/145833-solved-data-not-updating-even-though-it-was-successful/#findComment-768304 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.