Jump to content
Larry Ullman's Book Forums
Sign in to follow this  
mimerkou

Chapter 9 - Script 9.3 Edit_User.php Problem

Recommended Posts

Hi there!

 

I'm facing a problem with script 9.3

I wrote the code identically as the author did in the book, but it doesn't work. When I'm trying to edit a user, it only shows the h1: Edit a User

 

I checked the source code, but it also doesn't work properly. It allows the user to be edited even if you enter the same email address.

 

I include my code to this message (comments are in greek):

 

<?php # Script 9.3 - edit_user.php

// Αυτή η σελίδα επεξεργάζεται την εγγραφή ενός χρήστη
// Η σελίδα προσπελάζεται από το σενάριο view_users.php

$page_title = 'Edit a User';

include('includes/header.html');

echo '<h1>Edit a User</h1>';

// Έλεγχος του αναγνωριστικού χρήστη, με τη μέθοδο GET ή με τη μέθοδο POST:
if ( (isset($_GET['id'])) && (is_numeric($_GET['id'])) ) {
// Από το view_users.php
$id = $_GET['id'];
} elseif ( (isset($_POST['id'])) && (is_numeric($_POST['id'])) ) { // Υποβολή της φόρμας
$id = $_POST['id'];
} else { // Μη έγκυρο αναγνωριστικό, τερματισμός του σεναρίου
echo '<p class="error">This page has been accessed in error</p>';

include('includes/footer.html');

exit();
}

require_once('../mysqli_connect.php');

// Έλεγχος αν υποβλήθηκε η φόρμα
if (isset($_POST['submitted'])) {
$errors = array();

// Ελέγξτε για μικρό όνομα
if (empty($_POST['first_name'])) {
$errors[] = 'You forgot to enter your first name';
} else {
$fn = mysqli_real_escape_string($dbc, trim($_POST['first_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']));
}

// Έλεγχος για διεύθυνση ηλεκτρονικού ταχυδρομείου
if (empty($_POST['email'])) {
$errors[] = 'You forgot to enter your email';
} else {
$e = mysqli_real_escape_string($dbc, trim($_POST['email']));
}

if (empty($errors)) { // Αν όλα είναι εντάξει
// Έλεγχος για μοναδική διεύθυνση ηλεκτρονικού ταχυδρομείου
$q = "SELECT user_id FROM users
WHERE email='$e' AND user_id != $id";
$r = @msqli_query($dbc, $q);

if (mysqli_num_rows($r) == 0) {
// Δημιουργία του ερωτήματος
$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) { // Αν εκτελέστηκε σωστά
// Εμφάνιση μηνύματος
echo '<p>The user has been edited</p>';
} else { // Αν δεν εκτελέστηκε σωστά
echo '<p class="error">The user could not be edited due to a system error.
We apologize for any inconvience.</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 { // Αναφορά σφαλμάτων
echo '<p class="error">The following error(s) occured:<br>';
foreach ($errors as $msg) { // Εκτύπωση κάθε σφάλματος
echo " - $msg<br>\n";
}
echo '</p><p>Please try again</p>';
}
}

// Πάντα εμφάνιση της φόρμας...

// Ανάκτηση στοιχείων του χρήστη
$q = "SELECT first_name, last_name, email FROM users
WHERE user_id=$id";

$r = @mysqli_query($dbc, $q);

if (mysqli_num_rows($r) == 1) {
// Έγκυρο αναγνωριστικό χρήστη, εμφάνιση της φόρμας

// Λήψη στοιχείων του χρήστη
$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="40" value="'.$row[2].'"></p>
<p><input type="submit" name="submit" value="Submit"></p>
<input type="hidden" name="submitted" value="TRUE">
<input type="hidden" name="id" value="'.$id.'">
</form>';

} else { // Μη έγκυρο αναγνωριστικό
echo '<p class="error">This page has been accessed in error.</p>';
}

mysqli_close($dbc);

include('includes/footer.html');

?>

 

Any suggestions or help please!?

 

Share this post


Link to post
Share on other sites

I assume this query is failing:

 

$q = "SELECT first_name, last_name, email FROM users WHERE user_id=$id";

 

You can confirm by removing the error suppression operator from here:

 
$r = @mysqli_query($dbc, $q);
 
Or by running the query directly yourself.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...