Problem with deleting records from DB

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Odisey
    New Member
    • May 2007
    • 14

    #1

    Problem with deleting records from DB

    I am not sure if this is a MySQL syntax error or a DB issue yet. I am getting the error from the code:

    Page Error!
    This page has been accessed in error.

    The GET is populated in the browser address window with a valid user ID, yet I get the error. I've studied this script - it should work.

    Any ideas?

    =============== =============== =====
    [PHP]
    <?php
    //This page delets a user
    //This page is accessed through view_users.php

    $page_title = 'Delete a user';
    include ('./includes/header.html');

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

    if ( (isset($_GET['id'])) && (is_numeric($_G ET['id'])) ) { //Accessed through view_users.php
    $id = $_GET['id'];

    } elseif ( (isset($_POST['id'])) &&
    (is_numeric($_P OST['id'])) ) { //Form has been submitted

    $id = $_POST['id'];

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

    echo '<h1 id="mainhead">P age error.</h1>
    <p class="error">T his page has been accessed in error.</p><p><br /><br /></p>';


    include ('./includes/footer.html');
    exit();

    }

    require_once ('mysql_connect 2.php'); //Connect to DB

    // Check if the form has been submitted

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

    if ($_POST['sure'] == 'Yes') { //Delete them

    //Make the enquiry
    $query = "DELETE FROM users WHERE user_id=$id";

    $result = @mysql_query ($query); //Run the query

    if (mysql_affected _rows() == 1) { //If it ran OK


    //Print a message
    echo '<h1 id="mainhead">D elete a User</h1>
    <p>The user has been deleted.</p><p><br /><br /></p>';

    } else { //If the query did not run

    echo '<h1 id="mainhead">S ystem error</h1>
    <p class="error"> The user could not be deleted due to a system error.</p>'; //Message
    echo '<p>' . mysql_error() . '<br /><br />Query: ' . $query . '</p>'; //Debugging message

    }

    } else { // Was not sure about deleting the user

    echo '<h1 id="mainhead"> Delete a User </h1>

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

    }

    } else { //Show the form


    //Retrieve the user's information

    $query = "SELECT CONCAT(last_nam e, ', ', first_name) FROM users WHERE user_id=$id";

    $result = @mysql_query ($query); //Run the query

    if (mysql_num_rows ($result) == 1) { // Valid user ID - show the form

    // Get the users information

    $row = mysql_fetch_arr ay ($result, MYSQL_NUM);

    //Create the form

    echo '<h2> Delete a User </h2>

    <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="checke d" />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 ID

    echo '<h1 id="mainhead">P age Error</h1>
    <p class="error">T his page has been accessed in error.</p><p><br /><br /></p>';

    }


    } //End of the main submit conditional

    mysql_close(); //Close the database connection

    include ('./includes/footer.html');

    ?>
    [/PHP]
    =============== =============== =============== ==
    Last edited by Atli; May 14 '07, 10:17 PM. Reason: Added code tags
  • Odisey
    New Member
    • May 2007
    • 14

    #2
    I found it!

    The = sign in the id input tag......

    Sorry to bother...

    Odisey

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      I'm glad you found your answer.

      Just a thought, I usually put a limit clause on all DELETE and UPDATE statements to minimize the damage if something were to go wrong.

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        If where the variable came from isn't important (as appears to be the case in your script), you can also just use $_REQUEST (starting with PHP v4.1).

        [CODE=php]
        $id = $_REQUEST['id'];
        // Or better yet...
        $id = addslashes($_RE QUEST['id']);
        [/CODE]

        http://www.php.net/manual/en/reserved.variables.php (most of the way down the page)

        Comment

        Working...