Deleting row in mysql database using php script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • azmiza
    New Member
    • Dec 2006
    • 2

    Deleting row in mysql database using php script

    Hi everybody,

    I need your help.

    I want to view my sql database and its work very well which is display in my web browser
    but once I want to press button yes, its not working, I check the database the specified row was nit deleted

    Here I put my script for both.
    [php]
    <?php #script view_users.php pg 279


    include 'index1.php';

    //This script retrieves all the records from the register table.

    //This new version links to edit and delete pages.

    //Page header.
    echo '<h1 id="mainhead">R egistered Projects</h1>';

    //cONNECT TO THE DATABASE.
    require_once 'Connect/mysql_connect.p hp';

    //Make the query.

    $query = "SELECT serial, date, department, user_id, email, project, description FROM register";
    //ORDER BY serial ASC";

    //Run the query
    $result = mysql_query ($query);
    $num = mysql_num_rows( $result);

    if ($num > 0) { //If it ran OK, display the records.

    echo "<p>There are currently $num registered users.</p>\n";

    //Table header.

    echo '<table align-"center" cellspacing="15 " cellpadding-"5">
    <tr>

    <td align="left"><b >Serial</b></td>
    <td align="left"><b >Date</b></td>
    <td align="left"><b >Department</b></td>
    <td align="left"><b >User</b></td>
    <td align="left"><b >Email</b></td>
    <td align="left"><b >Project</b></td>
    <td align="left"><b >Description</b></td>
    <td align="left"><b >Edit</b></td>
    <td align="left"><b >Delete</b></td>
    </tr>
    ';

    //Fetch and print all the records.
    $bg = '#eeeeee'; //set the background color.

    while ($row = mysql_fetch_arr ay($result, MYSQL_ASSOC)) {
    // echo '<tr>
    $bg = ($bg=='#eeeeee' ? '#ffffff' : '#eeeeee'); //switch the background color.

    echo '<tr bgcolor="' . $bg . '">

    <td align="left">' . $row['serial'].'</td>
    <td align="left">' . $row['date'].'</td>
    <td align="left">' . $row['department'].'</td>
    <td align="left">' . $row['user_id'].'</td>
    <td align="left">' . $row['email'].'</td>
    <td align="left">' . $row['project'].'</td>
    <td align="left">' . $row['description'].'</td>
    <td align="left"><a href="edit_user .php?serial_num =' . $row['serial'] . '">Edit</a></td>
    <td align="left"><a href="delete_us er.php?serial_n um=' . $row['serial'] . '">Delete</a></td>
    </tr>

    ';
    }

    echo '</table>';

    mysql_free_resu lt ($result); //Free up the resources.

    } else { //If did not run OK.

    echo '<p class="error">T here are currently no registered projects.</p>';
    }

    mysql_close(); //Close the database connection.


    ?>


    and below for deleting the project specified.


    <?php #delete_user.ph p pg 283

    //This page deletes a user.
    //This page is accessed through view_users.php

    //Check for a valid user serial number ID, through GET or POST.

    if ( (isset($_GET['serial_num'])) && (is_numeric
    ($_GET['serial_num'])) ) { //acessed through view_users.php

    $serial_num = $_GET['serial_num'];

    } elseif ( (isset($_POST['serial_num']) ) &&

    (is_numeric($_P OST['serial_num'])) ) { //form has been submitted.

    $serial_num = $_POST['serial_num'];

    } 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>';
    }

    require_once 'Connect/mysql_connect.p hp'; //connect to the database.

    //check if the form has been submitted.
    if (isset($_POST['submitted'])) {

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


    //Make the query
    $query = "DELETE FROM register WHERE serial=$serial_ num";
    $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
    project</h1>

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

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

    echo '<h1 id="mainhead">S ystem
    Error</h1>

    <p class="error">T he project could
    not be deleted due to a system
    error.</p>'; //public message
    echo '<p>' . mysql_error() .
    '<br /><br />Query: '. $query .
    '</p>'; //debugging message.
    }

    } else { //wasn't not sure about deleting the project.

    echo '<h1 id="mainhead">D elete a
    project</h1>
    <p>The project has NOT been deleted.
    </p><p><br /></p>';

    }


    } else { // Show the form

    //Retrieve the user's information.

    $query = "SELECT project FROM register WHERE serial=$serial_ num";

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


    if (mysql_num_rows ($result) == 1) { //valid serial number, show the form.

    //Get the serial number information.

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


    //Create the form.

    echo '<h2>Delete the project</h2>

    <form action="delete_ user.php" method="post">
    <h3>Project: ' . $row[0] . '</h3>
    <p> Are you sure you want to delete this project?<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="serial_nu m" value="' . $serial_num . '" />
    </form>';


    } else { //Not a valid serial number.

    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.

    ?>[/php]
    Last edited by ronverdonk; Dec 8 '06, 11:07 AM. Reason: code within tags
  • Niheel
    Recognized Expert Moderator Top Contributor
    • Jul 2005
    • 2432

    #2
    wrong forum
    moved to php
    also added code tags
    niheel @ bytes

    Comment

    • ronverdonk
      Recognized Expert Specialist
      • Jul 2006
      • 4259

      #3
      Your form posts value "Submit" name "submit". [php]<input type="submit" name="submit" value="Submit" /></p>[/php]
      Your form handler checks "submitted" .[php]if (isset($_POST['submitted'])) {
      [/php]

      Ronald :cool:

      Comment

      Working...