Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to insert multiple records in Database?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 105
    Comment on it

    We can insert multiple records in database using PHP. To do this, suppose we have database *example* having table demo with columns firstname, lastname, email. We need to insert multiple record in the table by using PHP. To insert the multiple records in the table:

    <?php
    $hostname = "localhost";
    $username = "username";
    $password = "password";
    $dbname = "example";
    
    // Create connection
    $conn = new mysqli($hostname, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 
    
    $sql = "INSERT INTO demo (firstname, lastname, email)
    VALUES ('fname1', 'lname1', 'ex1@example.com');";
    $sql .= "INSERT INTO demo (firstname, lastname, email)
    VALUES ('fname2', 'lname2', 'ex2@example.com');";
    $sql .= "INSERT INTO demo (firstname, lastname, email)
    VALUES ('fname3', 'lname3', 'ex3@example.com')";
    
    if ($conn->multi_query($sql) === TRUE) {
        echo "New records created successfully";
    } else {
        echo "Error: " . $sql . "
    " . $conn->error; } $conn->close(); ?>

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: