0

I was trying to use basic php to insert data into a mysql table from an html form. i was succesfull initially with basic plain text inputs. However when i was trying to input an email id i keep getting this error :

Could not enter data: 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 '@gmail.com,moohawks )' at line 3

My php code is as follows:

$firstname = $_REQUEST['firstname'];
    $lastname = $_REQUEST['lastname'];
    $emailid = $_REQUEST['emailid'];
    $team_name = $_REQUEST['team_name'];
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'incredible';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
$sql = "INSERT INTO users 
       (firstname,lastname,emailid,teamname)
       VALUES ( $firstname,$lastname,$emailid,$team_name )";

mysql_select_db('adb project');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not enter data: ' . mysql_error());
}
$message2 =  "Entered data successfully\n";
mysql_close($conn);
?>

What is wrong with my insert statement. As far I can understand, it looks corrects and fairly simple. Any help?

1 Answer 1

2

Correct your SQL statement as follows with a single quote for every variable

$sql = "INSERT INTO users 
       (firstname,lastname,emailid,teamname)
       VALUES ( '$firstname','$lastname','$emailid','$team_name' )";
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.