PHP MySQL: Insert Record

This entry is part 6 of 17 in the series PHP & MySQL

We use similar function mysqli_query() to insert records to a database table.  Here we insert 3 records into the table allartist.


$mysqli = new mysqli("localhost", "root", "", "allartist");
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL";

$sql = "INSERT INTO artist
(artiste, album, year)
('Rod Stewart', 'Maggie May', 1973),
('New Order', 'Substance', 1987),
('Nirvana', 'Smell Like Teen Spirit', 1992)";

if (mysqli_query($mysqli, $sql)) {
    echo "Data inserted successfully";
} else {
    echo "Error inserting data into table with error no: " . $mysqli->errno;


After running the script and if it is successful, we should see a message as shown in the screenshot below.


Using Toad, we can display the inserted data using the query

select * from artist

inserted data

Series Navigation<< PHP MySQL: Create Table
PHP MySQL: Delete >>

Leave a comment

Leave a Reply