jquery传递数组并在不同的变量中存储不同的索引

时间:2018-02-19 07:20:00

标签: php jquery mysql

enter image description here我在数组中存储标签值,使用ajax将此数组传递给php。现在我想将每个索引值存储在不同的变量中,将这些值存储在mysql表的一个字段



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文件:

<?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);
?>

但问题是,说不通过。表显示空白字段。我不知道我做错了什么。我还没有找到任何解决方案。

0 个答案:

没有答案