无法获取 Ajax 以更新按 GROUP CONCAT 分组的 MySQL 行结果。有可能吗?
Cannot get Ajax to update MySQL row results grouped by GROUP CONCAT. Is it even possible?
我设法让 Ajax 通过单击我要更新的条目来动态更新 MySQL 行结果,这很有效。但是,如果我使用 GROUP CONCAT,我仍然可以单击要更新的条目,但不会保存更改。这是正常的吗?或者这仍然适用于 GROUP CONCAT 吗?
当 mysql 查询不使用组连接时有效的当前代码:
这是函数:
function edit_data(id, text, column_name)
{
$.ajax({
url:"food_edit.php",
method:"POST",
data:{id:id, text:text, column_name:column_name},
dataType:"text",
success:function(data){
//alert(data);
}
});
}
这是food_edit.php
<?php
include ("expenses.inc");
session_start();
$con = mysqli_connect("$server","$user","$pw","$db");
$id = $_POST["id"];
$text = $_POST["text"];
$column_name = $_POST["column_name"];
$query = "UPDATE food SET ".$column_name."='".$text."' WHERE id='".$id."'";
if(mysqli_query($con, $query))
{
echo 'Data Updated';
}
?>
我尝试按以下方式更新代码以补偿组连接但它不起作用:
function edit_data(ID, FOOD_TYPE, STORE_NAME, LOCATION, DATUM, COST)
{
$.ajax({
url:"food_edit.php",
method:"POST",
data:{ID:ID, FOOD_TYPE:FOOD_TYPE, STORE_NAME:STORE_NAME, LOCATION:LOCATION, DATUM:DATUM, COST:COST},
dataType:"text",
success:function(data){
//alert(data);
}
});
}
和food_edit.php:
<?php
include ("expenses.inc");
session_start();
$con = mysqli_connect("$server","$user","$pw","$db");
$id = $_POST["id"];
$FOOD_TYPE = $_POST["FOOD_TYPE"];
$STORE_NAME = $_POST["STORE_NAME"];
$LOCATION = $_POST["LOCATION"];
$DATUM = $_POST["DATUM"];
$COST = $_POST["COST"];
$query = "UPDATE food SET
".$FOOD_TYPE."='".$_POST["FOOD_TYPE"]."',
".$STORE_NAME."='".$_POST["STORE_NAME"]."',
".$LOCATION."='".$_POST["LOCATION"]."',
".$DATUM."='".$_POST["DATUM"]."',
".$COST."='".$_POST["COST"]."'
WHERE ID='".$_POST["ID"]."'";
if(mysqli_query($con, $query))
{
echo 'Data Updated';
}
?>
希望这能说明问题。
我添加了带有组 concat
的 mysql 查询
$query = "
Select
GROUP_CONCAT(ID,' <br>' ORDER BY DATUM SEPARATOR ' ') AS ID,
GROUP_CONCAT(FOOD_TYPE,' <br>' ORDER BY DATUM SEPARATOR ' ') AS FOOD_TYPE,
GROUP_CONCAT(STORE_NAME,' <br>' ORDER BY DATUM SEPARATOR ' ') AS STORE_NAME,
GROUP_CONCAT(LOCATION,' <br>' ORDER BY DATUM SEPARATOR ' ') AS LOCATION,
GROUP_CONCAT(DATUM,' <br>' ORDER BY DATUM SEPARATOR ' ') AS DATUM,
GROUP_CONCAT(COST,' <br>' ORDER BY DATUM SEPARATOR ' ') AS COST
FROM
(
SELECT food.ID, food.FOOD_TYPE, food.STORE_NAME, food.LOCATION, food.DATUM, food.COST FROM food
) TEST
group by DATUM
ORDER BY DATUM DESC";
查看您在后端调用什么来进行更改的示例会有所帮助,但我的猜测是您正在使用的 API 端点需要一个项目 ID,现在您正在提交多个。一般来说,更新不能包含任何分组,因为它们必须引用特定的行,分组可以通过子选择来完成。
我设法让 Ajax 通过单击我要更新的条目来动态更新 MySQL 行结果,这很有效。但是,如果我使用 GROUP CONCAT,我仍然可以单击要更新的条目,但不会保存更改。这是正常的吗?或者这仍然适用于 GROUP CONCAT 吗?
当 mysql 查询不使用组连接时有效的当前代码:
这是函数:
function edit_data(id, text, column_name)
{
$.ajax({
url:"food_edit.php",
method:"POST",
data:{id:id, text:text, column_name:column_name},
dataType:"text",
success:function(data){
//alert(data);
}
});
}
这是food_edit.php
<?php
include ("expenses.inc");
session_start();
$con = mysqli_connect("$server","$user","$pw","$db");
$id = $_POST["id"];
$text = $_POST["text"];
$column_name = $_POST["column_name"];
$query = "UPDATE food SET ".$column_name."='".$text."' WHERE id='".$id."'";
if(mysqli_query($con, $query))
{
echo 'Data Updated';
}
?>
我尝试按以下方式更新代码以补偿组连接但它不起作用:
function edit_data(ID, FOOD_TYPE, STORE_NAME, LOCATION, DATUM, COST)
{
$.ajax({
url:"food_edit.php",
method:"POST",
data:{ID:ID, FOOD_TYPE:FOOD_TYPE, STORE_NAME:STORE_NAME, LOCATION:LOCATION, DATUM:DATUM, COST:COST},
dataType:"text",
success:function(data){
//alert(data);
}
});
}
和food_edit.php:
<?php
include ("expenses.inc");
session_start();
$con = mysqli_connect("$server","$user","$pw","$db");
$id = $_POST["id"];
$FOOD_TYPE = $_POST["FOOD_TYPE"];
$STORE_NAME = $_POST["STORE_NAME"];
$LOCATION = $_POST["LOCATION"];
$DATUM = $_POST["DATUM"];
$COST = $_POST["COST"];
$query = "UPDATE food SET
".$FOOD_TYPE."='".$_POST["FOOD_TYPE"]."',
".$STORE_NAME."='".$_POST["STORE_NAME"]."',
".$LOCATION."='".$_POST["LOCATION"]."',
".$DATUM."='".$_POST["DATUM"]."',
".$COST."='".$_POST["COST"]."'
WHERE ID='".$_POST["ID"]."'";
if(mysqli_query($con, $query))
{
echo 'Data Updated';
}
?>
希望这能说明问题。
我添加了带有组 concat
的 mysql 查询$query = "
Select
GROUP_CONCAT(ID,' <br>' ORDER BY DATUM SEPARATOR ' ') AS ID,
GROUP_CONCAT(FOOD_TYPE,' <br>' ORDER BY DATUM SEPARATOR ' ') AS FOOD_TYPE,
GROUP_CONCAT(STORE_NAME,' <br>' ORDER BY DATUM SEPARATOR ' ') AS STORE_NAME,
GROUP_CONCAT(LOCATION,' <br>' ORDER BY DATUM SEPARATOR ' ') AS LOCATION,
GROUP_CONCAT(DATUM,' <br>' ORDER BY DATUM SEPARATOR ' ') AS DATUM,
GROUP_CONCAT(COST,' <br>' ORDER BY DATUM SEPARATOR ' ') AS COST
FROM
(
SELECT food.ID, food.FOOD_TYPE, food.STORE_NAME, food.LOCATION, food.DATUM, food.COST FROM food
) TEST
group by DATUM
ORDER BY DATUM DESC";
查看您在后端调用什么来进行更改的示例会有所帮助,但我的猜测是您正在使用的 API 端点需要一个项目 ID,现在您正在提交多个。一般来说,更新不能包含任何分组,因为它们必须引用特定的行,分组可以通过子选择来完成。