0

这个想法是将相同的数据插入 SQL DB $quantity 次,这意味着如果我有数量 = 10,则 SQL 查询需要插入相同的数据 10 次问题是,如果我有数量,我总是多一条记录=2 我将有 3 条记录,如果我有 1 我将有 2 条记录,所以

$i=1; 
while ($i<="$quantity")   
{
  $sql="INSERT INTO arivage (ID_Ship, Date_ariv, Date_achat, prov_id, Sph, cyl, Prod_type, Pord_color)  
       VALUES ('', '$date', '$date1', '$prov_id', '$sph', '$cyl', '$Prod_type', '$Prod_color')";
  mysql_query($sql);      
  $i++;
}
if (!mysql_query($sql,$con))
{
  die('Error: ' . mysql_error());
}

我也有 sql 连接并在代码中关闭。

4

1 回答 1

1

If you always have one record too many, that probably means your quantity value is not correct (?).

Also, this is not related to your problem, you should use while($i<=$quantity) instead of while($i<="$quantity"). You do not need the "s

This first block is ok:

$i=1; 
while ($i<="$quantity")   
{
    $sql="INSERT INTO arivage (ID_Ship, Date_ariv, Date_achat, prov_id, Sph, cyl, Prod_type, Pord_color)  
        VALUES ('', '$date', '$date1', '$prov_id', '$sph', '$cyl', '$Prod_type', '$Prod_color')";
    mysql_query($sql);      
    $i++;
}

The next if statement executes your query AGAIN, meaning you insert one too many rows. Removing the entire if statement would solve your "one too many rows is inserted" problem.

if (!mysql_query($sql,$con))
{
    die('Error: ' . mysql_error());
}

Alternatively, change your while loop to:.

$i=1; 
while ($i<="$quantity")   
{
    $sql="INSERT INTO arivage (ID_Ship, Date_ariv, Date_achat, prov_id, Sph, cyl, Prod_type, Pord_color)  
    VALUES ('', '$date', '$date1', '$prov_id', '$sph', '$cyl', '$Prod_type', '$Prod_color')";
    if(!mysql_query($sql, $con)) {
        die('Error: ' . mysql_error());
    }
    $i++;
}
于 2012-04-23T11:41:02.937 回答