Add new records in PHP

Add new records in PHP

So now you can view records stored in your MySQL database and display them in your browser using PHP. But you want to add new record. Assuming that you know about HTML forms, let’s code a page that will do just that. First we’ll create a static form, datain.html:

<HTML>  <BODY>  <form method="post" action="datain.php">  First name:<input type="Text" name="first"><br>  Last name:<input type="Text" name="last"><br>  Nick Name:<input type="Text" name="nickname"><br>  E-mail:<input type="Text" name="email"><br>  Salary:<input type="Text" name="salary"><br>  <input type="Submit" name="submit" value="Enter info">  </form>  </HTML>

Now we have a form that will post the information to a page “datain.php”. We must code this page so that it is able to process the posted data and send it to our MySQL database. The following listing of datain.php will do that for Add new records in PHP:

<HTML>  <?php  $db = mysql_connect("localhost", "root","");  mysql_select_db("learndb",$db);  $sql = "INSERT INTO personnel (firstname, lastname, nick, email, salary)  VALUES ('$first','$last','$nickname','$email','$salary')";  $result = mysql_query($sql);  echo "Thank you! Information entered.\n";  ?>  </HTML>

For Add new records in PHP  The first 3 lines are same as before, only we use the SQL command “INSERT INTO”, which means insert into the database into the columns specified (here firstname, lastname, nick, email) the data contained in the variable ‘$first’,’$last’,’$nickname’,’$email’, respectively.

Add new records in PHP

Add new records in PHP

But where did these variables come from? Well, PHP has a wonderful way of creating the variables automatically from the data posted to it. So the text box with name “first” created the variable $first and it contained the text typed in that textbox.

Putting it together for Add new records in PHP

Now let’s merge the code into one file. We will call it input.php

<HTML>  <?php  if($submit)  {  $db = mysql_connect("localhost", "root","");  mysql_select_db("learndb",$db);  $sql = "INSERT INTO personnel (firstname, lastname, nick, email, salary)  VALUES ('$first','$last','$nickname','$email','$salary')";  $result = mysql_query($sql);  echo "Thank you! Information entered.\n";  }  else  {  ?>  <form method="post" action="input.php">  First name:<input type="Text" name="first"><br>  Last name:<input type="Text" name="last"><br>  Nick Name:<input type="Text" name="nickname"><br>  E-mail:<input type="Text" name="email"><br>  Salary:<input type="Text" name="salary"><br>  <input type="Submit" name="submit" value="Enter information"></form>  <?  }  ?>  </HTML>

This creates a script that shows the form when there is no input or otherwise enters the information into the database. How does the script understand when to do what? We have already learned that for Add new records in PHP automatically creates variable with information posted to it. So it will create the variable $submit if the form is posted. The script determines whether there exists the variable $submit. If it exists and contains a value then we have to enter the posted values into the database; otherwise, we have to show the form. So now we can enter information to our database and view it. Try inserting some new data into the database and check to see if it really works by viewing them with viewdb.php.

andcombd