Help with Deletion of User from a database using PHP and SQL

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

You are trying to delete from a POST but are using $row to do it. Needs to be

DELETE FROM students WHERE id = {$_POST[‘delete’]}

(From your hidden input)

Thanks a lot man! Your the best, ive been staring at that for hours! Thanks so much!

Feel free to click the Karma link by my name.

Ok just did :), also one more thing my code is now only allowing 2 entries into the student database? Any ideas on how that happened? :). Its overwriting other entries when I click “add student” but only 2 students are shown.

Looks like you have not posted all your code. Seems to be missing a few functions in your original post.

output_textfield
output_submit_button

Is this just for learning? This really isn’t how you want to go about this. Rather than having a bunch of functions, you should just include files as needed in your main page.

main.php–
includes/
adduser.php
deleteuser.php
updateuser.php

For an example, check out the PDO database I have provided.
http://www.phphelp.com/forum/the-occasional-tutorial/beginners-pdo-bumpstart-code-use-it-now!

Fixed it :), Thanks again!

ah i have a library of functions im using :), the problem was names on the form they were called delete, i changed them so they were different and it fixed it!

Sponsor our Newsletter | Privacy Policy | Terms of Service