how to insert php variable in mysql database code example
Example: php mysql insert variables into database
<?php
$mysqli = new mysqli("localhost", "root", "", "demo");
if($mysqli === false){
die("ERROR: Could not connect. " . $mysqli->connect_error);
}
$first_name = $mysqli->real_escape_string($_REQUEST['first_name']);
$last_name = $mysqli->real_escape_string($_REQUEST['last_name']);
$email = $mysqli->real_escape_string($_REQUEST['email']);
$sql = "INSERT INTO persons (first_name, last_name, email) VALUES ('$first_name', '$last_name', '$email')";
if($mysqli->query($sql) === true){
echo "Records inserted successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . $mysqli->error;
}
$mysqli->close();
?>