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?

2
  • What is $cols value ? can you please post your code or can you explain more about. Commented Nov 9, 2013 at 9:30
  • $cols stores the values from tab separated file.$cols[$i][0] is column for Id and $cols[$i][4] is price from file respectively.i already echo the result it gives correct value from file. Commented Nov 9, 2013 at 9:33

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

It works one more doubt is I am executing query $result = mysqli_query($con,"SELECT id FROM TABLE 1 "); repeatedly in for loop. Can I used it once and reset Result set. i tried it using mysql_seek_data($result,0) But does not work?
0

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."' ");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.