Jump to content

[SOLVED] Data not updating even though it was successful


Eiolon

Recommended Posts

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>

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.

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>

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.

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.