Hey there,

So on my last post I taught you how to create a database and a table. Then, you inserted data into the table. I know you are excited to learn the next step: Select. But, the best way to learn something new is by using the 80 15 5 rule. I learnt this from Qazi@cleverprogrammer. Check him out he’s awesome !

The rule is 80% of the time you spent on learning something new is by PLAYING. 15% should be spent on BUILDING. While ONLY 5% should be spent on learning something new. For more detail, check out his video.

So today lets PLAY with INSERTING the data. Try to guess how can you play with it…

Well, the goal for today is to make a form to INSERT data into the table. This makes the process of inserting easier. To do this, we do need some HTML coding, but it is simple and I guarantee you you’ll get it in no time.

~~This is a continuation from yesterday’s post. If you haven’t check it out, do so~~

Now there are going to be two coding pages. The first one is for the form and the second form is for the process/input.

1st Page


 

<!DOCTYPE html>
<body>

<form action=”insertplay2.php” method=”POST”>

<input type=”text” name=”name”>

<input type=”text” name=”id”>

<input type=”submit”>

</form>

</body>
</html>


~~I would highly recommend you to try this first and look at the results. Then look at the explanation. So you won’t get very puzzled by the explanation.~~

 

Explanation:

 

1. <form action=”insertplay2.php” method=”POST”>

–  action is where you want to send the data to.

– method is how you want it to be sent. Since it’s not really important at this moment, just use the POST method.

 

2. <input type=”text” name=”name”>

– type is the type of input.

– name is how do you identify it.

 

2nd Page


<!DOCTYPE html>
<body>

<?php

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

$name=$_POST[‘name’];

$id=$_POST[‘id’];

$insert = “INSERT INTO staff ( Name , StaffID )
VALUES ( ‘$name’ , ‘$id’)”;

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

?>

</body>
</html>


 

Explanation:

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

-$ dollar tells us it’s a variable

-$_POST is how you sent the data. Since we posted it using the POST method, you write $_POST to get it back. Inside [”] this bracket is the name of the data which you posted.

2. “INSERT INTO staff ( Name , StaffID )
VALUES ( ‘$name’ , ‘$id’)” 

– Here we put ‘$name’ and ‘$id’ as value to insert into the table.

! Remember to end php statements with ;

! Test your code by opening chrome then type localhost/’yourfilename’

~~Any questions mail me at imdad@zeroprogrammer.com~~

~~Posts come out every day. Stay TUNED and don’t miss anything~~

~~Do leave comments~~