PHP WHILE循环无法正常工作
我在下面的代码中使用while循环在表中创建条目 问题是它总是进入最后一条记录两次 例如,如果我输入3条记录PHP WHILE循环无法正常工作,php,Php,我在下面的代码中使用while循环在表中创建条目 问题是它总是进入最后一条记录两次 例如,如果我输入3条记录 这是1 这是2 这是3 输入的记录将是: 这是1 这是2 这是3 这是3 最后一条记录总是输入两次。我不明白是什么问题。 请 if(isset($_POST['credit'])){ if($_POST['customer'] == ""){ echo "Please Choose a Customer"; }else{
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);
} }
请避免使用不推荐使用的mysql函数您的示例代码非常容易受到sql注入的攻击…@enigma我如何才能使其更好?正如HawasKaPujaari所说,您应该避免使用不推荐使用的mysql函数。改用。您可以了解。这意味着在我们可以看到的代码之后输入双精度。。我想。