0

-I created an HTML form and a PHP code that sends HTML form responses to my SQL database. but the PHP code is connecting to the MySQL database (on WAMP server). When I try to send data from submitting the HTML form, it redirects to the PHP file and there is an error. This is the error.

Connected successfully
Error: INSERT INTO MyGuests (firstname, lastname, email) VALUES (Senath, Sethmika, senath@example.com)
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 '@example.com)' at line 2



<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mydb";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);

// Check connection
if (!$conn) {
  die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully <br>";
$name = $_POST["fname"];
$lname = $_POST["lname"];
$msg = $_POST["msg"];
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ($name, $lname, $msg)";

if ($conn->query($sql) === TRUE) {
  echo "New record created successfully";
} else {
  echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>

I tried by using strtr() function and restarting the WAMP server but they didn't work.

0 Answers0