这个想法是将相同的数据插入到SQL $quantity中,这意味着如果我有数量= 10,SQL查询需要插入相同的数据10次,问题是我总是有一条记录,如果我有quantity=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连接和闭合代码。
发布于 2012-04-23 19:41:03
如果您总是有多一条记录,这可能意味着您的数量值不正确(?)。
此外,这与您的问题无关,您应该使用while($i<=$quantity)而不是while($i<="$quantity")。您不需要"%s
第一个块是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++;
}下一条if语句将再次执行查询,这意味着您多插入了一行。删除整个if语句可以解决“插入的行数太多”的问题。
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}或者,将while循环更改为:。
$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++;
}https://stackoverflow.com/questions/10279764
复制相似问题