1

I have a form with multiple inputs that user can append. I am able to get all the value from the inputs using jquery ajax. My issue is how to store the array on mysql? I have read about php serialize and unserialize but not quite sure to do it on my case. Below are the codes.

Form

<form>
    <input type="text" name="name" class="name" placeholder="Name">
    <input type="text" name="age" class="age" placeholder="Age">
    <input type="text" name="gender" class="gender" placeholder="Gender">
</form>

<button type="submit" id="store" value="store">Submit</button>
<button type="button">Cancel</button>

jQuery

$('#add').click(function(){     
    $('#append > ul').append(
        '<li style="list-style: none;">'
        + '<input type="text" name="name" class="name" placeholder="Name">' 
        + '<input type="text" name="age" class="age" placeholder="Age">'
        + '<input type="text" name="gender" class="gender" placeholder="Gender">'
        + '</li>';
    );

});

$('#remove').click(function(){
    $('li:last-child').detach();
});

function store_siblings(e){
    e.preventDefault();
    var arr = [];
    var submit = $('#store').val(),

    $('input.nama').each(function(){
        arr.push($(this).val());
    });

    $('input.age').each(function(){
        arr.push($(this).val());
    });

    $('input.gender').each(function(){
        arr.push($(this).val());
    });

    $.post('../upd-siblings.php',
    {
        submit : submit,    
        arr : arr
    }, function(data){
        $('#result').html(data)
    });

}

$("#store").click(store_siblings);

After I make the serialization and echo I get:

a:6:
{
   i:0;s:7:"MY WIFE";
   i:1;s:6:"MY SON";
   i:2;s:2:"27";
   i:3;s:1:"2";
   i:4;s:4:"WIFE";
   i:5;s:3:"SON";
}

Below are the table siblings That I create on mysql:

siblings
- id
- name
- age
- gender

Can I use mysqli INSERT using the serialize values?

Amran
  • 627
  • 3
  • 11
  • 30

1 Answers1

0

Checkout this link: Description, Quite good and easy to understand explanation of serialization in PHP, if you still get stuck somewhere, just comment and i'll try to clear it up.

Talha Abrar
  • 880
  • 8
  • 22
  • Thanks for the link. That means if I want to store the value, my query would look like `INSERT INTO siblings (name,age,gender ) VALUES ('$serialized_array')` – Amran May 30 '17 at 04:25
  • Yes, that's exactly what it means, and please accept the answer if it helped you – Talha Abrar May 30 '17 at 05:50