Jump to content
Larry Ullman's Book Forums

Script 10.3 Edit_User.Php Help?


Recommended Posts

Hi Larry great book. Hey everyone! This is my first time being stuck in the book. I've worked my way out from everything else. But now for the life of me I can't figure this out. I've stepped away from the computer overnight only to return the next day and be stuck once again. I keep on getting a "This page No valid ID, has been accessed in error" when I try to edit a users first or last name. I have replaced my code with Larry's code downloaded from the books/His website and Larry's code works fine. I have examined line by line, side by side Larry's code vs my code. I'm stuck. Any suggestions why My copied code no worky? Thank You... everyone..

 

 

php version PHP Version 5.2.4-2ubuntu5.19

Mysql Verion 5.0.51a-3ubuntu5.8 (Ubuntu)

Apache/2.2.8 (Ubuntu)

Edit a User

<?php #Script 10.3 Edit_user.php
//
//
$page_title = 'Edit a User';
include ('includes/header.html');
echo '<h1>Edit a User</h1>';
// check for a first name Verifiy Url submittal/ Verifiy post info
if ( (isset($_GET['id'])) && (is_numeric($_GET['id'])) ) { // From view_users.php
$id = $_GET['id'];
} elseif ( (isset($_POST['id'])) && (is_numeric($_POST['id'])) ) { // Form submission.
$id = $_POST['id'];
} else { // No valid ID, kill the script.
echo '<p class="error">This page No valid ID has been accessed in error.</p>';
include ('includes/footer.html');
exit();
}
// Connect Sql
require_once ('../mysqli_connect.php');
// check if form has been submitted
if ($_SERVER['REQUEST_METHOD'] == 'POST') {

$errors = array();

 if (empty($_POST['first_name'])) {
 $errors[] = 'You forgot to enter
your fist name';
} else {
$fn = mysqli_real_escape_string($dbc, trim($_POST['first_name']));
}

// Check for last name
  if (empty($_POST['last_name'])) {
 $errors[] = 'You forgot to enter
your last name';
} else {
$ln = mysqli_real_escape_string($dbc, trim($_POST['last_name']));
}

// check for empty email
if (empty($_POST['email'])) {
 $errors[] = 'You forgot to enter
your email.';
} else {
 $e = mysqli_real_escape_string($dbc, trim($_POST['email']));
}
if (empty($errors)) { //no errors

 // tests for unique email
$q = "SELECT user_id FROM users WHERE email='$e' AND user_id != $id";
$r = @mysqli_query ($dbc, $q);
if (mysqli_num_rows($r) == 0) {

 // makes the query:
 $q = "UPDATE users SET first_name='$fn', last_name='$ln', email='$e' WHERE
  user_id=$id LIMIT 1";
 $r = @mysqli_query ($dbc, $q);
 if (mysqli_affected_rows($dbc) == 1) {
  //if it ran ok

  //print a message
  echo '<p>The user has been edited.</p>';
 } else {
  // if it did not run
  echo '<p class="error"> The user could no be updated due to a system
  error. We Apologize for any inconvenience.</p>';
  // public message
echo '<p>' . mysqli_error($dbc) . '<br />Query: ' . $q . '</p>';
 }

} else {
 echo '<p class="error">The email address has already been registered.</p>';
}
} else { //report the errors

echo '<p class="error">The Following error(s) occurred:<br />';
foreach ($errors as $msg) {
 echo " -$msg<br />\n";
}
echo '</p><p>Please try again.</p>';
}
} // end of submit conditional
// Retrieve user infor
$q = "SELECT first_name, last_name, email FROM users WHERE user_id=$id";
$r = @mysqli_query ($dbc, $q);
if (mysqli_num_rows($r) == 1) { // valid user id

//get the user id
$row = mysqli_fetch_array ($r, MYSQLI_NUM);

echo '<form action="edit_user.php" method="post">
<p>First Name: <input type="text" name="first_name" size="15" maxlength="15" value="' . $row[0] . '" /></p>
<p>Last Name: <input type="text" name="last_name" size="15" maxlength="30" value="' . $row[1] . '" /></p>
<p>Email Address: <input type="text" name="email" size="20" maxlength="60" value="' . $row[2] . '" /></p>
<p><input type="submit" name="submit" value="Submit" /></p>
<input type="hidden" name="id" value"' . $id . '" />
</form>';
} else {
 echo '<p class="error">This page has been  1 accessed in error.</p>';

}
mysqli_close($dbc);
include ('includes/footer.html');
?>

Link to comment
Share on other sites

The error you keep seeing is hard coded into your script right at the top.

This tells me the problem lies with the 'id'.

Checklist:

are you sending the id to the page?

is the id a number? (integer)

 

If you can verify this for me and report back we'll investigate further but i think the answer may lie here..

 

Hope this helps,

Red.

  • Upvote 1
Link to comment
Share on other sites

Thank you so much! You pointed me in the right dirrection. I was Pulling my hair out. I forgot to insert an = sign in my html form code at the value"' . $id . '" when passing the value <--.

Works Now!!! Thanks Red...

 

<input type="hidden" name="id" value"' . $id . '" />
should have been
<input type="hidden" name="id" value="' . $id . '" />

Link to comment
Share on other sites

 Share

×
×
  • Create New...