1
私はユーザーが追加できる複数の入力を持つフォームを持っています。私はjquery ajax
を使って入力からすべての価値を得ることができます。私の問題はどのようにmysqlに配列を格納するのですか?私はphy serialize
とunserialize
について読んだことがありますが、私の場合はそれほど確かではありません。以下はそのコードです。私はシリアライズを作り、私が得るエコー後シリアル化を使用してmysqlに複数のテキスト入力を格納
フォーム
<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);
:以下
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";
}
はテーブルsiblings
Tです私はmysqlで作成します。
siblings
- id
- name
- age
- gender
serializeの値を使用してmysqli INSERTを使用できますか?
リンクありがとうございます。つまり、値を格納する場合、クエリは 'INSERT INTO兄弟(名前、年齢、性別)VALUES( '$ serialized_array')のようになります。 – Amran
はい、それはまさにその意味です。あなたを助けた –