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) 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? 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). 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");
Sign up to request clarification or add additional context in comments.

3 Comments

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.
Ah, the serialize wasn't there when I posted the comment. =)
Ah, sorry, yeah. Saved my edit just as you commented I think :P
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

Comments

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

?>

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.