serialize function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
andrade1
Forum Newbie
Posts: 14
Joined: Fri Apr 18, 2008 9:16 am

serialize function

Post by andrade1 »

Hi

I am trying to use the serialize function but I think I have something incorrect because I am getting a blank page when I submit my form.

Code: Select all

 
 
//request:
 
$request = serialize($_POST['request']);
$query=INSERT INTO request VALUES('$request');
mysql_query($query) or die (mysql_error());
 
$query = "SELECT * from Request";
$result = mysql_query($query);
while (mysql_fetch_array($result, MYSQL_ASSOC))
{
$request = unserialize($row['request']);
foreach ($request as $value)
{
echo "$value -";
}
echo "<br/>\n";
}
mysql_query($query) or die (mysql_error());
 
 
//Run Insert Query
 
$query = "INSERT INTO Log (DateRcvd, OrigDept, OrigContact, RespOffice, Request, contents,  refer, deadline, GivenTo, stats, outcome, notes, complete) 
VALUES ('$drcvd', '$ODT', '$ocont', '$RSP', '$contents', '$REF', '$deadline', '$request', '$givento', '$status', '$outcome', '$notes', '$completed')";
 
$result = mysql_query($query);
 
 
 
 
Last edited by Weirdan on Tue Jun 03, 2008 4:37 pm, edited 1 time in total.
Reason: php tags
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: serialize function

Post by Weirdan »

yeah, you forgot to quote your query - pretty easy to spot with syntax highlighting turned on, eh?
andrade1
Forum Newbie
Posts: 14
Joined: Fri Apr 18, 2008 9:16 am

Re: serialize function

Post by andrade1 »

Hi

I added the missing parenthesis in and I am receiving:
Column count doesn't match value count at row 1

Code: Select all

 
//request:
 
$request = serialize($_POST['request']);
$query="INSERT INTO Log VALUES('$Request')";
mysql_query($query) or die (mysql_error());
 
$query = "SELECT Request from Log";
$result = mysql_query($query);
while (mysql_fetch_array($result, MYSQL_ASSOC))
{
$request = unserialize($row['request']);
foreach ($request as $value)
{
echo "$value -";
}
echo "<br/>\n";
}
mysql_query($query) or die (mysql_error());
 
my database is Log, the array is request, and the name of the field the data is going to in the database is Request
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: serialize function

Post by Weirdan »

You need to escape the serialized data with mysql_real_escape_string() function.
andrade1
Forum Newbie
Posts: 14
Joined: Fri Apr 18, 2008 9:16 am

Re: serialize function

Post by andrade1 »

how would I use the escape string function? Currently with the code I have below I am receiving null (default value) in the Request field instead of the values of the checkboxes.

Code: Select all

 
//Checkbox array: request
 
$request = serialize($_POST['request']);
$query= "INSERT INTO Log (Request) VALUES('$request')";
mysql_query($query) or die (mysql_error());
 
 
$query = "SELECT Request from Log";
$result = mysql_query($query);
while (mysql_fetch_array($result, MYSQL_ASSOC))
{
$request = unserialize($row['request']);
foreach ($request as $value)
{
echo "$value -";
}
echo "<br/>\n";
}
mysql_query($query) or die (mysql_error());
 
 
 
 
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: serialize function

Post by Kieran Huggins »

can you echo the serialized string for us?
andrade1
Forum Newbie
Posts: 14
Joined: Fri Apr 18, 2008 9:16 am

Re: serialize function

Post by andrade1 »

the echo of the serialize function is:

N;
Post Reply