Hey there again,

For today, we’re gonna take a step back and change the past. Now, what could that possibly means? Ok, today we’ll learn how to update the data that has been inserted. So, the usage for this might be for situations in which if you inserted wrong data or if the person changed his address, and the list goes on.

So, we’ll just need one page of code, and we’ll use the database that we’ve created before to learn this. Here’s the code:


 

<?php

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

$update = “UPDATE staff SET Name=’Anyone’ WHERE StaffID=1234”;

if(mysqli_query($conn,$update))
{
echo “Succesful”;
}
else
{
echo “Unsuccesful” . mysqli_error($conn);
}

?>


As always, check out the results first before looking at the explanation part of it. It works best for many reasons!

 

Explanation:

1. $update = “UPDATE staff SET Name=’Anyone’ WHERE StaffID=1234”;

– Alright, here what I did was pass the update syntax of SQL to the $update variable.

– “UPDATE staff SET Name=’Anyone’ WHERE StaffID=1234” This is the syntax.

staff is the name of the table that we’re updating,

Name is the column of what we want to update, so now we’re updating the name to Anyone. But, how can the computer know who to update? Did that question pop up in your mind?

This is where the WHERE clause plays a role. It helps you to filter the data. Now, the computer will update the name of the person with the staffid of 1234 to Anyone.

So, that’s all. And, do play with this longer. Like trying to delete the WHERE clause and see what happens. On my next post I’m gonna tell you a more practical way to update things faster. So, take care till then!

–Any questions, mail me at imdad@zeroprogrammer.com. I’d love to hear comments to help us improve this blog–