Hey there,

For today, I’m gonna continue on what we left at on my post before. So, our GOAL today is to make the update process faster. And we’ll use the same technique we used on the insert add-ons post. We are going to make a form to update the data that we have in the database. We’re going to write two pages of code for this, and if you’ve understood the lessons I taught you before, this is gonna be a piece of cake. And as usual, we’ll use the database and table we’ve built before.

So, firstly, of course the computer needs to know who to update, right?


 

1st Page

<!DOCTYPE html>
<body>

<form action=”upplay2.php” method=”POST”>
<p>

Change the name of the staff with the
id of <input type=”text” name=”id”> to
<input type=”text” name=”newname”>
<input type=”submit”>

</p>
</form>

</body>
</html>

 


I think you already understand what this does if you tried it already and you read my lessons before. So, I’m going to provide you the explanation of ONLY what you haven’t know.

Explanation:

1. <p></p> is a html tag that is used to paragraph. Anything inside it will become part of that paragraph.

 

2nd Page


<?php

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

$id = $_POST[‘id’];

$newname= $_POST[‘newname’];

$update = “UPDATE staff SET Name=’$newname’ WHERE StaffID=$id”;

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

?>


Explanation:

1. $id = $_POST[‘id’];

     $newname= $_POST[‘newname’]; 

– Now both are used to substitute what we had sent in the form at the 1st page.

 

2. $update = “UPDATE staff SET Name=’$newname’ WHERE StaffID=$id”;

– So, I gave the update mysql syntax to the variable $update.

 

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

– So, this is the if else statement to check whether or not our query is working.

 

So, that’s all for this post. To understand better, go and do other simple codes that is related to this topic. There are a bunch of things that you can do with this mysqli delete syntax, so try it out.

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