我正在发送ajax请求。到一个php页面,其中我使用while循环来获取所有数据,我想将数据减少1并更新exixing数据并插入到新表中,我只希望执行一次此操作,但是问题是更新和插入数据的次数更多。 / p>
$("#create_button").click(function(){
if (! confirm('Do You Want To Create Product?')) { return false; preventDefault(); }else{
$.ajax({
type :'POST',
url : 'response.php', //Here you will fetch records
//dataType:'json',
data :{'select_id':select_id},
success:function(response){
//alert(response);
}
});
}
});
}
in response.php
<?php
include_once("../database/db.php");
if(isset($_POST["select_id"]))
{
$select_id=$_POST["select_id"];
$get_product=$con1->query("SELECT * FROM `components` WHERE
FIND_IN_SET($select_id,product_id)");
$i=1;
while($row=$get_product->fetch(PDO::FETCH_ASSOC)){
$component_id=$row['id'];
$component_qty=$row['quantity_remain'];
$sql = "UPDATE components SET quantity_remain=? WHERE id=?" ;
$con1->prepare($sql)->execute([$component_qty - 1,$component_id]);
$i++;
}
$sql = "INSERT INTO created_products (product_id)
VALUES (?)";
$con1->prepare($sql)->execute([$select_id]);
unset($_POST["select_id"]);
}
?>