Home > AI > Language > PHP > mysqli >

insert one record

Working code:

<?php

$db_host = "";
$db_user = "";
$db_password = "";
$db_dbname = "";


// Create connection
$con = new mysqli($db_host, $db_user, $db_password, $db_dbname);
// Check connection
if ($con->connect_error) {
    die("Connection failed: " . $con->connect_error);
}




// sql to create table
$sql = "CREATE TABLE MyGuests (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)";

if ($con->query($sql) === TRUE) {
    echo "Table MyGuests created successfully";
} else {
    echo "Error creating table: " . $con->error;
}


// insert 
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com')";

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




$con->close();

Leave a Reply