I have this script:
var wall_user_id = "<?php echo $_SESSION['user_id'];?>";
$.ajax({
type: "POST",
url: "insert.php",
data: "user_id=" + wall_user_id,
success: function () {
$('ul#posts').prepend(wall_post);
}
});//end success function
that send wall_user_id to the php script:
<?php
require 'core/functions/init.php';
$user_id = mysql_real_escape_string($_POST['user_id']);
$sql = 'INSERT INTO wall (user_id) VALUES('.$user_id.')';
mysql_query($sql);
?>
this works fine but it doesn’t if I try to add more values like this:
var wall_user_id = "<?php echo $_SESSION['user_id'];?>";
var wall_userimage = "<?php echo $user_data['imagename']; ?>";
$.ajax({
type: "POST",
url: "insert.php",
data: "user_id=" + wall_user_id +"userimage="+ wall_userimage,
success: function () {
$('ul#posts').prepend(wall_post);
}
});//end success function
and insert.php:
<?php
require 'core/functions/init.php';
$user_id = mysql_real_escape_string($_POST['user_id']);
$userimage = mysql_real_escape_string($_POST['userimage']);
$sql = 'INSERT INTO wall (user_id, image) VALUES('.$user_id.','.$userimage.')';
mysql_query($sql);
?>
it doesn’t inserts any values into the database.
what am I doing wrong?
You could pass as many values as you want in the
data:and in your example use
&to separate parameters:But the first method is the recommended way because jQuery takes care of properly url encoding the values. With your example if you wanted to do it properly you should have written:
which obviously is way too much code for this. So use the first approach.