1

When I echo a variable it gave me a great looking list but when I update mysql database it only add the last row in the list I read that there is a problem with array and foreach with mysql.

  foreach ($array["product"] as $list)
            {
            $ok = ''.$list[0].'';
            $sql = mysql_query("UPDATE product SET desc='$ok' WHERE id='$id'") or die(mysql_error());
                      }
            }

Is there an easy way to put my list in mysql whitout having to serialize I read its not the best thing to do and I am not familiar with that.

Thanks!

Pierre-luc
  • 33
  • 8
  • 2
    mysql_* functions are deprecated as of PHP 5.5.0, and removed as of PHP 7.0.0. Switch your code to use [PDO](https://secure.php.net/manual/en/pdo.prepared-statements.php) or [mysqli](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) instead. This will also help prevent any quoting headaches you might be running into. – aynber May 04 '17 at 16:32
  • `UPDATE` queries don't generally "put in mysql". – Uueerdo May 04 '17 at 16:44
  • Yeah you right I already insert information I want to update the row – Pierre-luc May 04 '17 at 16:47
  • how(when you change the $id ??? – ScaisEdge May 04 '17 at 16:57
  • @Pierre-luc yeah, just making sure to cover bases; and scaisEdge beat me to my next question. – Uueerdo May 04 '17 at 16:59
  • Thanks for fast reply i have no trouble update the row but the issue is it only save the last line in my list i need all line. sometime my list can be big like 50 line and right now I only manage to save the last. When i echo it show all of them something wrong. – Pierre-luc May 04 '17 at 17:09
  • It appears you are updating the same record in the database every iteration. `WHERE id='$id'` never changes. – Uueerdo May 04 '17 at 18:01
  • Yah thats what I want :) I just need the complete list not just the last row :) the $list[0] is my issue here I think And I am really block with that. – Pierre-luc May 04 '17 at 19:25
  • Without changing the $id, you will just be overwriting the contents of `desc` with the last $ok. Were you wanting to append instead? – Uueerdo May 04 '17 at 20:21
  • No I want to overwrite for now. – Pierre-luc May 05 '17 at 02:55
  • Great i manage to serialize and add all data using the example of "tipico" here http://stackoverflow.com/questions/8641889/how-to-use-php-serialize-and-unserialize – Pierre-luc May 05 '17 at 06:45
  • After testing i dont recommand serialize because the loading time of the page will increase a lot. – Pierre-luc May 06 '17 at 14:02

0 Answers0