K.Rice K.Rice - 5 months ago 15
jQuery Question

Inserting variable into mysql with php

I'm trying to insert variable, which I'm getting from my form via ajax. I can

echo
my text and I can insert hard-coded text into my table, so I know that I can get my POST and insert things with my code correctly. The problem is a type of the value of my variable, I guess.

Here is my php:

parse_str($_POST['vals'], $data);
$message = $data['text'];
echo $message;
$result = mysql_query("INSERT INTO comments SET message = $message;");


(I also tried to use
mysql_real_escape_string
function, but it just resulted empty string.)

My ajax query:

$.ajax({
type: "POST",
url: url,
data: {'vals' : $("#idForm").serialize()},
success: function(data)
{
console.log(data);
alert(data);
}
});


I'll be very thankful if someone helps me, cause I'm troubling with this so long although it looks rather simple.

Answer

You need add quote for query value. $result = mysql_query("INSERT INTO comments SET message = '$message';"); Then try to remove vals variable quote for data ajax data: {vals : $("#idForm").serialize()}, Hopefully it will help you.

Comments