Hey there,

so this post is the last basic thing that I’d like all of you to know on SQL. It’s on deleting data. Now, this syntax is commonly used when you don’t need the data anymore. Say for example, when a staff resigns from a company, so at that time the staff’s data is unimportant anymore and will be deleted. So, as usual we’ll use the table and database we’ve created before to do this. We’ll only need one page of coding to do this.

Code:


<?php

$conn = mysqli_connect(“localhost”,”root”,””,”company”);

$delete = “DELETE FROM staff WHERE StaffID=1234”;

if(mysqli_query($conn,$delete))
{
echo “Successful”;
}
else
{
echo “Unsuccessful” . mysqli_error($conn);
}

?>


As always, try out this code first and see what happens to the database before reading the explanation.

 

Explanation:

1. $delete = “DELETE FROM staff WHERE StaffID=1234”;

– “DELETE FROM ” is the delete syntax from which table you want to delete. WHERE clause is used to pinpoint the data. Without the WHERE clause all the data inside the staff table would be deleted.

– So we just passed the Delete syntax to the $delete variable so that it can be called easily. It’s like giving it a name.

 

2. if(mysqli_query($conn,$delete))
{
echo “Successful”;
}
else
{
echo “Unsuccessful” . mysqli_error($conn);
}

– As always, this is just another test to see whether it’s working or not. If it pops up Successful, then it means that the data has been successfully deleted.

 

So, that’s all there is to share on SQL Delete, it’s very simple right? So don’t try to complicate things, and see you on my next post.

–PLAY WITH IT like make a form or something but at least use it once on your ideas–

–Any questions, mail me at imdad@zeroprogrammer.com–