我使用以下代码中的while循环来输入表格。
问题是它总是两次进入最后一次记录。
例如,如果我输入3条记录,输入的记录将是:
始终输入最后一条记录两次。我不明白这是什么问题。 请.....
if(isset($_POST['credit'])){
if($_POST['customer'] == ""){
echo "Please Choose a Customer";
}else{
$sql = "SELECT * FROM products WHERE id IN (";
foreach($_SESSION['cart'] as $id => $value){
$sql .=$id . ",";
}
$sql = substr($sql,0,-1).") ORDER BY name ASC";
$query = mysql_query($sql);
$profit = 0;
global $ftot;
while($row = mysql_fetch_array($query)){
$cost = $row['cost']*$_SESSION['cart'][$row['id']]['quantity'];
$subtotal = $_SESSION['cart'][$row['id']]['quantity']*$_SESSION['cart'][$row['id']]['cost'];
$ftot += $subtotal;
$profit= $subtotal-$cost;
$no_sold=$_SESSION['cart'][$row['id']]['quantity'];
$price_sold=$_SESSION['cart'][$row['id']]['cost'];
$sql_2="INSERT INTO credit_sales (name, no_sold, price_sold, profit, shop_id, total, customer_name)
VALUES ('$row[id]', '$no_sold', '$price_sold','$profit','$_SESSION[shop]','$subtotal','$_POST[customer]')";
mysql_query($sql_2) or die(mysql_error());
$final_quantity = $row['quantity']-$no_sold;
$rowid=$row['id'];
$sql5="UPDATE `products` SET `quantity`= $final_quantity WHERE id = $rowid";
mysql_query($sql5);
} }