i have store label values in array, pass this array using ajax to php. now i want to store each index value in different variables the store these value under one field of mysql table
function rs() {
var arr = [];
$('.cat').each(function() {
arr.push($(this).text());
});
$.ajax({
url: 'insert.php',
data: {
array: arr
},
type: 'POST',
success: function() {
alert("data has been sent");
document.getElementById('exampleModal1').style.display = "none";
}
});
}
php file :
<?php
$con = mysqli_connect("localhost", "root", "", "test");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: ".mysqli_connect_error();
}
// escape variables for security
//home tab
$cat1 = mysqli_real_escape_string($con, $_POST['array[0]']);
$cat2 = mysqli_real_escape_string($con, $_POST['array[1]']);
$cat3 = mysqli_real_escape_string($con, $_POST['array[2]']);
$cat4 = mysqli_real_escape_string($con, $_POST['array[3]']);
$cat5 = mysqli_real_escape_string($con, $_POST['array[4]']);
$sql1 = "INSERT INTO rs (Category)
VALUES('".$cat1."'), ('".$cat2."'), ('".$cat3."'), ('".$cat4."'), ('".$cat5."')
";
if (!mysqli_query($con, $sql1)) {
die('Error: '.mysqli_error($con));
}
echo "1 record added";
mysqli_close($con);
?>
but the problem is that , the said is not passed. table shows blank fields. i do not know what i am doing wrong. i did not find any solution yet.
"; } this add value but only last value – Sarah_Salar Feb 19 '18 at 09:57
"; } can anyone help? this only adds last value of the array into the field. $sql .="" does not help – Sarah_Salar Feb 20 '18 at 07:34