Welcome Guest, Not a member yet? Register   Sign In
[HELP NEEDED] array serialization and inserting into database
#1

[eluser]M52 Studios[/eluser]
Hey guys,

I need to serialize an array and insert it into the database. Two things are coming up as odd.

First of all, when I try to serialize an array and print it to screen it looks as it should.

Code:
a:9:{s:10:"legal_name";s:10:"legal name";s:12:"display_name";s:11:"public name";s:12:"live_support";s:2:"No";s:17:"reservation_phone";s:9:"123456789";s:7:"website";s:11:"www.www.com";s:16:"insured_vehicles";s:1:"6";s:10:"start_year";s:4:"1986";s:11:"association";s:2:"No";s:17:"short_description";s:25:"short company description";}

BUT

when I do a database insertion using similar code...

Code:
$foo = array();

$foo[] = 1;
$foo[] = 2;

$str = serialize($foo);

$data = array('part_1' => $str);

$this->db->insert('temp_form_data', $data);

I get two entries... one being identical to the one above under 'id' of 1, and another one that has no values, under 'id' of 2. So, for some odd reason, I'm getting two insertions.

Code:
a:9{s:10:"legal_name";N;s:12:"display_name";N;s:12:"live_support";N;s:17:"reservation_phone";N;s:7:"website";N;s:16:"insured_vehicles";N;s:10:"start_year";N;s:11:"association";N;s:17:"short_description";N;}

Has anyone else ran across this issue? If so, how did you fix it?

Another problem is when I try to update the other columns of the same table with similar serialized arrays, I am getting arrays that have no value, as my last example, but when I print them to screen, all the values are there. This has been breaking my head for most of the day. Anyone have any suggestions?

Thank you in advance.
#2

[eluser]Frenky.net[/eluser]
You could echo the count() of the $data array, and trace it back from there. Or you can try to var_dump the serialized data, and see where that takes you. There's not much to work with here.
#3

[eluser]M52 Studios[/eluser]
Here's my entire function and the results that are printed to the page, along with database insertions:

Code:
function process_part1()
    {
        /* legal_name - required
         * display_name
         * live_support - yes/no
         * reservation_phone - required
         * website
         * insured_vehicles - required
         * start_year - required
         * association - yes/no
         */
        
        $temp_data = array();
        
        /*
         * assign $_POST values to proper variables
         */
        $temp_data['legal_name'] = (isset($_POST['legal_name'])) ? $_POST['legal_name'] : NULL;
        //$this->legal_name = (isset($_POST['legal_name'])) ? $_POST['legal_name'] : NULL;
        //echo $this->legal_name . "<br />";
        $temp_data['display_name'] = (isset($_POST['display_name'])) ? $_POST['display_name'] : NULL;
        //echo $this->display_name . "<br />";
        $temp_data['live_support'] = (isset($_POST['live_support'])) ? $_POST['live_support'] : NULL;
        //echo $this->live_support . "<br />";
        $temp_data['reservation_phone'] = (isset($_POST['reservation_phone'])) ? $_POST['reservation_phone'] : NULL;
        //echo $this->reservation_phone . "<br />";
        $temp_data['website'] = (isset($_POST['website'])) ? $_POST['website'] : NULL;
        //echo $this->website . "<br />";
        $temp_data['insured_vehicles'] = (isset($_POST['insured_vehicles'])) ? $_POST['insured_vehicles'] : NULL;
        //echo $this->insured_vehicles . "<br />";
        $temp_data['start_year'] = (isset($_POST['start_year'])) ? $_POST['start_year'] : NULL;
        //echo $this->start_year . "<br />";
        $temp_data['association'] = (isset($_POST['association'])) ? $_POST['association'] : NULL;
        //echo $this->association . "<br />";
        $temp_data['short_description'] = (isset($_POST['description'])) ? $_POST['description'] : NULL;
        //echo $this->short_description . "<br />";
        
        $str = serialize($temp_data);
        
        print var_dump($str) . "<br /><br />";
        
        
        print $str . "<br /><br />";
        
        $data = array('part_1' => $str, 'part_2' => NULL, 'part_3' => NULL);
        
        print_r($data);
        print "<br /><br />";
        
        $this->db->insert('temp_form_data', $data);
        
        $id = $this->db->insert_id();
        
        return $id;
        
    } /* END process_part1*/

HERE'S THE OUTPUT FROM ABOVE

Code:
string(331) "a:9:{s:10:"legal_name";s:10:"legal name";s:12:"display_name";s:11:"public name";s:12:"live_support";s:2:"No";s:17:"reservation_phone";s:9:"123456789";s:7:"website";s:11:"www.www.com";s:16:"insured_vehicles";s:1:"6";s:10:"start_year";s:4:"1986";s:11:"association";s:2:"No";s:17:"short_description";s:25:"short company description";}"

a:9:{s:10:"legal_name";s:10:"legal name";s:12:"display_name";s:11:"public name";s:12:"live_support";s:2:"No";s:17:"reservation_phone";s:9:"123456789";s:7:"website";s:11:"www.www.com";s:16:"insured_vehicles";s:1:"6";s:10:"start_year";s:4:"1986";s:11:"association";s:2:"No";s:17:"short_description";s:25:"short company description";}

Array ( [part_1] => a:9:{s:10:"legal_name";s:10:"legal name";s:12:"display_name";s:11:"public name";s:12:"live_support";s:2:"No";s:17:"reservation_phone";s:9:"123456789";s:7:"website";s:11:"www.www.com";s:16:"insured_vehicles";s:1:"6";s:10:"start_year";s:4:"1986";s:11:"association";s:2:"No";s:17:"short_description";s:25:"short company description";} [part_2] => [part_3] => )

AND - here are the two entries that I am getting in the database:

Code:
[6] - a:9:{s:10:"legal_name";s:10:"legal name";s:12:"display_name";s:11:"public name";s:12:"live_support";s:2:"No";s:17:"reservation_phone";s:9:"123456789";s:7:"website";s:11:"www.www.com";s:16:"insured_vehicles";s:1:"6";s:10:"start_year";s:4:"1986";s:11:"association";s:2:"No";s:17:"short_description";s:25:"short company description";}    NULL    NULL
[7] - a:9{s:10:"legal_name";N;s:12:"display_name";N;s:12:"live_support";N;s:17:"reservation_phone";N;s:7:"website";N;s:16:"insured_vehicles";N;s:10:"start_year";N;s:11:"association";N;s:17:"short_description";N;}

See how the second one is a lot shorter because all the "variables" are there, but no values associated with them?

When I try to update part_2, and part_3 from similar functions, all I get are "no value" serialized arrays.

What else can I do to troubleshoot? I've tried using $_SESSIONS to keep track of all of this, but they mysteriously lose values after going through more than one page. I had a post regarding that one earlier.

Thanks guys, any help, at all would be much appreciated!!!!
#4

[eluser]WanWizard[/eluser]
I have a distinct feeling your issue is elsewhere.

Check your logs for a double request. This can easily happen if you use 'simple' rewrite rules, and a missing asset causes a second rewrite to index.php.

The second record is what you get if you request the page without a post, all "N" value's are the NULL's, assigned when the corresponding POST variable is not present.
#5

[eluser]M52 Studios[/eluser]
Could this be due to the fact that I am using a custom controller?

Code:
&lt;?php

class MY_Controller extends Controller
{
    function __construct()
    {
        parent::Controller();
        session_start();
    }
}
#6

[eluser]M52 Studios[/eluser]
This is what my logs look like...


DEBUG - 2010-12-20 09:09:58 --&gt; Config Class Initialized
DEBUG - 2010-12-20 09:09:58 --&gt; Hooks Class Initialized
DEBUG - 2010-12-20 09:09:58 --&gt; URI Class Initialized
DEBUG - 2010-12-20 09:09:58 --&gt; Router Class Initialized
DEBUG - 2010-12-20 09:09:58 --&gt; Output Class Initialized
DEBUG - 2010-12-20 09:09:58 --&gt; Input Class Initialized
DEBUG - 2010-12-20 09:09:58 --&gt; Global POST and COOKIE data sanitized
DEBUG - 2010-12-20 09:09:58 --&gt; Language Class Initialized
DEBUG - 2010-12-20 09:09:58 --&gt; Loader Class Initialized
DEBUG - 2010-12-20 09:09:58 --&gt; Helper loaded: url_helper
DEBUG - 2010-12-20 09:09:58 --&gt; Helper loaded: form_helper
DEBUG - 2010-12-20 09:09:59 --&gt; Database Driver Class Initialized
DEBUG - 2010-12-20 09:09:59 --&gt; Model Class Initialized
DEBUG - 2010-12-20 09:09:59 --&gt; Controller Class Initialized
DEBUG - 2010-12-20 09:09:59 --&gt; File loaded: C:\USB\xampp\htdocs\limousineservice.org/system/application/views/signup_view.php
DEBUG - 2010-12-20 09:09:59 --&gt; Final output sent to browser
DEBUG - 2010-12-20 09:09:59 --&gt; Total execution time: 0.2359
DEBUG - 2010-12-20 09:09:59 --&gt; Config Class Initialized
DEBUG - 2010-12-20 09:09:59 --&gt; Hooks Class Initialized
DEBUG - 2010-12-20 09:09:59 --&gt; URI Class Initialized
DEBUG - 2010-12-20 09:09:59 --&gt; Router Class Initialized
DEBUG - 2010-12-20 09:09:59 --&gt; Output Class Initialized
DEBUG - 2010-12-20 09:09:59 --&gt; Input Class Initialized
DEBUG - 2010-12-20 09:09:59 --&gt; Global POST and COOKIE data sanitized
DEBUG - 2010-12-20 09:09:59 --&gt; Language Class Initialized
DEBUG - 2010-12-20 09:09:59 --&gt; Loader Class Initialized
DEBUG - 2010-12-20 09:09:59 --&gt; Helper loaded: url_helper
DEBUG - 2010-12-20 09:09:59 --&gt; Helper loaded: form_helper
DEBUG - 2010-12-20 09:09:59 --&gt; Database Driver Class Initialized
DEBUG - 2010-12-20 09:09:59 --&gt; Model Class Initialized
DEBUG - 2010-12-20 09:09:59 --&gt; Controller Class Initialized
DEBUG - 2010-12-20 09:09:59 --&gt; File loaded: C:\USB\xampp\htdocs\limousineservice.org/system/application/views/signup_view.php
DEBUG - 2010-12-20 09:09:59 --&gt; Final output sent to browser
DEBUG - 2010-12-20 09:09:59 --&gt; Total execution time: 0.2524

I see what you mean in regards to two requests, but how do I fix it Big Grin Can you point me in the right direction?
#7

[eluser]WanWizard[/eluser]
You probably use a .htaccess that has rewrite rules in place testing for file or directory existence (the -f and -d flags) to prevent rewriting.
Which means that if you made a typo or used an image in your page that doesn't exist, the request for that asset is rewritten to index.php.

Add some debug code to write the requested URI to the log (see here for an suggestion).
#8

[eluser]InsiteFX[/eluser]
If you are inserting into the database, the field needs to be of type text.

You can also look at the session libraries _serialize() and _unserialize() functions.

InsiteFX
#9

[eluser]M52 Studios[/eluser]
Thanks for the answers guys. Insite, I do have the fields set as Text in MySQL, that doesn't seem to be a problem.

WanWzard, I've tried literally all of the .htaccess files that I could get my hands on. Some of them flat out do not show the main page / rest of the site, while the others work, but I am still getting the classes loading twice.

Also, someone mentioned in another thread something about the favicon. I do remember seeing that it is missing in my log files. Where is it supposed to be and how can I turn that off, also, what other recommendations do you guys have. I am out of options and I'm really under a gun here. I tremendously appreciate your guys' help!
#10

[eluser]M52 Studios[/eluser]
double post - Mods, please delete Smile




Theme © iAndrew 2016 - Forum software by © MyBB