So, I'm using prepared statements from PHP to insert an entry from my text field into my database, and when using this prepared statement, the data does get inserted successfully, however, I get this error message:
Error: INSERT INTO posts (firstname, lastname, body, date_posted) VALUES (?, ?, ?, NOW())
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '?, ?, ?, NOW())' at line 1 -->
Any clue on what's occuring? Thanks for the help. PHP code:
<?php
session_start();
// Making Connection To The Database
$dbHost = "localhost";
$dbUser = "root";
$dbPass = "";
$database = "signup";
$connection = mysqli_connect($dbHost, $dbUser, $dbPass, $database) or die ("Sorry, we could not connect to the database");
// Posting System
if (!empty($_POST['postContent'])) {
$post = $_POST['postContent'];
$firstname = $_SESSION['firstname'];
$lastname = $_SESSION['lastname'];
$sql = "INSERT INTO posts (firstname, lastname, body, date_posted) VALUES (?, ?, ?, NOW())";
$stmt = mysqli_stmt_init($connection);
// nested if statement
if (!mysqli_stmt_prepare($stmt, $sql)) {
echo "";
} else {
mysqli_stmt_bind_param($stmt, "sss", $firstname, $lastname, $post);
mysqli_stmt_execute($stmt);
}
} else {
echo "";
}
if (mysqli_query($connection, $sql)) {
echo "";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($connection);
}
?>