1

I have an array with three values red, green and yellow. Now i should

insert red into column3 row1
insert green into column3 row2
insert yellow into column3 row3

How can i do that i tried writing the code

foreach ($output as $value)
{
    echo ($value.'<br>');
    $tstring = implode(',' , $output);
    $insert_col= "UPDATE INTO `5` (B) VALUES ('$tstring')";
    $insert_result = mysql_query($insert_col);
    if ($insert_result)
    { 
        echo ("RECORDED!")|
        exit();
    }
}

but it does not work. it is filling extra rows to the existing table with a value R.

Please help!

7
  • What about connecting to your DB before trying to query? Also, don't use mysql_*, use PDO or mysqli_* Commented Aug 27, 2012 at 11:43
  • 1
    This doesn't $insert_col= "UPDATE INTO `5` (B) VALUES ('$tstring')"; make any sense. I doubt that your table name is "5", further you need to tell the database which column to update by a WHERE clause. Commented Aug 27, 2012 at 11:47
  • i did the connection part before itself i have not mentioned that code here, since the code will be lengthy. Commented Aug 27, 2012 at 11:48
  • i specified the column name as (B).. sorry i m a newbie, dont know much about that... fine i ll try using "WHERE" thanks Commented Aug 27, 2012 at 11:49
  • I've a question though: why is there an implode? If I have understood, you want to update each row with a value from the array, not with all the values of the array. Commented Aug 27, 2012 at 11:57

2 Answers 2

3

if I remember correctly, an UPDATE statement should have a WHERE clause.

Like:

 UPDATE table SET column_name='value' WHERE condition;

You can find examples at w3schools.

As for inserting the right value:

foreach($output as $value){
     $tstring = $value;
     $insert_col = "UPDATE `5` SET B='" . $tstring . "' WHERE insert a condition here";
     $insert_result = mysql_query($insert_col);

    if ($insert_result) {
         echo ("RECORDED!") |
    }
}

You don't want to insert all your array in one row, which means that the implode is useless.

And I repeat, you need a WHERE clause here. Without it, you'll update all your rows with the same values.

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

12 Comments

Good spot. WHERE is optional, yet, without it all rows will be set to the new value, which is rarely intended.
Thanks i tired using the following command but it dint work :( $insert_col= "UPDATE 5 SET column_name='B' WHERE VALUES ('$tstring')";
No no, the code should be: "UPDATE 5 SET B='".$tstring."' WHERE id=1" for example. id=1 is an example, I don't know your table.
it works...! but i dont know why it filling the values "R" in all the fields :( where i m going wrong ...!! :(
ups I deleted my comment as you did answer the question above. Well, I don't see why it's recording the first letter of your string, can you show us your new query (in case of there would be something missiny)?
|
1

You got couple of errors. Looks you forgot to connect to the database and also you exit() after 1st successful insert. So if you got more elements in array, these will not be stored. You may also want to display value of mysql_error(); in case of failure

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.