I am using a call to AJAX to update a value in a table which Im then using in a query locate data in the table for output. The data is updating in the table as required, but not in query output after the AJAX updates the table.

Here is my work:

AJAX on index.php

<script type="text/javascript">
$(function() {
$(".accept").click(function(){
var element = $(this);
var del_id = $(this).attr('id');
var info = 'id=' + del_id;

 $.ajax({
   type: "POST",
   url: "accept.php",
   data: info,
   success: function(){}

});
  $(this).parents(".show").animate({ backgroundColor: "#003" }, "slow")
  .animate({ opacity: "hide" }, "slow");

});
});
</script>

It sends data to accept.php, which uses the data to update the table:

accept.php

include('db.php');
$id = $_POST['id'] ;
$name = $_POST['order_id'] ;
$sql = "UPDATE mgap_ska SET mgap_status = '1' WHERE mgap_ska_id = '".$id."' AND mgap_ska_report_category = '".$name."';"; 
$stmt = $pdo->prepare($sql); 

$stmt->execute(array(
':id' => '$id',
':name' => '$name'
));

and then I query the table for the updated info:

"SELECT SUM(mgap_ska_growth) AS total FROM mgap_ska WHERE mgap_status = 1 ";

'mgap_ska_growth' is a column full of dollar values and all related 'mgap_status' values are set to '0' by default. The AJAX updates a row value from '0' to '1' and the query locates all values with mgap_status of '1' and SUMs there totals.

The SUM is working, but only once upon entering the site. The query output doesn't update after the AJAX runs even though the values in the table are uodating as require.

What am I missing?

没有正确的解决方案

其他提示

You didn't set order_id in javascript.

$(function() {
$(".accept").click(function(){
var element = $(this);
var del_id = $(this).attr('id');
var order_id  = //something collect order_id
var info = 'id=' + del_id + '&order_id=' + /*order_id value*/;

 $.ajax({
   type: "POST",
   url: "accept.php",
   data: info,
   success: function(){}

});
  $(this).parents(".show").animate({ backgroundColor: "#003" }, "slow")
  .animate({ opacity: "hide" }, "slow");

});
});

because you used AND in your query and you didn't set $_POST['order_id'], and then $name, your query will always return false...

and I think you should recheck this part:

$sql = "UPDATE mgap_ska SET mgap_status = '1' WHERE mgap_ska_id = '".$id."' AND mgap_ska_report_category = '".$name."';"; 
$stmt = $pdo->prepare($sql); 

$stmt->execute(array(
':id' => '$id',
':name' => '$name'
));

try this type :

$sql = "UPDATE mgap_ska SET mgap_status = '1' WHERE mgap_ska_id = :id AND mgap_ska_report_category = :name;"; 
$stmt = $pdo->prepare($sql); 

$stmt->execute(array(
':id' => $id,
':name' => $name
));
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top