Jump to content

Deletion from a database using submit button


Bendoon

Recommended Posts

Hi all, im having a problem with deleting users from a student database, the database adds new students fine, the problem is deleting a student from the system. Each student has a button beside the student name labeled "Delete Student Number: (student number here)", but for some reason the code is just always deleting the last entry from the database and not the student for which the button beside their name is pressed... Any ideas? :) Thanks!

 

<?php

echo "<h1>Student Database</h1>";

 

require_once('output_functions.php');

 

function is_initial_request()

{

return ! isset($_POST['submit']);

}

 

function output_form()

{

echo "<form action=\"{$_SERVER['PHP_SELF']}\" method=\"post\">";

output_textfield('username', 'New Student: ', 'username', 30, 30, '', false);

output_submit_button('Add Student');

echo "</form>";

}

 

// Try to connect to database

$dbconnection = mysqli_connect( "localhost", "bfm3", "eeshaica", "2017_bfm3" );

if ( ! $dbconnection )

{

die('Unable to connect to database');

}

 

// Code to allow the user to enter a new Student

if ( ! is_initial_request() )

{

$username = $_POST['username'];

// Insert into the database

$insert_sql = "INSERT INTO students ( username )

VALUES ('{$username}');";

$dbinsert_result = mysqli_query( $dbconnection, $insert_sql );

if ( ! $dbinsert_result )

{

die();

}

}

 

// Code to allow the user to delete a new Student

if ( isset($_POST['delete_row']) )

{

$id = $_POST['deleteStudent'];

$delete_sql = "DELETE FROM students

WHERE id = {$id}";

$dbdelete_result = mysqli_query( $dbconnection, $delete_sql );

}

 

$retrieve_sql = "SELECT * FROM students"; 

    $dbretrieve_result = mysqli_query( $dbconnection, $retrieve_sql );

    if ( ! $dbretrieve_result )

    {

        die();

    }

 

    if ( mysqli_num_rows( $dbretrieve_result ) != 0 ) 

    {

        while ( $row = mysqli_fetch_assoc( $dbretrieve_result ) ) 

        {

echo $row['id'];

echo "<table>";

echo "<form action=\"{$_SERVER['PHP_SELF']}\" name=\"delete_student\" method=\"POST\">";

echo "<tr><td><p><input type=\"submit\" name=\"delete_row\" value=\"Delete Student Number: {$row['id']}\" /></p></td><td><b>Student ID NO.</b> {$row['id']}</td></tr>";

echo "<input type=\"hidden\" name=\"deleteStudent\" value=\"{$row['id']}\">";

echo "<tr><td></td><td><b>Student Name:</b> {$row['username']}</td></tr>";

echo "</table>";

echo "<br>";

        }

    }

output_form();

 

// Free up memory and close the database connection

mysqli_free_result( $dbretrieve_result );

mysqli_close( $dbconnection );

?>

Link to comment
Share on other sites

Ok think ive narrowed down the problem its:

echo "<input type=\"hidden\" name=\"deleteStudent\" value=\"{$row['id']}\">";

The value thats its giving back is always the last value of the database instead of the one allocated to the button being pressed... ive tried a few different things but nothing is working so far sad.gif

Link to comment
Share on other sites

You haven't  closed the form in the while loop

while ( $row = mysqli_fetch_assoc( $dbretrieve_result ) ) 
{
    echo $row['id'];
    echo "<table>";
    echo "<form action=\"{$_SERVER['PHP_SELF']}\" name=\"delete_student\" method=\"POST\">";
    echo "<tr><td><p><input type=\"submit\" name=\"delete_row\" value=\"Delete Student Number: {$row['id']}\" /></p></td><td><b>Student ID NO.</b> {$row['id']}</td></tr>";
    echo "<input type=\"hidden\" name=\"deleteStudent\" value=\"{$row['id']}\">";
    echo "<tr><td></td><td><b>Student Name:</b> {$row['username']}</td></tr>";
    echo "</from>";// <-- missing closing form tag
    echo "</table>";
    echo "<br>";
}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.