Jump to content


Photo

unserialize , really need some help please


  • Please log in to reply
11 replies to this topic

#1 benluke

benluke
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 24 May 2006 - 01:49 PM

Hi,

Im storing arrays in mysql and "trying" to unserialize the data. But im pretty much stuck! Can anybody help me?

Im using 3 scripts/pages.

form.php
<form action="submit.php" method="post">
<input type="checkbox" name="knowledge1[]" value="A"> 
<input type="checkbox" name="knowledge1[]" value="B"> 
<input type="checkbox" name="knowledge1[]" value="C"> 
<input type="checkbox" name="knowledge1[]" value="D"> 
<br>
<input type="checkbox" name="knowledge2[]" value="A"> 
<input type="checkbox" name="knowledge2[]" value="B"> 
<input type="checkbox" name="knowledge2[]" value="C">
<input type="checkbox" name="knowledge2[]" value="D"> 
<br>
<input type="checkbox" name="knowledge3[]" value="A"> 
<input type="checkbox" name="knowledge3[]" value="B"> 
<input type="checkbox" name="knowledge3[]" value="C">
<input type="checkbox" name="knowledge3[]" value="D"> 
<input name="pc" type="hidden"  value="pc">
</form>


submit.php
<?
$pc1=serialize($_POST['knowledge1']);
$pc2=serialize($_POST['knowledge2']); 
$pc3=serialize($_POST['knowledge3']);
$update = $_POST["pc"];



if ($update == 'pc'){
    
    $query="UPDATE hsc SET knowledge_1 = '$pc1', knowledge_2 = '$pc2', knowledge_3 = '$pc3', submitted = '1', submitted_date=now()   WHERE userid = $userid LIMIT 1";
    $result=mysql_query($query);

        if ($result){ 
           echo "<h3>Submitted</h3>
      <p>This has been successfully submitted.<br><br>
        <a href='index.php'>Click Here</a> to continue</p><br><br><br><br><br><br><br><br>";
        }
        elseif (!$result){
                echo "<h3>ERROR</h3><p> There was a Problem submitting .<br><br>
                If the problem persists please contact the support team.";
    }
}

?>


The above 2 scripts seem to be working fine, the data is being stored in the database looking like this a:4:{i:0;s:1:"A";i:1;s:1:


Im having problems with pulling the info and displaying it. Infact i cant work out how to do it? Im thinking this post should have gone in PHP newbie. Still, can anybody help. How do i pull the arrays from the DB and disp[ly them??

Benluke




#2 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 24 May 2006 - 01:57 PM

A quick example.
<?php

  // connect to db.
  $sql = "SELECT knowledge_1 FROM hsc WHERE userid = $userid LIMIT 1";
  if ($result = mysql_query($sql)) {
    $row = mysql_fetch_assoc($result);
    $array = unserialize($row);
    print_r($array);
  }

?>


#3 benluke

benluke
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 24 May 2006 - 02:05 PM

Thanks thorpe,

Just tried your code. Not getting any errors but it does not display any data. Just blank

Checked the database to make sure data is stored and thats fine a:4:{i:0;s:1:"A";i:1;s:1: is stored in the knowledge_1 field

Thanks benluke

#4 benluke

benluke
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 24 May 2006 - 06:05 PM

can Anybody help me out with this one??

ben


#5 benluke

benluke
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 25 May 2006 - 09:07 AM

Could the problem be that it has not been stored in the table correctly??
Im at a dead end with this at the mo.

this is the data in the table

a:1:{i:0;s:1:"D";}

this is the code that thorpe kindly supplied


<?php

 
  $sql = "SELECT knowledge_1 FROM hsc WHERE userid = $userid LIMIT 1";
  if ($result = mysql_query($sql)) {
    $row = mysql_fetch_assoc($result);
    $array = unserialize($row);
    print_r($array);
  } else {
  echo "there was a problem";
  }

?>


Its returning a blank screen.

Could anyone shed any lite on this for me please??

Benluke


#6 Crimpage

Crimpage
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts
  • LocationBrisbane, Australia

Posted 25 May 2006 - 09:40 AM

I didn't think you would be able to unserialize all of $row. I think you will need to do:

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


Crimpage

#7 benluke

benluke
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 25 May 2006 - 09:49 AM

Hi Crimpage, Tahnks for your help with this.

<?php


  $sql = "SELECT knowledge_1 FROM hsc WHERE userid = $userid LIMIT 1";
  if ($result = mysql_query($sql)) {
    $row = mysql_fetch_assoc($result);
    $array = unserialize($row['knowledge_1']);
    print_r($array);
  } else {
  echo "there was a problem";
  }

?>

unfortunately im still getting same blank screen.

benluke

#8 Crimpage

Crimpage
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts
  • LocationBrisbane, Australia

Posted 25 May 2006 - 09:54 AM

Right after $row = ...

can you do print_r ($row);

see what it returns, if nothing, then it is a problem with the sql query . $userid variable...

#9 benluke

benluke
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 25 May 2006 - 11:07 AM

hi,

that does display the folllowing

Array ( [knowledge_1] => a:2:{i:0;s:1:"A";i:1;s:1: )


ben

#10 Crimpage

Crimpage
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts
  • LocationBrisbane, Australia

Posted 25 May 2006 - 11:33 AM

try setting after $row = etc...

$array = array();

before running the $array = unserialize etc...

#11 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 25 May 2006 - 12:35 PM

First, go back to how you store the serialized data in your database and change it to use the mysql_real_escape_string() function:
<?php
$pc1=mysql_real_escape_string(serialize($_POST['knowledge1']));
$pc2=mysql_real_escape_string(serialize($_POST['knowledge2']));
$pc3=mysql_real_escape_string(serialize($_POST['knowledge3']));
?>
This will make sure the data is properly escaped so it goes into the database correctly.

Once the data is in the database correctly, then retrieving it should be easy.
<?php
  $sql = "SELECT knowledge_1 FROM hsc WHERE userid = $userid LIMIT 1";
  $result = mysql_query($sql) or die("Problem with query: $sql<br>" . mysql_error());
  $row = mysql_fetch_assoc($result);
  echo '<pre>' . print_r($row,true) . '</pre>';
  $array = unserialize($row['knowledge_1']);
  echo '<pre>' . print_r($array,true) . '</pre>';
?>

Ken

#12 benluke

benluke
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 19 June 2006 - 12:15 PM

Thanks to everyone,

works perfectly now

benluke





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users