Bendoon Posted March 19, 2014 Share Posted March 19, 2014 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 ); ?> Quote Link to comment Share on other sites More sharing options...
Bendoon Posted March 19, 2014 Author Share Posted March 19, 2014 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 Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted March 20, 2014 Share Posted March 20, 2014 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>"; } Quote Link to comment Share on other sites More sharing options...
Bendoon Posted March 20, 2014 Author Share Posted March 20, 2014 Omg your a lifesaver!!! I knew the problem was small but I couldnt spot it!!! thanks so much man! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.