PHP Insertion Into Table
PHP Insertion Into Table:– 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 INTO statement 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 insert query through passing it to the PHP mysqli_query() function to insert data in table. Here’s an example, which insert a new row to the persons table by specifying values for the first_name, last_name and email fields.
Inserting Rows And Column Values Into Table
The INSERT INTO statement is used to add new records to a MySQL table.The following examples add a new record to the “JohnData” table.
Example // How to create a database table
<?php $servername = "localhost"; $username = "root"; $password = ""; $dbname = "employee"; // Create connection $conn = new mysqli($servername, $username, $password,$dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = "INSERT INTO JohnData (firstname, lastname, email) VALUES ('John', 'Doe', 'john@example.com')"; if ($conn->query($sql) === TRUE) { echo "New record added successfully into the table"; } else { echo "Error: " . $sql . "<br>" . $conn->error; } $conn->close(); ?> |
Advertisements