Monday 3 September 2018

MySQL Error: & ldquo; an error in your SQL syntax; check the manual corresponding to your version of the MySQL server for the correct syntax & rdquo;

I am getting this error from MySQL:

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 'Details
(title, first, last, NRIC, po' at line 1

Here is the code:
<?php
  $link = mysql_connect("localhost", "root", "");
  if (!$link) { die('Could not connect: ' . mysql_error()); }

  $db_selected = mysql_select_db(Membership, $link);
  if (!$db_selected) { die('Can\'t use' . Membership . ':' . mysql_error()); }

  $value1 = $_POST["title"];
  $value2 = $_POST["first"];
  $value3 = $_POST["last"];
  $value4 = $_POST["NRIC"];
  $value5 = $_POST["birthdate"];
  $value6 = $_POST["birthmonth"];
  $value7 = $_POST["birthyear"];
  $value8 = $_POST["address"];
  $value9 = $_POST["postal"];
  $value10 = $_POST["genderSelect"];
  $value11 = $_POST["contact"];
  $value12 = $_POST["email"];
  $value13 = $_POST["enter"];
  $value14 = $_POST["password"];
  $value15 = $_POST["Updates"];
  $value16 = $_POST["Terms"];
  $value17 = $_POST["submit_but"];
  $value18 = $_POST["status"];

  $sql = "INSERT INTO Member Details (title, first, last, NRIC, birthdate, birthmonth, birthyear, address, postal, genderSelect, contact, email, enter, password, Updates, Terms, submit_but, status) VALUES ('$value', '$value2', '$value3', '$value4', '$value5', '$value6', '$value7', '$value8', '$value9', '$value10', '$value11', '$value12', '$value13', '$value14', '$value15', '$value16', '$value17', '$value18')";

  if (!mysql_query($sql)){     //The error is thrown here
   die('Error: ' . mysql_error());
  }
  mysql_close();
?>


Your problem lies here:
INSERT INTO Member Details ...

Because you have a space there, it thinks Member is the table name and Details is extraneous, hence the error.
If your table is Member Details, you need to enclose it within back ticks:
INSERT INTO `Member Details` ...


I'm actually not that big a fan of spaces in SQL table names (or filesystem file names for that matter). In this particular case, I believe MemberDetails (or Member_Detailsmember_details and probably others) is just as readable, without requiring the use of backticks scattered throughout your code.

0 comments:

Post a Comment