Jump to content
Larry Ullman's Book Forums

mikidudle

Members
  • Posts

    6
  • Joined

  • Last visited

Posts posted by mikidudle

  1. So,

    While I've played with this every which way I have discovered that, the id variable that shows in the url (localhost/sitename/delete_user.php?id=6) disappears from the url when I click the Submit button (localhost/sitename/delete_user.php). Please can you explain where it went, why that does that, and how to prevent that? Thanks

     

    M

  2. Hi Jonathon,

    The error for the 9.2 script is:

     

    The user could not be deleted due to a system error.

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIMIT 1' at line 1

    Query: DELETE FROM users WHERE user_id= LIMIT 1

     

    What's odd is that if I run the script and echo the variable $id right after the first isset conditional, it is empty. However, it exists everywhere else. Since $id is empty, the query fails and presents that error. I can echo $id in every part of the code and it's there, except after those conditionals.

     

    Thanks in advance!

     

    the code is:

     

    <?php # Script 9.2 - delete_user.php

     

    //This page is for deleting a user record.

    //This page is accessed through view_users.php

     

    $page_title = 'Delete a User';

    include('includes/header.htm');

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

     

    //Check for a valid user ID, through GET or POST:

    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 = $_GET['id'];

    } else { // No valid ID - kill the script ---

    echo '<p class = "error">This page has been accessed in error.</p>';

    include('includes/footer.html');

    exit();

    }

     

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

     

    //Check if the form has been submitted"

    if(isset($_POST['submitted'])) {

     

    if($_POST['sure'] == 'Yes') { //Delete the record.

     

    //Make the query:

    $q = "DELETE FROM users 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 deleted.</p>';

     

    } else { // If the query did not run OK.

    echo '<p class="error">The user could not be deleted due to a system error.</p>'; //Public Message.

    echo '<p>' . mysqli_error($dbc) . '<br />Query: ' . $q . '</p>'; // Debugging message.

    }

     

    } else { // No confirmation of deletion

    echo'<p>The user has NOT been deleted.</p>';

    }

     

    } else { //Show the form

     

    //Retrieve the users information:

    $q = "SELECT CONCAT(first_name, ' ', last_name) FROM users WHERE user_id=$id";

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

     

    if (mysqli_num_rows($r) == 1) { // Valid user ID, show the form

     

    // Get the user's information

    $row = mysqli_fetch_array($r, MYSQLI_NUM);

     

    //Create the form"

    echo '<form action="delete_user.php" method="post">

    <h3>Name: ' . $row[0] . '</h3>

    <p>Are you sure you want to delete this user?<br />

    <input type="radio" name="sure" value="Yes" /> Yes

    <input type="radio" name="sure" value="No" checked="checked" /> No</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 { // Not a valid user ID.

    echo '<p class="error">This page has been accessed in error.</p>';

    }

     

    } // End of the main submission conditional.

     

    mysqli_close($dbc);

     

    include('includes/footer.html');

    ?>

  3. Thanks Jonathon,

     

    Yes I am using the @ error suppressor - that's what's in the book. Should I remove it?

     

    Thank you so much!

     

    Here's the code:

     

    <?php # Script 9.3 - edit_user.php

     

    // This page is for editing a user record.

    // This page is accessed through view_users.php

     

    $page_title = 'Edit a User';

    include('includes/header.htm');

     

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

     

    // Check for a valid user ID, through Get or Post:

    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, kell the script.

    echo '<p class="error">This page has been accessed in error.</p>';

    include('includes/footer.html');

    exit();

    }

     

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

     

    // Check if the form has been submitted:

    if (isset($_POST['submitted'])) {

     

    $errors = array();

     

    // Check for a first name:

    if(empty($_POST['first_name'])) {

    $errors[] = 'You forgot to enter your first name.';

    } else {

    $fn = mysqli_real_escape_string($dbc, trim($_POST['first_name']));

    }

     

    // Check for the last name:

    if(empty($_POST['last_name'])) {

    $errors[] = 'You forgot to enter your last name.';

    } else {

    $ln = mysqli_real_escape_string($dbc, trim($_POST['email']));

    }

     

    // Check for an email address:

    if (empty($_POST['email'])) {

    $errors[] = 'You forgot to enter your email address';

    } else {

    $ln = mysqli_real_escape_string($dbc, trim($_POST['email']));

    }

     

    if (empty($errors)) { // If everything is ok

     

    // Test for unique email addresses:

    $q = "SELECT user_id FROM users WHERE email = '$e' AND user_id != $id";

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

    if (mysqli_num_rows($r) == 0) {

     

    // Make 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 OK

    echo'<p class="error">The user could not be edited due to a system error. We apologize for any inconvenience.</p>'; // Public message

    echo '<p>' . mysqli_error($dbc) . '<br />Query: ' . $q . '</p>'; // Debugging message.

    }

     

    } else {

    echo '<p class="error">That email address has already been registered</p>';

    }

     

    } else {

     

    echo '<p class="error">The following error(s) occurred:<br />';

    foreach ($errors as $msg) { // Print each error.

    echo" - #msg<pr /> \n";

    }

    echo '</p><p>Please try again.</p>';

     

    } // End if if (empty($errors)) IF.

     

    } //End of submit conditional.

     

    // Always show the form ...

     

    // Retrieve the user's information"

    $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, show the form.

     

    // Get the user's information:

    $row = mysqli_fetch_array ($r, MYSQLI_NUM);

     

    // Create the form

    echo '<form action="edit_user.php" method = "post">

    <p>First Name: <input type = "text" name ="first_name" size="15" maxlenght="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');

    ?>

     

  4. Hi,

    Old novice I am - confused and frustrated I'm getting.

    I see the following error: Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\XAMPP\xampp\htdocs\websitename\edit_user.php on line 91

     

    This happens in scripts 9.2 and 9.3 when they are called from script 9.1 I can't tell if this is because I think I'm talking to the database and I'm not, or if there's something about the setup I have of XAAMP - which I kind of doubt - or if there's something wrong with script 9.1. That's why I didn't initially post the code - cause I don't know what'd be helpful. I have looked over the code in 9.1 and 9.2 with a fine tooth comb. Found mistakes but nothing that fixed this error.

     

    What causes mysqli_num_rows() to return a boolean? I have been searching everywhere. Did I miss something in Larry's explanation? I'm sure it's right in front of me ... I'll be dayng-ed if I can find it!

     

    Help - and Thanks!

     

     

    Mike

×
×
  • Create New...