How to add multiple data to a MySQL database. PHP function e... open

How to add multiple data to a MySQL database. PHP function example.

Approved. Code works!
This is exactly the working code that is verified by the moderator or site administrators
Tested: PHP 5.6 +

For example, we have added a users table with three columns id, name, age with the following definition:

CREATE TABLE Users (id INTEGER AUTO_INCREMENT PRIMARY KEY, name VARCHAR(30), age INTEGER)

The INSERT command is used to add data to MySQL

//connect to database
$conn = new mysqli("localhost", "root", "mypassword", "testdb");

//check an errors
if($conn->connect_error){
    die("error: " . $conn->connect_error);//return errors
}

//write the data in the varriable $sql
$sql = "INSERT INTO Users (name, age) VALUES 
            ('Sam', 41), 
            ('Bob', 29), 
            ('Alice', 32)
            ";

//write users to the database and display errors
if($conn->query($sql)){
    echo "Users added";
} else{
    echo "Error: " . $conn->error;
}

$conn->close();
0

More

Leave a Reply

Your email address will not be published. Required fields are marked *

How many?: 22 + 22

lil-code© | 2022 - 2024
Go Top
Authorization
*
*
Registration
*
*
*
*
Password generation