Jump to content

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


Link to comment
https://forums.phpfreaks.com/topic/10348-unserialize-really-need-some-help-please/
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]
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
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
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
  • 4 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.