insert_into_table.php
<?php
//Config
$server = "localhost";
$user = "root";
$password = "";
$database = "address_book";
//Establishing a Connection to MySQL Server
$connection = mysqli_connect($server, $user, $password, $database);
//Check Connection
if (!$connection) {
die("<h2>Total Fail</h2> " . mysqli_connect_error());
} else {
echo "Connection Successfull <br>";
}
//SQL Command
$sql_command = "INSERT INTO people (id, name, lastname, telephon, email, address)
VALUES (NULL, 'Samantha', 'Fox', 555444, 'mail@server.com', 'Main Road 12a')";
//Check SQL Command
if (mysqli_query($connection, $sql_command)) {
echo "SQL Command OK";
} else {
echo "SQL ERROR" . mysqli_error($connection);
}
?>
This PHP command will execute an SQL INSERT statement to insert a new row into the 'people' table of a MySQL database. The values for each column are provided in the VALUES clause:
- id: Since it is set to NULL, the auto-increment feature of the 'id' column will automatically generate a new unique value for this row.
- name: The value 'Samantha' will be inserted into the 'name' column.
- lastname: The value 'Fox' will be inserted into the 'lastname' column.
- telephon: The value 555444 will be inserted into the 'telephon' column.
- email: The value 'mail@server.com' will be inserted into the 'email' column.
- address: The value 'Main Road 12a' will be inserted into the 'address' column.
If the INSERT statement executes successfully, the code will output the message "SQL Command OK". Otherwise, if there is an error, the code will output "SQL ERROR" followed by the error message returned by MySQL.
After First Run, One Row is in Table. You can run it multiple times.
Connection Successfull
SQL Command OK
No comments:
Post a Comment