MySQLi Insert Using PHP
The INSERT INTO statement is used to add new records to a database table. The insert statement is used to add new records to a database table. each time a new record is to be added we use INSERT INTO statement for that purpose. There are two ways of inserting records either explicitly providing the column name with values respectively or simply by providing values of the table but doesn't specify the column name.
Syntax rules to follow:
- The SQL query must be quoted in PHP
- String values inside the SQL query must be quoted
- Numeric values must not be quoted
- The word NULL must not be quoted
Syntax
The INSERT INTO statement is used to add new records to a MySQLi table:
INSERT INTO table_name (column1, column2, column3,...)VALUES (value1, value2, value3,...)
PHP MySQLii Insert Record Example
<?php
$host = "localhost";
$user = "";
$pass = "";
$dbname = "users";
$conn = mysqli_connect($host,$user,$pass,$dbname) or die(mysqli_error());
$sql = "INSERT INTO users(name,salary) VALUES ('suhail','90000')";
if(mysqli_query($conn,$sql)){
echo "Record inserted successfully";
}else{
echo "Could not insert record: ". mysqli_error();
}
mysqli_close();
?>
Output:
Record inserted successfully
Series:
MySql Tutorials Hub
