Query gives error and cannot see why

OK. I am trying to see what is wrong with the following setup.

  $pdo1 = get_db_connection();
   if ($pdo1)
	// echo 'Connected to database OK.';
	echo 'Cannot connect to DB<br>';
	$temp = "";

 	$STM = $pdo1->prepare("INSERT INTO Meeting_Places ('MeetingPlace', 'MeetingAddress', 'MeetingCity', 'MeetingState', 'MeetingZip', 'Place_Latitude', 'Place_Longitude', 'MeetingPlaceLogo') VALUES (:MeetingPlace, :MeetingAddress, :MeetingCity, :MeetingState,:MeetingZip, :Lat, :Lng, :MeetingPlaceLogo)"); 

 	$STM->bindValue(':MeetingPlace', $_POST['element_4_1']);
	$STM->bindValue(':MeetingAddress', $_POST['element_5_1']);
	$STM->bindValue(':MeetingCity', $_POST['element_5_3']);
	$STM->bindValue(':MeetingState', $_POST['element_5_4']);
	$STM->bindValue(':MeetingZip', $_POST['element_5_5']);
	$STM->bindValue(':Lat', $lat);
	$STM->bindValue(':Lng', $lng);
	$STM->bindValue(':MeetingPlaceLogo', $temp);	 

The above generates the following error:
( ! ) 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 ‘‘MeetingPlace’, ‘MeetingAddress’, ‘MeetingCity’, ‘MeetingState’, ‘MeetingZip’, ‘’ at line 1’ in C:\wamp\www\SACCC567\AddNewMeetingPlace20.php on line 167

Line 167 is the execute() line.


The MySQL error tells you what you need to know. Basically…

The problem you have is that you dont put apostrophes (') around field names. You put backticks (`) instead.


try {
	$db_options = array(
		/* important! use actual prepared statements (default: emulate prepared statements) */
		/* throw exceptions on errors (default: stay silent)                                */
		/* fetch associative arrays (default: mixed arrays)                                 */
		PDO::ATTR_EMULATE_PREPARES => false                     

	$pdo = new PDO('mysql:host=localhost;dbname=safabc;charset=utf8', 'your_username', 'your_password', $db_options);	

} catch (PDOException $e) { // Report the Error!
	/* You never want to use the following on a production website */
	$status_message = "<p>Something is not right, check your php.ini settings or code</p>";

$temp = "";

/* Make a query string for better flow of the code */
$query = "INSERT INTO Meeting_Places 
  ('MeetingPlace', 'MeetingAddress', 'MeetingCity', 'MeetingState', 'MeetingZip', 'Place_Latitude', 'Place_Longitude', 'MeetingPlaceLogo') 
  (:MeetingPlace, :MeetingAddress, :MeetingCity, :MeetingState,:MeetingZip, :Lat, :Lng, :MeetingPlaceLogo)";

/* Prepare the Statement */
$stmt = $pdo->prepare($query);  

/* Execute the Query with the Prepared Statements */
$result = $stmt->execute( 
	array(':MeetingPlace' => $_POST['element_4_1'],
		':MeetingAddress' => $_POST['element_5_1'],
		':MeetingCity' => $_POST['element_5_3'],
		':MeetingState' => $_POST['element_5_4'],
		':MeetingZip' => $_POST['element_5_5'],
		':Lat' => $lat,
		':Lng' => $lng,
		':MeetingPlaceLogo' => $temp

/* Display the result of the Execution of the Query */
if ($result) {
	$status_message = "Data Successfully inserted";
} else {
	$status_message = "Insertion Failed";

Maybe the above might help you out?

not if you still dont fix the not-apostrophes-around-field-names problem :stuck_out_tongue_winking_eye:

You only have to use backticks if you want to use reserved words as column names. Otherwise you can just use the column names without putting anything around them.

I always avoid having to use backticks as they aren’t on my keyboard.

Correct. I should have said… IF you’re going to put something around your field names, it must be a backtick, rather than an apostrophe.