我正在使用Jquery数组来获取动态输入表单上的变量。我可以将数组分组并输出到console.log中,但是不能在多表数据库本身中插入任何数据。
这是带有ajax和console.log的Jquery代码
$("button[name='submit_addPackresCategories']").click(function(){
var packres_categories_name;
$("input[id='packres_categories_name']").each(function(){
packres_categories_name = this.value;
});
console.log(packres_categories_name);
var packres_categories_description;
$("textarea[id='packres_categories_description']").each(function(){
packres_categories_description = this.value;
});
console.log(packres_categories_description);
var packres_categories_paxhead_day = {};
$("input[id^='packres_categories_paxhead_day']").each(function(){
packres_categories_paxhead_day[this.id] = this.value;
});
console.log(packres_categories_paxhead_day);
var packres_categories_paxhead_price = {};
$("input[id^='packres_categories_paxhead_price']").each(function(){
packres_categories_paxhead_price[this.id] = this.value;
});
console.log(packres_categories_paxhead_price);
var packres_categories_paxgroup_size = {};
$("input[id^='packres_categories_paxgroup_size']").each(function(){
packres_categories_paxgroup_size[this.id] = this.value;
});
console.log(packres_categories_paxgroup_size);
var packres_categories_paxgroup_consumable = {};
$("input[id^='packres_categories_paxgroup_consumable']").each(function(){
packres_categories_paxgroup_consumable[this.id] = this.value;
});
console.log(packres_categories_paxgroup_consumable);
$.ajax({
url: "ajax/addPackresCategories.php",
type: "post",
data: {
packres_categories_name: packres_categories_name,
packres_categories_description: packres_categories_description,
packres_categories_paxhead_day: packres_categories_paxhead_day,
packres_categories_paxhead_price: packres_categories_paxhead_price,
packres_categories_paxgroup_size: packres_categories_paxgroup_size,
packres_categories_paxgroup_consumable: packres_categories_paxgroup_consumable
}
})
.done(function(response){
if (JSON.parse(response)) {
alert("Successfully Added New Package!");
}
else {
alert("Failed!");
}
});
});
这是ajax的网址
<?php
include_once("../db.php");
include "admin_session.php";
$packres_categories_name = $_POST["packres_categories_name"];
$packres_categories_description = $_POST["packres_categories_description"];
$packres_categories_paxhead = array_combine($_POST["packres_categories_paxhead_day"], $_POST["packres_categories_paxhead_price"]);
$packres_categories_paxgroup = array_combine($_POST["packres_categories_paxgroup_size"], $_POST["packres_categories_paxgroup_consumable"])
$sql_insertPackresNameDesc_packresCategories = "INSERT INTO packres_categories(packres_categories_name, packres_categories_description) VALUES('" . mysqli_real_escape_string($conn, $packres_categories_name) . "', '" . mysqli_real_escape_string($conn, $packres_categories_description) . "')";
$query_sql_insertPackresNameDesc_packresCategories = $conn->query($sql_insertPackresNameDesc_packresCategories);
$affectedRows_query_sql_insertPackresNameDesc_packresCategories = mysqli_affected_rows($conn); //will be used to verify if inserted properly on database later
$packres_id = mysqli_insert_id($conn);//will be used later
foreach ($packres_categories_paxhead as $key => $value) {
$sql_insertPackresCategoriesPaxhead_packresCategoriesPaxHead = "INSERT INTO packres_categories_paxhead(packres_id, packres_categories_paxhead_day, packres_categories_paxhead_day) VALUES('" . mysqli_real_escape_string($conn, $packres_id) . "', '" . mysqli_real_escape_string($conn, $key) . "', '" . mysqli_real_escape_string($conn, $value) . "')";
$query_sql_insertPackresCategoriesPaxhead_packresCategoriesPaxHead = $conn->query($insertPackresCategoriesPaxhead_packresCategoriesPaxHead);
$affectedRows_query_sql_insertPackresCategoriesPaxhead_packresCategoriesPaxHead += mysqli_affected_rows($conn);
}
foreach ($packres_categories_paxgroup as $key => $value) {
$sql_insertPackresCategoriesPaxGroup_packresCategoriesPaxgroup = "INSERT INTO packres_categories_paxgroup(packres_id, packres_categories_paxgroup_size, packres_categories_paxgroup_consumable) VALUES('" . mysqli_real_escape_string($conn, $packres_id) . "', '" . mysqli_real_escape_string($conn, $key) . "', '" . mysqli_real_escape_string($conn, $value) . "')";
$query_sql_insertPackresCategoriesPaxGroup_packresCategoriesPaxgroup = $conn->query($sql_insertPackresCategoriesPaxGroup_packresCategoriesPaxgroup);
$affectedRows_query_sql_insertPackresCategoriesPaxGroup_packresCategoriesPaxgroup += mysqli_affected_rows($conn);
}
$conn->close();
if (($affectedRows_query_sql_insertPackresNameDesc_packresCategories != 0) && ($affectedRows_query_sql_insertPackresCategoriesPaxhead_packresCategoriesPaxHead != 0) && ($$affectedRows_query_sql_insertPackresCategoriesPaxGroup_packresCategoriesPaxgroup != 0)) {
echo json_encode(true);
}
else {
echo json_encode(false);
}
?>
应将数据数组正确插入数据库中,但没有数据进入数据库。
在控制台上抛出错误行
Uncaught SyntaxError: Unexpected token < in JSON at position 0
at JSON.parse (<anonymous>)
at Object.<anonymous> (master.js:70)
at u (jquery.min.js:2)
at Object.fireWith [as resolveWith] (jquery.min.js:2)
at k (jquery.min.js:2)
at XMLHttpRequest.<anonymous> (jquery.min.js:2)
答案 0 :(得分:0)
json_encode()-返回值的JSON表示形式。 而且您正在返回布尔值