sachin_ware
sachin_ware

Reputation: 1561

PHP String Compare

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?

Upvotes: 1

Views: 81

Answers (2)

Krish R
Krish R

Reputation: 22711

Product id is a primary key and integer and it doesn't support the 101-1 value, from 101-1 which one is id related I mean 101 0r 1, in that use use split with - and use that.

Can you replace this from existing code,

 $price = trim($cols[$i][4]);
 mysqli_query($con,"UPDATE `TABLE 1` SET `price`='".$price."'  WHERE `id`='".$v."' ");

Upvotes: 0

Esqarrouth
Esqarrouth

Reputation: 39181

If $v or $cols[$i][4] is the String that is not updating correctly,

WHERE `id`='$v'

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

You need to use ' ' around a string in the query.

Upvotes: 2

Related Questions