Insert
form data into MySQL using php
Before you start this lesson, I assume that you finished reading
the following tutorials:
- install phpmyAdmin, mysql , php
- Create Database by using phpMyAdmin
- Working with forms
In this lesson, we will learn how to insert data collected in form
into MySQL database.
In Working with forms, we already
know how to get the form input. We need add a little bit more code
to insert data into Mysql Database.
PHP Connect to Mysql Database
The PHP code connecting to MySql database is following:
$username="root";
$password="YourPassword";
$database="phonebook";
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die("<b>Unable to specified
database</b>");
In the above code, we create 3 variables, $username,$password and
$database, which store the user name, database name and your password.
mysql_connect(<address>, <username>, <password>) connect
to your local MySQL.In our example, <address> is localhost or may
be a remote machine IP address. @mysql_select_db($database) specifies
which database in MySQL you are gonna use.Placing a @ symbol
in front of the function name tells the function to fail silently,
allowing us to display our own, friendlier error message. die(...) just
show an error message in case connection to database fails and terminate
the script execution.
Php insert data into Mysql
PHP uses the following statements to insert data into MySQL.
$name=$_POST['name'];
$phone=$_POST['phone'];
$query = "INSERT INTO contact VALUES ('$name','$phone')";
mysql_query($query) or die('Error, insert query failed');
In the above code, $name and $phone are form input from user. $query
is a standard SQL insert query, which syntax is
insert into table-name values ('','',''...);
mysql_query($query) executes the query. die(...) will print out
error message and end the script running if query fails.
Overall code of PHP insert data into MySQL.
<html>
<body>
<?php
$username="root";
$password="123456";
$database="phonebook";
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die("<b>Unable to specified
database</b>");
$name=$_POST['name'];
$phone=$_POST['phone'];
$query = "INSERT INTO contact VALUES ('$name','$phone')";
mysql_query($query) or die('Error, insert query failed');
echo "Hi! $name. Your phone number <b> $phone </b> is
in our database";
?>
</body>
</html>
Save the above code to a file named action.php. Input this file
name in your form tag "action" field. Your form may look like this:
<html>
<body>
<form action="action.php" method=post>
<input name="name" type=text ><br>
<input name="phone" type=text ><br>
<input type="submit" >
</form>
</body>
</html>
Check PHP insert result by PHPMyAdmin
You may check if your input data in database or not by using PhPMyadmin.
Launch your phpMyAdmin, select your database (phonebook in this lesson)
and click on the table "contact". Then, on the right side
you will see the following picture:
Suppose you input "Tom" as name, "123456789" as phone number. Click
on the browser, you will see the following :
Did you see your input in the above image?
Congratulations! You did it. |