Jump to content


Photo

Deletion from a database using submit button

database php submit

  • Please log in to reply
3 replies to this topic

#1 Bendoon

Bendoon

    Member

  • Members
  • PipPip
  • 17 posts

Posted 19 March 2014 - 05:28 PM

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 );
?>


#2 Bendoon

Bendoon

    Member

  • Members
  • PipPip
  • 17 posts

Posted 19 March 2014 - 06:54 PM

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



#3 Ch0cu3r

Ch0cu3r

    Advanced Member

  • Moderators
  • 2,013 posts

Posted 20 March 2014 - 07:11 AM

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


#4 Bendoon

Bendoon

    Member

  • Members
  • PipPip
  • 17 posts

Posted 20 March 2014 - 08:49 AM

Omg your a lifesaver!!! I knew the problem was small but I couldnt spot it!!! thanks so much man!






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com