I managed to get Ajax to dynamically update MySQL row results by clicking on the entry I want to update and this works. However, if I use GROUP CONCAT I can still click on the entry I want to update but the changes don't get saved. Is this normal? Or should this still work with GROUP CONCAT?
Current code that works when the mysql query doesn't use group concat:
This is the function:
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);
}
});
}
And this is 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';
}
?>
I tried updating the code as below to compensate for group concat but it did not work:
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);
}
});
}
and 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';
}
?>
Hope this clarifies.
I added the mysql query with the group concat
$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";
It would help to see an example of what exactly you are calling on your back end to make changes, but my guess is that the API Endpoint you are using is expecting a single item ID and now you are submitting multiple. In general Updates can not contain any grouping as they have to refer to specific rows, Grouping can be accomplished via sub-selects.