Insert Data into MySQL Database
SQL INSERT INTO statement are used to insert record in MySQL tables.
Let's write a MySQL query to insert data by using insert into statement, after this we will execute this insert query through passing it to mysqli_query() method to store data in MySQL table.
See the example which store the record in MySQL table.
- $con = mysqli_connect("localhost", "root", "", "demo");
-
- if($con === false){
- die("ERROR: Could not connect. " . mysqli_connect_error());
- }
-
- $sql = "INSERT INTO table_name (id, name, email) VALUES (1, 'ajay', 'ajay.agrahari09@mail.com')";
- if(mysqli_query($con, $sql)){
- echo "Records added successfully.";
- } else{
- echo "ERROR: Could not able to execute $sql. " . mysqli_error($con);
- }
-
- mysqli_close($con);
/* create MySQL connection object with default setting (user 'root' with no password) */ $con = mysqli_connect("localhost", "root", "", "demo"); // Check connectionif($con === false){ die("ERROR: Could not connect. " . mysqli_connect_error()); } // Attempt insert query execution$sql = "INSERT INTO table_name (id, name, email) VALUES (1, 'ajay', 'ajay.agrahari09@mail.com')";if(mysqli_query($con, $sql)){ echo "Records added successfully."; } else{ echo "ERROR: Could not able to execute $sql. " . mysqli_error($con); } // Close connection mysqli_close($con);
Insert Record Into a Database From an HTML Form
Try out this example by putting this code in insert_employee.php, A html form is displayed which hold the values from your side and then it will store the data in MySQL table by clicking submit button.
- <title>Insert Data into MySQL Database</title>
- <?php
- if(isset($_POST['insert'])) {
- $servername = "localhost";
- $username = "username";
- $password = "password";
- $dbname = "myDB";
- $conn =mysqli_connect($servername, $username, $password, $dbname);
- if(! $conn ) {
- die('Could not connect: ' . mysqli_connect_error());
- }
- if(! get_magic_quotes_gpc() ) {
- $name = addslashes ($_POST['name']);
- $address = addslashes ($_POST['address']);
- }else {
- $name = $_POST['name'];
- $address = $_POST['address'];
- }
- $salary = $_POST['salary'];
- $sql = "INSERT INTO employee (name,address, salary,
- join_date) VALUES('$name','$address',$salary, NOW())";
- if (mysqli_query($conn, $sql)) {
- echo "New record created successfully";
- } else {
- echo "Error: " . $sql . "" . mysqli_error($conn);
- }
- mysqli_close($conn);
- }else {
- ?>
- <form method="post" action="">
- <table width="400" border="0" cellspacing="1" cellpadding="2">
- <tbody>
- <tr>
- <td width="100">Name</td>
- <td><input name="name" type="text" id="name"></td>
- </tr>
- <tr>
- <td width="100">Address</td>
- <td><input name="address" type="text" id="address"></td>
- tr>
- <tr>
- <td width="100">Salarytd>
- <td><input name="salary" type="text" id="salary"></td>
- </tr>
- <tr>
- <td width="100"> </td>
- <td> </td>
- </tr>
- <tr>
- <td width="100"> </td>
- <td><input name="insert" type="submit" id="insert" value="Add Data"></td>
- </tr>
- </tbody>
- </table>
- </form>
- <?php } ?>
title>Insert Data into MySQL Database " . mysqli_error($conn); } mysqli_close($conn); }else { ?>