MySQL – PHP to INSERT Into Table

You can use PHP to INSERT data into MySQL Tables. This data can come from HTML forms, or things such as scripts that parse text documents.

phpInsert.php

<?php

$servername = "localhost";
$username = "bob";
$password = "123456";
$db = "classDB";

$ranNum = rand();
$timestamp = time();

$conn = new mysqli($servername, $username, $password, $db);

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

$sql = "insert into insert_demo(randomVar, timestamp) values ($ranNum, $timestamp)";

if ($conn->query($sql) === TRUE) {
	echo "Random Number: ".$ranNum." Timestamp: ".$timestamp;
} else {
	echo "Error: ".$sql."<br>".$conn->error;
}

$conn->close();

?>

Be the first to comment

Leave a Reply