Insert data into MySQL database table using php. Insert data into MySQL database table. Now that you know how to create databases and tables in MySQL. In this tutorial, you will learn how to perform SQL queries to insert records in the table.
Let’s use the INSERT INTO statement with the appropriate SQL query value, after that we will pass through it to mysqli_query () function to insert data in a table to perform this SQL query. Here is an example, it increases the staff table a record by specifying “PERSON_ID”, “FIRST_NAME” values, “surname” and “EMAIL_ADDRESS” field:

Example

<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user ‘root’ with no password) */
$link = mysqli_connect(“localhost”, “root”, “”, “demo”);

// Check connection
if($link === false){
die(“ERROR: Could not connect. ” . mysqli_connect_error());
}

// Attempt insert query execution
$sql = “INSERT INTO persons (person_id, first_name, last_name, email_address) VALUES (1, ‘Peter’, ‘Parker’, ‘peterparker@mail.com’)”;
if(mysqli_query($link, $sql)){
echo “Records added successfully.”;
} else{
echo “ERROR: Could not able to execute $sql. ” . mysqli_error($link);
}

// Close connection
mysqli_close($link);
?>

If you remember, from the previous section, “as person_id” field is marked AUTO_INCREMENT flag. This modifier tells MySQL to if not specified, and insert a new record in the personnel table value is automatically assigned to this field. To see the action, try using the following statement to add another record:

Example
<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user ‘root’ with no password) */
$link = mysqli_connect(“localhost”, “root”, “”, “demo”);

// Check connection
if($link === false){
die(“ERROR: Could not connect. ” . mysqli_connect_error());
}

// Attempt insert query execution
$sql = “INSERT INTO persons (first_name, last_name, email_address) VALUES (‘John’, ‘Rambo’, ‘johnrambo@mail.com’)”;
if(mysqli_query($link, $sql)){
echo “Records added successfully.”;
} else{
echo “ERROR: Could not able to execute $sql. ” . mysqli_error($link);
}

// Close connection
mysqli_close($link);
?>

Now go to the phpMyAdmin (HTTP: // localhost / phpmyadmin of /) and check the personnel data within a table in the demo database, you will see the new “PERSON_ID ‘by increasing the previous” PERSON_ID’1 value automatically distribution.

Insert Data Into a Database From an HTML Form
Let’s create an HTML form that can be used to insert new records to persons table.
Creating the HTML Form
Here’s a simple HTML form that has three text <input> fileds and a submit button.

Example

<!DOCTYPE html>
<html lang=”en”>
<head>
<meta charset=”UTF-8″>
<title>Add Record Form</title>
</head>
<body>
<form action=”insert.php” method=”post”>
<p>
<label for=”firstName”>First Name:</label>
<input type=”text” name=”firstname” id=”firstName”>
</p>
<p>
<label for=”lastName”>Last Name:</label>
<input type=”text” name=”lastname” id=”lastName”>
</p>
<p>
<label for=”emailAddress”>Email Address:</label>
<input type=”text” name=”email” id=”emailAddress”>
</p>
<input type=”submit” value=”Submit”>
</form>
</body>
</html>

Retrieve and insert form data

When the user clicks the add button recording HTML form submission, in the above example, the form data is sent to the ‘insert.php’ files. In “insert.php ‘file to connect to the MySQL database server for retrieval using PHP $ _ POST variable form field, and finally execute insert query, add the record. This is our.’ Insert.php” complete code file:

Example

<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user ‘root’ with no password) */
$link = mysqli_connect(“localhost”, “root”, “”, “demo”);

// Check connection
if($link === false){
die(“ERROR: Could not connect. ” . mysqli_connect_error());
}

// Escape user inputs for security
$first_name = mysqli_real_escape_string($link, $_POST[‘firstname’]);
$last_name = mysqli_real_escape_string($link, $_POST[‘lastname’]);
$email_address = mysqli_real_escape_string($link, $_POST[’email’]);

// attempt insert query execution
$sql = “INSERT INTO persons (first_name, last_name, email_address) VALUES (‘$first_name’, ‘$last_name’, ‘$email_address’)”;
if(mysqli_query($link, $sql)){
echo “Records added successfully.”;
} else{
echo “ERROR: Could not able to execute $sql. ” . mysqli_error($link);
}

// close connection
mysqli_close($link);
?>

Note:The mysqli_real_escape_string() function escapes special characters in a string and create a legal SQL string to provide security against SQL injection.