What are common syntax errors to watch out for when inserting data into a table using PHP?
Common syntax errors when inserting data into a table using PHP include missing quotation marks around values, incorrect variable names, and missing commas between columns. To avoid these errors, make sure to properly quote values, double-check variable names, and separate columns with commas.
<?php
// Example of inserting data into a table with correct syntax
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Insert data into table
$sql = "INSERT INTO table_name (column1, column2) VALUES ('$value1', '$value2')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>