PHP MySQL Insert Data

Insert Data Into MySQL Using MySQLi

After making a database and a table, we can start putting information in them.

Here are some rules about how to use language:

PHP needs to quote the SQL query.
Quotes must be used around string values in the SQL query.
Numeric values must not be quoted
The word NULL can’t be used in a quote.

Adding new records to a MySQL table is done with the INSERT INTO statement:

INSERT INTO table_name (column1, column2, column3,…)
VALUES (value1, value2, value3,…)

Visit our SQL tutorial to learn more about SQL.

In the last chapter, we made a table called “Student” that was empty and had four columns: “id,” “firstname,” “lastname,” and “email.” Now, let’s put the information into the table.

Example

<?php
$servername = “localhost”;
$username = “username”;
$password = “password”;
$dbname = “school”;

// Create connection
$link = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$link) {
die(“Connection failed: ” . mysqli_connect_error());
}

$sql = “INSERT INTO student (firstname, lastname, email)
VALUES (‘ram’, ‘lal’, ‘ramlal@example.com’)”;

if (mysqli_query($link, $sql)) {
echo “New record insert successfully”;
} else {
echo “Error: ” . $sql . “<br>” . mysqli_error($link);
}

mysqli_close($link);
?>

People also search
Scroll to Top