1

Ii have stored an array list using

<?php
$array = array("foo", "bar", "hello", "world");
include("mydb.php");
$array_string=mysql_escape_string(serialize($array));
mysql_query("insert into tsest (array) values('$array_string')",$con) or die("not updated");
?>

now i want to update that field with a new array but don't delete previeus one i used

   <?php
$array = array("aaa");
include("mydb.php");
$array_string=mysql_escape_string(serialize($array));

$sql= 'select * from tsest where uname = "manoj" ';    
    $result = mysql_query($sql,$con);
    $row = mysql_fetch_array($result);  
$value=$row['array'];
mysql_query('UPDATE tsest SET array="'.$array_string.'"'.$value.' where uname="manoj"',$con) or die("hello");

?>

but it didn't work

4
  • 3
    "but it didn't work" is not a valid MySQL error message (and although I don't know PHP, I'm pretty sure it isn't a valid PHP error message either)
    – user330315
    Commented May 10, 2013 at 14:00
  • 1
    What does "it didn't work" mean? It didn't do anything? It updated everything? The wrong fields? The right fields with the wrong data?
    – andrewsi
    Commented May 10, 2013 at 14:01
  • if the value is null in that field then it updates but if array is stored previously then it didn't update and the die statement executes Commented May 10, 2013 at 14:02
  • You should first get existing serialized array from the db (SELECT), unserialize it, update in PHP, serialize again and put back to MySQL (UPDATE).
    – Voitcus
    Commented May 10, 2013 at 14:03

3 Answers 3

2

You have serialized your array before you store it, but you need to unserialize before you can add to it.

$value=unserialize($row['array']);

You also need to merge the arrays, doing a $array_string.'"'.$value will not work.

Try this:

$newArray = mysql_escape_string(serialize(array_merge($value, $array_string)));
mysql_query('UPDATE tsest SET array="'.$newArray.' where uname="manoj"',$con) or die("hello");
3
  • He is serializing his array before storing it. I don't agree with the way he's doing it, but it solves the issue he is experiencing.
    – hyarion
    Commented May 10, 2013 at 14:05
  • Ah, the serialize wasn't there when I posted the comment. =)
    – Alix Axel
    Commented May 10, 2013 at 14:06
  • Ah, sorry, yeah. Saved my edit just as you commented I think :P
    – hyarion
    Commented May 10, 2013 at 14:07
0

When you save the array like this

mysql_query("insert into tsest (array) values('$array_string')",$con) or die("not updated");

Then you are not saving a username along with this data. However when you are trying to fetch it you are using a username

$sql= 'select * from tsest where uname = "manoj" '; 

Same for update. Hence the update doesn't work because your array was not saved for a username manoj

So Therefore when you save the array you have to also save the username in your table

0

there is some error with your update sql and serialized value can not be simply joined to produce a new array try code below

   <?php

   $array = array("aaa"); include("mydb.php"); $sql= 'select * from
   tsest where uname = "manoj" ';

------------------------------------------------------------------------

   $result = mysql_query($sql,$con); $row = mysql_fetch_array($result); 
   $value=$row['array']; $new_array = array_merge($array,
   unserialize($value)); array_string =
   mysql_escape_string(serialize($new_array)); mysql_query('UPDATE tsest
   SET array="'.$array_string.'"'.' where uname="manoj"',$con) or
   die("hello");

?>

Not the answer you're looking for? Browse other questions tagged or ask your own question.