<?xml version="1.0" encoding="utf-8"?>
<tutorial>

<description>Learn how to add a row to a mySQL database using a PHP form.</description>
<keywords>tutorial, PHP, tutorials, mysql, database, add, row, field</keywords>
<title>Add a Row to mySQL</title>

<slug>Learn how to add a row to a mySQL database using a PHP form.</slug>

<text>
Here is an useful script that Spoono uses to update the news.We have a database called spoono_news, with a table called news. In the mySQL news table, we have 6 fields: id, title, message, who, date, and time. You can see the mySQL code here: <link><url>mysql.txt</url>mysql.txt</link>. What we're trying to do in this script is to add a new post to the news, which requires adding another row to the mySQL and putting the information in. 
<br></br><br></br>
Alright, adding a row to mySQL through forms can basically be broken into 2 sections. One is the form, and one is the processing after the form. To get started, open up a blank HTML page and save it as add.php. Everything we will be coding will be in the Body of one HTML page. Essentially, you could copy-paste this code all into one big file and change it around from there. You can download a working copy that I used to test this code by <link><url>add.txt</url>right clicking and saving here</link>.
<br></br><br></br>
Lets get started: we have to work backwards in that first we'll have to write the processing for the news before hand, and the form later. Both of these can be done with one PHP script. Lets get started with the processing.<br></br><br></br>
Here is what we have to write in English for the processing:
<ol>
	<li>If submit is hit, then connect to the database</li>
	<li>Insert the values into the correct database.</li>
	<li>Get a conformation that it has been uploaded</li>
</ol>
Here it is in PHP:

<![CDATA[
<pre>
&lt;? 
//initilize PHP

if($_POST['submit']) //If submit is hit
{
   //then connect as user
   //change user and password to your mySQL name and password
   mysql_connect("localhost","user","password"); 
	
   //select which database you want to edit
   mysql_select_db("spoono_news"); 

   //convert all the posts to variables:
   $title = $_POST['title'];
   $message = $_POST['message'];
   $who = $_POST['who'];
   $date = $_POST['date'];
   $time = $_POST['time'];
   
   //Insert the values into the correct database with the right fields
   //mysql table = news
   //table columns = id, title, message, who, date, time
   //post variables = $title, $message, '$who, $date, $time
   $result=MYSQL_QUERY("INSERT INTO news (id,title,message,who,date,time)".
      "VALUES ('NULL', '$title', '$message', '$who', '$date', '$time')"); 

    //confirm
   echo "Query Finished"; 
}
?&gt;
</pre>
]]>

And that is basically all the code we have to write for the processing. Now the hard part, writing the form, which isn't really as hard as it seems. Here is what we have to write in English for the Form:
<ol>
	<li>If submit is NOT hit, then make the form with the action as this file itself.</li>
	<li>Create an HTML table and plug in all the information. Make sure that the "name" section of each input matches up with the processing code. For example, the "$_POST['title']" on the processing up above means one of the fields down here must me named title. The ID is left blank because its Auto-Increment.</li>
	<li>Make the Submit button, so the If-Then on this runs properly.</li>
</ol>

I hope you can understand HTML forms, as the code is fairly basic. All forms are inputs, the name field becomes the PHP post variable with a value of the value field. Here is the HTML and PHP:
<![CDATA[
<pre>
&lt;?
else
{
// close php so we can put in our code
?&gt;
&lt;form method="post" action="add.php"&gt;
&lt;TABLE&gt;
&lt;TR&gt;
   &lt;TD&gt;Title:&lt;/TD&gt;
   &lt;TD&gt;&lt;INPUT TYPE='TEXT' NAME='title' VALUE='Random Update' size=60&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
   &lt;TD&gt;Message:&lt;/TD&gt;
   &lt;TD&gt;&lt;INPUT TYPE='TEXT' NAME='message' VALUE='' size=60&gt;&lt;/TD&gt;
&lt;/TR&gt;&lt;br&gt;
&lt;TR&gt;
   &lt;TD&gt;name_upper:&lt;/TD&gt;
   &lt;TD&gt;
      &lt;SELECT NAME='who'&gt;
         &lt;OPTION VALUE='Akash'&gt;Akash
         &lt;OPTION VALUE='Brian'&gt;Brian
      &lt;/SELECT&gt;
   &lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
   &lt;TD&gt;date:&lt;/TD&gt;
   &lt;TD&gt;
      &lt;!-- You can use PHP functions to automatically get the value of date --&gt;
      &lt;INPUT TYPE='TEXT' NAME='date' VALUE='&lt;? echo date("M.j.y"); ?&gt;' size=60&gt;
   &lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
   &lt;TD&gt;time:&lt;/TD&gt;
   &lt;TD&gt;
      &lt;!-- You can use PHP functions to automatically get the value of time --&gt;
      &lt;INPUT TYPE='TEXT' NAME='time' VALUE='&lt;? echo date("g:i a"); ?&gt;' size=60&gt;
   &lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
   &lt;TD&gt;&lt;/TD&gt;&lt;br&gt;
   &lt;TD&gt;&lt;INPUT TYPE="submit" name="submit" value="submit"&gt;&lt;/TD&gt; 
&lt;/TR&gt;
&lt;/TABLE&gt;
&lt;/form&gt;

&lt;?
} //close the else statement
?&gt;
</pre>
]]>

The name is title, which matches up with $title at the top, and when you run the script, the default text is going to be post on the board and ask for help and we'll try to help you out. You can see my code that I worked on and made sure worked by right clicking and saving <link><url>add.txt</url>delete.txt</link>.
</text>
</tutorial>

