Inserting data in MySql table using PHP

Spread the love

mysql_query()

For inserting data in MySql, PHP is having mysql_query() function where we can pass MySQL insert query to insert our data into database tables.

Synatx: mysql_query(sql_query, connection);

sql_query – its an sql query written in string format.
connection –  its connection object returned from mysql_connect function.

Before using this function lets create a new PHP script file for employees registration, named register.php and add below HTML form in it.

<form name="register" method="post" action="register.php">
<input type="text" name="empname"/>
<input type="text" name="username"/>
<input type="text" name="password"/>
<input type="submit" name="submit" value="Submit"/>
</form>

Include dbconnection.php file inside register.php at the top of form using require_once() function.

<?php
require_once("dbconnection.php");
?>

<form name="register" method="post" action="register.php">
<input type="text" name="empname"/>
<input type="text" name="username"/>
<input type="text" name="password"/>
<input type="submit" name="submit" value="Submit"/>
</form>

 

Add your files dbconnection.php and register.php in your project folder say “mysql_practice” inside your “WWW” folder in wamp. Now when this file runs on the server it will connect database with register.php. You will see form gets rendered in register.php in the browser.

When you fill data in the form and submit it will collect form’s data in register.php in $_POST superglobal array. We need to insert this data in table “employees” using mysql_query() function.

To insert data add below code in register.php.

<?php
require_once("dbconnection.php");

if(isset($_POST['submit']) && !empty($_POST['submit']) )
{
$name = $_POST['name'];
$username = $_POST['username'];
$password = $_POST['password'];

$query  = "insert into employee (name, username, password) values ('$name','$username','$password')";
$result = mysql_query($query, $con);

if($result)
{
echo "Employee registered successfully.";
}
else
{
echo "Error in registration";
}
}
?>
<form name="register" method="post" action="register.php">
<input type="text" name="empname"/>
<input type="text" name="username"/>
<input type="text" name="password"/>
<input type="submit" name="submit" value="Submit"/>
</form>

As you see in above example we have collected $_POST data and use it in SQL query, for inserting data in MySql table. We have created a query as a string and store it in the variable $query. Then we passed that query using $query variable inside mysql_query() function.

When you click on “Submit” button regiser.php will run and of all works fine your data will get stored in database table “employees”.

 

Closing MySQL connection:

After MySQL operation, we have to close the database connection. You can close database connection using the mysql_close function of PHP.

Syntax: mysql_close(connection_variable);

connection_variable – its connection object returned from mysql_connect function.


Spread the love

Leave a Comment