K.Rice K.Rice - 1 year ago 96
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

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
function, but it just resulted empty string.)

My ajax query:

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

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

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download