I am getting this error in postman while working with a PHP REST API:
Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error inyour SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near'listing_name = 'name',address = 'test',po...' at line 2 in C:\xampp\htdocs\project\dTb\models\Listing.php:137Stack trace:
Here is my model
// Create Post public function create(){ // create query $query = 'INSERT INTO ' . $this->table . 'SET listing_name = :listing_name, address = :address, postal_code = :postal_code, google_id = :google_id, website = :website, tag_array = :tag_array, area_array = :area_array'; // Prepare Statement $stmt = $this->conn->prepare($query); // Clean Data $this->listing_name = htmlspecialchars(strip_tags($this->listing_name)); $this->address = htmlspecialchars(strip_tags($this->address)); $this->postal_code = htmlspecialchars(strip_tags($this->postal_code)); $this->google_id = htmlspecialchars(strip_tags($this->google_id)); $this->website = htmlspecialchars(strip_tags($this->website)); $this->tag_array = htmlspecialchars(strip_tags($this->tag_array)); $this->area_array = htmlspecialchars(strip_tags($this->area_array)); // Bind Data $stmt->bindParam(':listing_name', $this->listing_name); $stmt->bindParam(':address', $this->address); $stmt->bindParam(':postal_code', $this->postal_code); $stmt->bindParam(':google_id', $this->google_id); $stmt->bindParam(':website', $this->website); $stmt->bindParam(':tag_array', $this->tag_array); $stmt->bindParam(':area_array', $this->area_array); // Execute Query if($stmt->execute()) { return true; } // Print Error is Something Goes Wrong printf("Error: %s ", $stmt->error); }
and this is create.php
<?php // Headersheader('Access-Control-Allow-Origin: *');header('Access-Control-Allow-Methods: POST');header('Access-Control-Allow-Headers: Access-Control-Allow-Headers, Access-Control-Allow-Methods, Authorization, X-Requested-With');include_once '../../config/Database.php';include_once '../../models/Listing.php';// Instantiate DB and Connect$database = new Database();$db = $database->connect();// Instantiate Listing Object$listing = new Listing($db);// Get The Raw POST data$data = json_decode(file_get_contents("php://input"));$listing->listing_name = $data->listing_name;$listing->address = $data->address;$listing->postal_code = $data->postal_code;$listing->google_id = $data->google_id;$listing->website = $data->website;$listing->tag_array = $data->tag_array;$listing->area_array = $data->area_array;// Create Listingif($listing->create()){ echo json_encode( array('message' => 'Post Created') );} else { echo json_encode( array('message' => 'Listing Not Created') );}
finally this is what went into POSTMAN - POST (JSON) Body - Raw:
{"listing_name": "test name","address": "test address","postal_code": "n12 345","google_id": "googleid","website": "website","tag_array": "tagarray, array","area_array": "areaarray, array"}