My PHP form is designed to insert data to a MySQL database. It's working great, except when there is an apostrophe in my input. Then, I get this error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'fdfdf','fdfdf')' at line 2
This is my first project using PHP. Can someone please provide sample code for how to escape the apostrophe?
<html>
<body>
<form action="http://thefaithperspective.com/verses">
<input type="submit" value="Add Another" />
</form>
</body>
</html>
<?php
$servername = "...";
$username = "...";
$password = "...";
$dbname = "...";
$verse= $_POST['verse'];
$book= $_POST['book'];
$reference = $_POST['reference'];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO myverses (versetext, book, reference)
VALUES ('$verse','$book','$reference')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>