I’m writing a fairly basic commenting function for a site using post method to pass input values to a php file that performs the relevant mysql query.
The $.ajax part is as follows:
// AJAX URL ENCODING
$("#add").click(function() {
// POST COMMENT FORM VALUES
var ajaxOpts = {
type: "post",
url: "includes/addComment.php",
data: "&author=" + $("#author").find("input").val() + "&email=" + $("#email").find("input").val() + "&comment=" + $("#leaveComment").find("textarea").val() + "&parent=<?php echo $vid_id; ?>",
success: function(data) {
// IMMEDIATE DISPLAY
// Not relevant to this problem.
// CLEAR TEXTAREA FOR NEXT COMMENT
$("textarea").val('');
}
};
$.ajax(ajaxOpts);
});
This is passed through to addcomment.php:
require('connection.php');
//get POST data
$name = mysql_real_escape_string($_POST["author"]);
$email = strtolower(md5(trim(mysql_real_escape_string($_POST["email"]))));
$comment = mysql_real_escape_string($_POST["comment"]);
$time = Date ('c');
$parent = trim(mysql_real_escape_string($_POST["parent"]));
//add new comment to database
mysql_query("INSERT INTO comments VALUES(' $name ',' $email ',' $comment ', ' $time ', ' $parent ')");
Everything works fine except that the mysql_query doesn’t end up inserting any values into my database. After a bit of digging I found this:
So i’m assuming my query isn’t going ahead because the ‘ : ‘ bit of junk(?) data is throwing everything out?
I’m trying to figure out where this errant ‘ : ‘ has come from, anyone have any ideas?
P.S – Apologies for the length of this question, I’ve tried to narrow down possible culprits as much as I can.
what happens if you remove the & from the front of &author?
normally the first data field doesn’t have an &
also as others have mentioned this would be a welcome addition
and its usually good practice to name your database columns on the insert
otherwise you risk breaking the insert statement if you ever alter the table structure