Bakht
Bakht

Reputation: 113

How to serialize and unserialize array?

I want to serialize this:

$product_detail = array(
            'product_name' => $_POST['product_name'],
            'product_price' => $_POST['product_price'],
            'product_description' => $_POST['product_description'],
            'product_size' => $_POST['product_size'] // this line get the item in option "value" 
        );
$product_detail=serialize($product_detail); //convert array into string.

But when I un serialize, and enter that into db, this error shows :

INSERT INTO `products` (`0`) VALUES ('') 

If I don't unserialize it shows:

INSERT INTO `products` (`QA92000I` like it alotLarge) VALUES ('') 

Tell me where i am getting wrong i am using codeigniter

Upvotes: 1

Views: 1365

Answers (3)

RiggsFolly
RiggsFolly

Reputation: 94662

Ok if you want to serialize those fields and store them in a single field on the row, thats fine, but you need to know what field in the table you want to place the serialized data into so this is the basic flow

function product($id)
{
   $this->load->model("your_model");// load your model file

   $product_detail = array(
            'product_name' => $_POST['product_name'],
            'product_price' => $_POST['product_price'],
            'product_description' => $_POST['product_description'],
            'product_size' => $_POST['product_size'] 
        );
   $product_detail=serialize($product_detail); 

   $product = array('id' => $id,
                    'product_details' = $product_detail
              );
   $this->your_model->insert($product);
}

Upvotes: 0

Bakht
Bakht

Reputation: 113

Thats how i un serialize:

$product_detail=unserialize($product_detail);

That is how I transfer variable to model:

$this->Product_area_model->product_add_new($product_detail);

This is my query:

$this->db->insert('products',$product_detail);

Upvotes: 1

Saty
Saty

Reputation: 22532

No need to serialize and unserialize array in codeigniter while insert it into database:-

You can insert it using this:-

//this is controller code:-

function product()
{
 $this->load->model("your_model");// load your model file
    $product_detail = array(
                'product_name' => $_POST['product_name'],
                'product_price' => $_POST['product_price'],
                'product_description' => $_POST['product_description'],
                'product_size' => $_POST['product_size'] // this line get the item in option "value" 
        );
$this->your_model->product($product_detail);// pass your array directly to your model file

}

// this is your model code
function product($product_detail)
{
$this->db->insert('products',$product_detail)
}

Upvotes: 2

Related Questions