How can the issue of incorrect SQL syntax be addressed when attempting to delete a database entry using PHP?
To address the issue of incorrect SQL syntax when attempting to delete a database entry using PHP, you should make sure to properly format the SQL query with the correct syntax. This includes using the DELETE FROM statement followed by the table name and a WHERE clause to specify the entry to be deleted.
<?php
// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Delete entry from database
$id = 1; // example entry to delete
$sql = "DELETE FROM table_name WHERE id = $id";
if ($conn->query($sql) === TRUE) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . $conn->error;
}
$conn->close();
?>
Related questions
-
file permissions PHP scripts web server
-
PHP array array_slice
-
form submission user experience PHP applications