Last user being removed from database and not one specified

Ok so thought i had this code finished but found a bug :(, the program has buttons beside each students name in the database with the value “Delete Student Number: (Student no. here)” but when i click the button its just deleting the last person in the database and not the student corresponding to the button, any ideas? Code below:

<?php echo "

Student Database

"; require_once('output_functions.php'); function is_initial_request() { return ! isset($_POST['submit']); } function output_form() { echo ""; output_textfield('username', 'New Student: ', 'username', 30, 30, '', false); output_submit_button('Add Student'); echo ""; } // Try to connect to database $dbconnection = mysqli_connect( "localhost", "dkf3", "edsfdsfy", "2017_dfk3" ); 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']; echo $id; $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 ""; echo ""; echo ""; echo ""; echo ""; echo "
Student ID NO. {$row['id']}
Student Name: {$row['username']}
"; echo "
"; } } output_form(); // Free up memory and close the database connection mysqli_free_result( $dbretrieve_result ); mysqli_close( $dbconnection ); ?>

I think it may be because after the while loop ends the $id = $_POST[‘deleteStudent’]; is always set to the last ID number?

Hi thanks for the reply guys, it gave back this:

Array ( [deleteStudent] => 221 [delete_row] => Delete Student Number: 218 [username] => )

I think its because in the while loop:

while ( $row = mysqli_fetch_assoc( $dbretrieve_result ) )
{
echo $row[‘id’];
echo “

”;
echo “<form action=”{$_SERVER[‘PHP_SELF’]}" name=“delete_student” method=“POST”>";
echo “<input type=“hidden” name=“deleteStudent” value=”{$row[‘id’]}">";
echo “”;
echo “”;
echo “

<input type=“submit” name=“delete_row” value=“Delete Student Number: {$row[‘id’]}” />

Student ID NO. {$row[‘id’]}
Student Name: {$row[‘username’]}
”;
echo “
”;
}

once this is finished the last value being sent from the hidden input type is the last $row[‘id’] ? Instead of only being selected when a button is pressed?

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 :frowning:

I’ve cleaned up your code. May I suggest closing PHP when you’re writing HTML because it can get disorganized echoing all of it and the source code viewed in browser usually looks ridiculously cluttered and mixed up.

Copy/paste this and let me know what it does…if anything.

[php]<?php
echo “

Student Database

”;
require_once(‘output_functions.php’);
function is_initial_request() {
return ! isset($_POST[‘submit’]);
}
function output_form() {
echo “”;
output_textfield(‘username’, 'New Student: ', ‘username’, 30, 30, ‘’, false);
output_submit_button(‘Add Student’);
echo “”;
}
// Try to connect to database
$dbconnection = mysqli_connect(“localhost”, “dkf3”, “edsfdsfy”, “2017_dfk3”);
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’])) {
$idP = $_POST[‘deleteStudent’];
echo $idP;
$delete_sql = “DELETE FROM students WHERE id = “.$idP.””;
$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)) {
$idR = $row[‘id’];
$usernameR = $row[‘username’];
?>

Student ID NO.<?php echo $idR; ?>
Student Name: <?php $usernameR; ?>

<?php } } output_form(); // Free up memory and close the database connection mysqli_free_result( $dbretrieve_result ); mysqli_close( $dbconnection ); ?>[/php]

Hey thanks a million for helping me man! Yeah that code now works perfectly for deleting the users as we wanted but it no longed lets me add users to the database?

Hey man fixed it , thanks for helping me really appreciate it ! Im only a beginner so I am thankful for your advice about closing the php and re-opening ! Thanks very much!

Oh no wait that code is still deleteing the only the last user in the database ><, the code you provided was deleting students from the database but still only working from the bottom up and it wasnt letting me add new students, I put the form back in the While loop which enables me too add students again but there is still of the problem of it deleting the last entry.

What he showed you is a Ternary operator. It is basically a short if/else, true/false operator.

Solved :), wasnt being closed :). Thanks for the help guys appreciate it!

Sponsor our Newsletter | Privacy Policy | Terms of Service