Ajax 无法正常工作。 PHP 无法在 SQL 中插入数据
Posted
技术标签:
【中文标题】Ajax 无法正常工作。 PHP 无法在 SQL 中插入数据【英文标题】:Ajax not working properly. PHP cannot insert data in SQL 【发布时间】:2019-11-26 01:17:37 【问题描述】:我正在使用 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)
【问题讨论】:
将对此进行调查。谢谢你:) 【参考方案1】:json_encode()- 返回值的 JSON 表示。 你正在返回布尔值
【讨论】:
以上是关于Ajax 无法正常工作。 PHP 无法在 SQL 中插入数据的主要内容,如果未能解决你的问题,请参考以下文章