php serialize array, insert database, error when unserialize

Hi,

I am using "serialize" before I insert the $_POST variables into a database.

I can put it in the db, get it back, unserialize and have a copy if the array as I put it in.

There is a problem when I use quotes.

When I have a quote in the form, it will go into the database, but when I try to
get it back and unserialize, but there is an error in the following code.

The error message is "Warning: Invalid argument supplied for foreach()".

When
$row = a:11:{s:11:"contactName";s:9:"test name";s:15:"contactAddress1";s:12:"test address";s:11:"contactCity";s:9:"test city";s:12:"contactState";s:13:"Please Select";s:10:"contactZip";s:0:"";s:13:"contactPhone1";s:0:"";s:13:"contactPhone2";s:0:"";s:13:"contactPhone3";s:0:"";s:12:"contactEmail";s:0:"";s:18:"contactAddComments";s:0:"";s:6:"submit";s:6:"Submit";}

it will work.

When
$row = a:11:{s:11:"contactName";s:18:"test with quo'tes";s:15:"contactAddress1";s:0:"";s:11:"contactCity";s:0:"";s:12:"contactState";s:13:"Please Select";s:10:"contactZip";s:0:"";s:13:"contactPhone1";s:0:"";s:13:"contactPhone2";s:0:"";s:13:"contactPhone3";s:0:"";s:12:"contactEmail";s:0:"";s:18:"contactAddComments";s:0:"";s:6:"submit";s:6:"Submit";}

it will get the error.

      while ($row = mysql_fetch_array($dbresult))
      {            
            foreach ($row as $key => $val) {
                        $tmp[$key] = $val;
                }

      $tmpArray=unserialize($tmp[body]);

      foreach ($tmpArray as $key1 => $val1) {
      print "$key2 = $val1";            
      }
jackjohnson44Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Diablo84Commented:
have you tried adding stripslashes

$tmpArray=unserializestripslashes(($tmp['body']));
0
Diablo84Commented:
or rather

$tmpArray=unserialize(stripslashes($tmp['body']));
0
_GeG_Commented:
or use this as the first line of you PHP code:
set_magic_quotes_runtime(0);
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

RoonaanCommented:
Doesn't unserialize itself give a notice?

What db field are you using? varchar/text/blob?

Did you use addslashes(serialize($array)) on insertion?

-r-
0
jackjohnson44Author Commented:
this did not work
$tmpArray=unserialize(stripslashes($tmp['body']));

I don't see why it would though, there are no slashes in the serialized array

the part that is breaking it is here ->    test with quo'tes

look:
$row = a:11:{s:11:"contactName";s:18:"test with quo'tes";s:15:"contactAddress1";s:0:"";s:11:"contactCity";s:0:"";s:12:"contactState";s:13:"Please Select";s:10:"contactZip";s:0:"";s:13:"contactPhone1";s:0:"";s:13:"contactPhone2";s:0:"";s:13:"contactPhone3";s:0:"";s:12:"contactEmail";s:0:"";s:18:"contactAddComments";s:0:"";s:6:"submit";s:6:"Submit";}


I am using a field of type 'text' in the database.

I am not using add slashes when I put it in the db because magic quotes are turned on.
The data is going into the db correctly, I just can't get it back.  Slashes are added, but
when it goes in the db, they are not there any more.  look at the sample serialized array above.
0
InvolveITCommented:
You can replace the quotes with some combination of characters before serializing it and correct it after you have unserialized the data.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.