Hi all, ive got a database connection set up called students and ive made a button to add more students to the database by typing in their name and clicking the “add student” button, what im trying to do now it be able to delete a student out of the database, im trying this by adding a “delete student” button beside each entry in the database, but cant figure out why it wont work :), Code is below. Appreciate any help you can give ! Thanks!
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', 'Username: ', 'username', 30, 30, '', false);
output_submit_button('Add Student');
echo "</form>";
}
// Try to connect to database
$dbconnection = mysqli_connect( "localhost", "####", "####", "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']) )
{
$delete_sql = "DELETE FROM students
WHERE id = {$row['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 "<form action=\"{$_SERVER['PHP_SELF']}\" name=\"delete\" method=\"post\">";
echo "<p><input type=\"button\" name=\"delete\" value=\"Delete Student\" />{$row['id']} {$row['username']}</p>";
echo "<input type=\"hidden\" name=\"delete\" value=\"{$row['id']}\">";
}
}
output_form();
// Free up memory and close the database connection
mysqli_free_result( $dbretrieve_result );
mysqli_close( $dbconnection );