Problem when pasting using JSON

I am trying to insert data like this

function submitData() { //Posting to ContactDB with JSON format $.post("contactDB.php", JSON.stringify({ name: $("#name").val(), email: $("#email").val(), phone: $("#phone").val(), message: $("#message").val() }), function(usrava){ // if data is inserted successfully than show insert success message in Result div if(usrava=='Data Inserted') { $("#result").fadeTo(200,0.1,function(){ $(this).html('Your message is successfully saved with us. We will get back to you soon.').fadeTo(900,1); }); } //else show the error else { $("#result").fadeTo(200,0.1,function(){ $(this).html(usrava).fadeTo(900,1); }); } }); } 

ContactDB.php

 <?php $mysqli = new mysqli("localhost", "root", "", "contactDB"); //Connection to the Database //If Error than die if (mysqli_connect_errno()) { die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error()); //Echo for the response echo "Data base connection NOT Successful. Please get the assistance from your Administrator."; //Should not go out if not connected exit(); } //data received in json. decoded it to an array $data = json_decode(file_get_contents('php://input'), true); //Create a insert Command using implode as the data is already in the array $insert = "INSERT INTO contact(Name,Email,Phone,Message) VALUES ('" .implode(",",$data)."')"; $mysqli->query($insert); //Close the connection $mysqli->close(); // Echo for the response echo "Data Inserted"; ?> 

The problem is that I get an error on $mysqli->query($insert); that 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 Rajesh, at line 1 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 Rajesh, at line 1

What to do to run. I tried everything, but could not find any working solution. Please help !! thanks in advance

+4
source share
3 answers

The implode function combines the elements of an array with a string. Now your function is implode(",",$data) , which will make the line implode(",",$data) something like this

 NameData,EmailData,PhoneData,MessageData 

While you will look as if it

 'NameData','EmailData','PhoneData','MessageData' 

To do this, you need to make your implode function look like implode("','",$data) , and the first and last ' already in the code you wrote.

+1
source

There are several string values. You need to use it correctly. '

 VALUES ('" .implode("','",$data)."')" 

Explanation

The code used generates something like - VALUES ('name, email, phone, message') , which is incorrect in the syntax. They are all string and must be wrapped in ' s.

('" . implode("', '", $data) . "') - will correctly wrap them. It will generate - VALUES ('name', 'email', 'phone', 'message')

+2
source

Try the following:

 $postdata = implode("','", $data) ; $insert = "INSERT INTO contact(Name,Email,Phone,Message) VALUES ('$postdata')"; 
0
source

All Articles