dimanche 24 juillet 2016

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.

Aucun commentaire:

Enregistrer un commentaire