gpt4 book ai didi

php - MYSQL 错误 - 使用 PHP For LOOP

转载 作者:行者123 更新时间:2023-11-30 00:00:51 25 4
gpt4 key购买 nike

我知道当涉及语法错误时不应该提出问题,但是,我完全不知道为什么会抛出以下错误:

PHP 和 MYSQL 代码

//Possible size array to loop through when checking quantity
$con_size = array (26,28,30,32,33,34,35,355,36,365,37,375,38,385,39,395,
40,405,41,415,42,425,43,435,44,445,45,455,46,465,47,475,48,485);
$arrayLength=count($con_size);

for($x=0;$x<$arrayLength;$x++)
{

// check if size is available
if($line['quantity_c_size_'.$con_size[$x].'_con_b'] > 0 )
{

$quantity = $line['quantity_c_size_'.$con_size[$x].'_con_b'];
$current = $line['quantity_c_size_'.$con_size[$x]];
$id = $line ['product_id'];


$data = "UPDATE product_test
SET quantity_c_size_$con_size[$x] = $quantity + $current,
quantity_c_size_$con_size[$x]_con_b = 0
WHERE product_id = $id";

$result2 = mysql_query($data);

if ($result2)
{
echo 'Stock Updated <br />';
}
else
{
echo mysql_error();
echo '<br />';
}
}
}

MYSQL 错误

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' quantity_c_size_40_con_b = 0 WHERE product_id = 12897' at line 2

我已使用上面相同的 UPDATE 语法在 PHPMyAdmin 中尝试了 MYSQL 查询,并且查询成功完成。然而,当它在 PHP FOR 循环中运行时,它会抛出上述错误。

我可能错过了一些完全明显的东西,但现在我被难住了。

有什么建议吗?谢谢

最佳答案

按照Andrew的建议,我回显了UPDATE查询,发现$current变量为NULL,因此语句中的加法计算无法完成。

我将代码修改如下:

//Possible size array to loop through when checking quantity
$con_size = array (26,28,30,32,33,34,35,355,36,365,37,375,38,385,39,395,
40,405,41,415,42,425,43,435,44,445,45,455,46,465,47,475,48,485);
$arrayLength=count($con_size);

for($x=0;$x<$arrayLength;$x++)
{

// check if size is available
if($line['quantity_c_size_'.$con_size[$x].'_con_b'] > 0 )
{

$quantity = $line['quantity_c_size_'.$con_size[$x].'_con_b'];

if ($line['quantity_c_size_'.$con_size[$x]] == NULL)
{
$current = '0';
}
else
{
$current = $line['quantity_c_size_'.$con_size[$x]];

}

$id = $line ['product_id'];


$data = "UPDATE product_test
SET quantity_c_size_{$con_size[$x]} = $quantity + $current,
quantity_c_size_{$con_size[$x]}_con_b = 0
WHERE product_id = $id";

...

关于php - MYSQL 错误 - 使用 PHP For LOOP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25040560/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com