You are on page 1of 5

STORING YOUR DATA INTO A DATABASE WITH PHP/MYSQL Lets say we need to get the following information from

our online user: Name, Sex, Address, Phone Number, and store them in a data base for retrieval at a latter date. How do we go about this task? First of all, I will create an html page with a form that accept these information, then create a database with phpmyadmin. I will then proceed to write a php code for inserting the data into the database. Html code for collecting user info Lets create a page called page1.html. In the page, paste the following code: <html> <head> </head> <body> <form action="page2.php" method="post"> Name: <input type="text" name="name"><br/> Sex: <select><option value="M">Male</option> <option value="F">Female</option> </select><br/> Address: <textarea name="addr" rows="3" cols="50"></textarea><br/> <input type="submit" name="submit"> </form> </body> </html> I presume you understand the basics of html. If not you can visit www.htmlite.com. It will give you a good starting to understand the code. Between the body tag (<body> and </body>) is a form tag. The action attribute of the form is set to page2.php. This means when the submit button is clicked, the page is redirected to page2.php. The method is basically how the form is submitted. With post, the values of what you are submitting are not attached to the url. With get, the values are attached to the url. I have decided to use post in this tutorial.

Creating the Database We will be using phpmyadmin. To do this you will need to first download wamp server. Wamp server is an open source by apache. You need this for two reasons: 1. It serve as a server for us to test run our database. 2. Php, the language we will be using can only be interpreted from a server. Hence it is called server-side program. You can download wampserver at http://www.wampserver.com/en/download.php. Alternatively, type wampserver on google. Am sure you will get many alternatives.

When installed, you will see an icon like this

on your task bar. Clicking the icon will display this:

Click on phpMyAdmin.

In the create new database box, I have typed mytutorial. Leave the other settings as they are. Click Create.

In the page that appears, you will see a prompt for Name. That is name of table. I typed mydata and in the Number of fields, I typed 4. We want to store only 3 data, but I have chosen 4 fields. This is because I want to reserve one field as an autoincrement field. That is as the number of data in my table increases, that field will increment by 1.

In the page that displays, type in your field names. The first field, id is set to type:INT and Extra: auto_increment. Int by default can take upto 11 digits. The second field name is set to type:TEXT. Text takes upto 255 characters and does not require Length/value When VARCHAR is used, the Length/Value has to be set. So I set the length to 1 because I am saving either M or F, just 1 char. Click on Save and youve created your database! Now lets move on to inserting data into the database. Create another page and call it page2.php. Notice the php extension. This is because we will be writing php code! On the page paste the following codes: <? //connect to the server and select the database. $link = mysql_connect ("localhost","root","") or die ("cannt connect ".mysql_error()); mysql_select_db("mytutorial", $link);

//----Get data: $name=$_POST["name"]; $sex = $_POST["sex"]; $addr = $_POST["addr"]; //insert the data into table mydata

$sql="insert into mydata set name='$name', sex='$sex', addr='$addr'"; mysql_query ($sql) or die ("cannt insert ".mysql_error()); ?> Note that lines starting with // means comment. I have added comment so that you understand the code. With the above discussion, I hope youve been able to successfully insert data into your database

You might also like