1

I am trying to update the price of my product based on ID. ID and price both are string.My code is

for($i=1;$i<=$rows;$i++)
{
    $flag=0;
   $result = mysqli_query($con,"SELECT id FROM `TABLE 1` ");
   while($row = mysqli_fetch_array($result))
   {     
     $v=strval($cols[$i][0]);
     if(strcmp($row['id'],$v)==0)//id exists in database=> update
     {
        mysqli_query($con,"UPDATE `TABLE 1` SET `price`=".$cols[$i][4]."  WHERE `id`=$v");
        //echo $cols[$i][0];
        $flag=1;
     }
    }

Where cols[][] is my multidimensional array. It update the record correctly whose type is integer but not those are String.But product with id 101-1 not updated correctly.Where I am missing?

4

2 回答 2

2

如果 $v 或 $cols[$i][4] 是未正确更新的字符串,

WHERE `id`='$v'

`price`='".$cols[$i][4]."'

您需要在查询中的字符串周围使用 ' '。

于 2013-11-09T09:31:44.850 回答
0

产品 id 是主键和整数,它不支持101-1从 101-1 开始的值,其中一个与 id 相关,我的意思是 101 0r 1,使用 split with-并使用它。

您可以从现有代码中替换它吗?

 $price = trim($cols[$i][4]);
 mysqli_query($con,"UPDATE `TABLE 1` SET `price`='".$price."'  WHERE `id`='".$v."' ");
于 2013-11-09T09:35:17.813 回答