Jump to content

Archived

This topic is now archived and is closed to further replies.

benluke

unserialize , really need some help please

Recommended Posts

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
[code]
<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>
[/code]


submit.php
[code]
<?
$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.";
    }
}

?>
[/code]


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


Share this post


Link to post
Share on other sites
A quick example.
[code]
<?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);
  }

?>
[/code]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

[code]
a:1:{i:0;s:1:"D";}
[/code]

this is the code that thorpe kindly supplied

[code]

<?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";
  }

?>

[/code]

Its returning a blank screen.

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

Benluke

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Hi Crimpage, Tahnks for your help with this.

[code]
<?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";
  }

?>
[/code]

unfortunately im still getting same blank screen.

benluke

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
hi,

that does display the folllowing

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


ben

Share this post


Link to post
Share on other sites
try setting after $row = etc...

$array = array();

before running the $array = unserialize etc...

Share this post


Link to post
Share on other sites
First, go back to how you store the serialized data in your database and change it to use the mysql_real_escape_string() function:
[code]<?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']));
?>[/code]
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.
[code]<?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>';
?>[/code]

Ken

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.