0

I'm facing this error while inserting some data to our database. My code for inserting data is below.The whole error message is:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 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 '','nmnm','Female','01/06/2017','info@example.com','9999999999')' at line 1' in /home/avfswallet/pancard.avfswallet.com/user.php:35 Stack trace: #0 /home/avfswallet/pancard.avfswallet.com/user.php(35): PDO->query('INSERT INTO use...') #1 {main} thrown in /home/avfswallet/pancard.avfswallet.com/user.php on line 35

$category=$_POST['category'];
$req_type=$_POST['requestType'];
$lname=$_POST['last_name'];
$fname=$_POST['first_name'];
$mname=$_POST['middle_name'];
$gender=$_POST['iCheck'];
$birth=$_POST['dob'];
$email=$_POST['email'];
$phone=$_POST['phone'];
echo $category."<br/>";
echo $req_type."<br/>";
echo $lname."<br/>";
echo $fname."<br/>";
echo $mname."<br/>";
echo $gender."<br/>";
echo $birth."<br/>";
echo $email."<br/>";
echo $phone."<br/>";
$query="INSERT INTO users (app_category,user_title,last_name,first_name,middle_name,gender_type,dob,email_id,phone_no) VALUES('{$category}','{$req_type}','{$lname}',{$fname}','{$mname}','{$gender}','{$birth}','{$email}','{$phone}')";
mujuonly
  • 11,370
  • 5
  • 45
  • 75
mofidul
  • 119
  • 3
  • 11
  • 1
    This is a syntax error in query. Echo your `$query`. – Michael Jun 10 '17 at 09:36
  • I think there might be a chance for unsanitized values in the input. Don't ever directly insert values from $_POST .read about SQL injection – Shobi Jun 10 '17 at 09:39
  • Even with fixing this typo, you would experience problems with inserting names like `O'Brian`, try it. Use prepared statements with placeholders! – Qirel Jun 10 '17 at 09:50

1 Answers1

3

You should be using prepared statements with binds, but you mistake is a missing ' before fname...

$query="INSERT INTO users (app_category,user_title,last_name,first_name,middle_name,gender_type,dob,email_id,phone_no) VALUES('{$category}','{$req_type}','{$lname}','{$fname}','{$mname}','{$gender}','{$birth}','{$email}','{$phone}')";
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55