Open links in new tab
  1. To insert values into a MySQL table using MySQLi in PHP, you first establish a connection to the database, then execute an INSERT INTO SQL statement. You can use either Object-Oriented or Procedural style.

    Example – MySQLi Object-Oriented:

    <?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "myDB";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);

    // Check connection
    if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
    }

    // SQL Insert Query
    $sql = "INSERT INTO MyGuests (firstname, lastname, email)
    VALUES ('John', 'Doe', '[email protected]')";

    if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
    } else {
    echo "Error: " . $sql . "<br>" . $conn->error;
    }

    $conn->close();
    ?>
    Copied!

    This connects to the database and inserts a single row into the MyGuests table.

    Example – MySQLi Procedural:

    <?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "myDB";

    // Create connection
    $conn = mysqli_connect($servername, $username, $password, $dbname);

    // Check connection
    if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
    }

    $sql = "INSERT INTO MyGuests (firstname, lastname, email)
    VALUES ('Jane', 'Smith', '[email protected]')";

    if (mysqli_query($conn, $sql)) {
    echo "New record created successfully";
    } else {
    echo "Error: " . mysqli_error($conn);
    }

    mysqli_close($conn);
    ?>
    Copied!
  1. PHP | Inserting into MySQL database - GeeksforGeeks

    Aug 26, 2024 · In this article, We will learn about How to Inserting into MySQL Database Using PHP in detail. Inserting data into a MySQL database using PHP is a fundamental operation for any web …

  2. PHP MySQL Insert Data: Step-by-Step Guide with Code Examples and …

    Learn how to insert data into a MySQL database using PHP. Includes MySQLi and PDO examples, best practices, and tips for avoiding common mistakes - studyzone4u.com.

  3. How to Insert Data Into MySQL Database Table Using PHP - Tutorial …

    • Now that you've understood how to create database and tables in MySQL. In this tutorial you will learn how to execute SQL query to insert records into a table. The INSERT INTOstatement is used to insert new rows in a database table. Let's make a SQL query using the INSERT INTO statement with appropriate values, after that we will execute this inser...
    See more on tutorialrepublic.com
  4. Insert Data into MySQL Database - Online Tutorials Library

    Data can be entered into MySQL tables by executing SQL INSERT statement through PHP function mysql_query. Below a simple example to insert a record …

  5. PHP MySQL Insert Data: Adding Records to Tables

    Sep 9, 2024 · Learn how to easily add records to your database tables using PHP and MySQL. Our step-by-step guide covers various methods to insert data …

  6. PHP MySQL Insert Data: Secure & Efficient Methods

    Inserting data into a MySQL database using PHP is an essential operation for dynamic web applications. This guide will show you how to securely insert data using both mysqli and PDO, with best practices …

  7. PHP MySQL: Insert Data Into a Table

    This tutorial shows you how to use a prepared statement to insert one or more rows into a table from a PHP script.

  8. How To Insert Data In MySQL Database Using PHP In …

    Aug 15, 2025 · This guide provides a detailed, step-by-step explanation of how to insert data into a MySQL database using PHP, particularly when working within …

  9. Complete guide to insert data into MySQL with PHP

    Aug 19, 2024 · Learn how to insert data into MySQL with PHP by following our complete guide. Discover step by step how to perform this task efficiently.