Wednesday, April 23, 2025

MySQLi Delete by ID

delete_by_id.html


<form action="delete_id.php" method="post">
<label>ID To Delete</label><br>
<input type="text" name="id_to_delete">
<br><br>

<input type="submit" value="Submit">
</form>

This form is used to delete a record from the database. It asks for the ID of the record to be deleted and has a submit button. When the form is submitted, it sends the data to a PHP script located at "delete_id.php" using the HTTP POST method.

delete_id.php


<?php
//Config

$server = "localhost";
$user = "root";
$password = "";
$database = "address_book";

$get_id_to_delete = $_POST["id_to_delete"];

//Establishing a Connection to MySQL Server
$connection = mysqli_connect($server, $user, $password, $database);

//Check Connection
if (!$connection) {
	die("<h2>Total Fail</h2> " . mysqli_connect_error());
} else {
	echo "Connection Successfull <br>";
}

//SQL Command
$sql_command = "DELETE FROM people WHERE id=$get_id_to_delete";

if (mysqli_query($connection, $sql_command)) {
	echo "SQL Command OK";	
} else {
	echo "SQL Error " . mysqli_error($connection) ;
}

?>

This PHP script connects to a MySQL server and receives a POST variable called "id_to_delete" from a form.

It then constructs a SQL command to delete a row from the "people" table where the ID matches the value of the POST variable. If the SQL command is successful, it displays the message "SQL Command OK".

If there is an error, it displays the error message using the mysqli_error() function. The script assumes that the database configuration values are correct and the connection to the database server is successful.

If you submit existing ID to delete:


Connection Successfull
SQL Command OK

Check html_table_report.php to see what's in database now.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...