Php 将多个动态值存储到表中
我试图一次将多个值存储到mysql数据库中。我有一个具有动态输入字段的简单表单。当我提交表单中的值时,它只为第一个人插入值。其他值根本不显示。如何存储每个输入字段的值 Jquery创建字段Php 将多个动态值存储到表中,php,Php,我试图一次将多个值存储到mysql数据库中。我有一个具有动态输入字段的简单表单。当我提交表单中的值时,它只为第一个人插入值。其他值根本不显示。如何存储每个输入字段的值 Jquery创建字段 $(文档).ready(函数(){ $('#btnAdd')。单击(函数(){ var num=$('.clonedSection').length; var newNum=新的数字(num+1); var newSection=$('#pq_entry_'+num).clone().attr('id','
$(文档).ready(函数(){
$('#btnAdd')。单击(函数(){
var num=$('.clonedSection').length;
var newNum=新的数字(num+1);
var newSection=$('#pq_entry_'+num).clone().attr('id','pq_entry_'+newNum);
newSection.children(':first').children(':first').attr('id','name_'+newNum).attr('name','name_'+newNum).attr('placeholder','Person_35;'+newNum+'first name');
newSection.children(':nth child(1)')。children(':first').attr('id','faculty_uu'+newNum)。attr('name','faculty_'+newNum);
newSection.children(':nth child(2')).children(':first').attr('id','讲师\'+newNum).attr('name','讲师\'+newNum);
newSection.insertAfter('#pq_entry_'+num).last();
$('btnDel').prop('disabled','');
如果(newNum==5)$('#btnAdd').prop('disabled','disabled');
});
$('#btnDel')。单击(函数(){
var num=$('.clonedSection').length;//当前有多少个“可复制”输入字段
$('#pq_entry.'+num).remove();//删除最后一个元素
//启用“添加”按钮
$('btnAdd').prop('disabled','');
//如果只剩下一个元素,请禁用“删除”按钮
如果(num-1==1)$('btnDel').prop('disabled','disabled');
});
$('btnDel').prop('disabled','disabled');
});
用于插入的PHP
我在JavaScript和PHP中检测到错误,HTML正常:
JavaScript:必须使用:first、:n个子项(2)和:n子项(3)。看见
$(文档).ready(函数(){
$('#btnAdd')。单击(函数(){
var num=$('.clonedSection').length;
var newNum=新的数字(num+1);
var newSection=$('#pq_entry_'+num).clone().attr('id','pq_entry_'+newNum);
newSection.children(':first').children(':first').attr('id','name_'+newNum).attr('name','name_'+newNum).attr('placeholder','Person_35;'+newNum+'first name');
newSection.children(':nth child(2')).children(':first').attr('id','faculty_u'+newNum).attr('name','faculty_'+newNum);
newSection.children(':nth child(3')).children(':first').attr('id','讲师\'+newNum).attr('name','讲师\'+newNum);
newSection.insertAfter('#pq_entry_'+num).last();
$('btnDel').prop('disabled','');
如果(newNum==5)$('#btnAdd').prop('disabled','disabled');
});
$('#btnDel')。单击(函数(){
var num=$('.clonedSection').length;//当前有多少个“可复制”输入字段
$('#pq_entry.'+num).remove();//删除最后一个元素
//启用“添加”按钮
$('btnAdd').prop('disabled','');
//如果只剩下一个元素,请禁用“删除”按钮
如果(num-1==1)$('btnDel').prop('disabled','disabled');
});
$('btnDel').prop('disabled','disabled');
});
PHP:循环名称_1、名称_2和名称_3。。。
<script>
$(document).ready(function () {
$('#btnAdd').click(function () {
var num = $('.clonedSection').length;
var newNum = new Number(num + 1);
var newSection = $('#pq_entry_' + num).clone().attr('id', 'pq_entry_' + newNum);
newSection.children(':first').children(':first').attr('id', 'name_' + newNum).attr('name', 'name_' + newNum).attr('placeholder', 'Person #' + newNum + ' First Name');
newSection.children(':nth-child(1)').children(':first').attr('id', 'faculty_' + newNum).attr('name', 'faculty_' + newNum);
newSection.children(':nth-child(2)').children(':first').attr('id', 'instructor_' + newNum).attr('name', 'instructor_' + newNum);
newSection.insertAfter('#pq_entry_' + num).last();
$('#btnDel').prop('disabled', '');
if (newNum == 5) $('#btnAdd').prop('disabled', 'disabled');
});
$('#btnDel').click(function () {
var num = $('.clonedSection').length; // how many "duplicatable" input fields we currently have
$('#pq_entry_' + num).remove(); // remove the last element
// enable the "add" button
$('#btnAdd').prop('disabled', '');
// if only one element remains, disable the "remove" button
if (num - 1 == 1) $('#btnDel').prop('disabled', 'disabled');
});
$('#btnDel').prop('disabled', 'disabled');
});
</script>
<?php
if(isset($_POST['submit'])){
$con = new mysqli(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
$person1_faculty = $_POST['faculty_1'] ? 1 : 0;
$person1_instructor = $_POST['instructor_1'] ? 1 : 0;
$person1_name = $_POST['name_1'];
mysqli_query(
$con,
"INSERT INTO person (faculty_role, instructor_role, person_name) VALUES " .
"(" . $person1_faculty . ", " . $person1_instructor . ", '" . $person1_name . "')"
);
print_r($_POST);
}
?>
<form action="test3.php" method="POST">
<b>Select the role for each person</b>
<ul id="pq_entry_1" class="clonedSection">
<li>
<input id="name_1" name="name_1" placeholder="Person #1 - First Name" type="text" />
</li>
<li>
<input id="phone_1" name="faculty_1" type="checkbox" />Faculty
</li>
<li>
<input id="phone_1" name="instructor_1" type="checkbox" />Instructor
</li>
</ul>
<input type='button' id='btnAdd' value='add another Person' />
<input type='button' id='btnDel' value='delete Delete' /> </br>
<input value="SAVE" name="submit" type="submit">
</form>
<script>
$(document).ready(function () {
$('#btnAdd').click(function () {
var num = $('.clonedSection').length;
var newNum = new Number(num + 1);
var newSection = $('#pq_entry_' + num).clone().attr('id', 'pq_entry_' + newNum);
newSection.children(':first').children(':first').attr('id', 'name_' + newNum).attr('name', 'name_' + newNum).attr('placeholder', 'Person #' + newNum + ' First Name');
newSection.children(':nth-child(2)').children(':first').attr('id', 'faculty_' + newNum).attr('name', 'faculty_' + newNum);
newSection.children(':nth-child(3)').children(':first').attr('id', 'instructor_' + newNum).attr('name', 'instructor_' + newNum);
newSection.insertAfter('#pq_entry_' + num).last();
$('#btnDel').prop('disabled', '');
if (newNum == 5) $('#btnAdd').prop('disabled', 'disabled');
});
$('#btnDel').click(function () {
var num = $('.clonedSection').length; // how many "duplicatable" input fields we currently have
$('#pq_entry_' + num).remove(); // remove the last element
// enable the "add" button
$('#btnAdd').prop('disabled', '');
// if only one element remains, disable the "remove" button
if (num - 1 == 1) $('#btnDel').prop('disabled', 'disabled');
});
$('#btnDel').prop('disabled', 'disabled');
});
</script>
<?php
if(isset($_POST['submit']))
{
$con = new mysqli(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
$i = 1;
while(isset($_POST['name_' . $i]))
{
$person1_faculty = isset($_POST['faculty_' . $i]) ? 1 : 0;
$person1_instructor = isset($_POST['instructor_' . $i]) ? 1 : 0;
$person1_name = $_POST['name_' . $i];
mysqli_query(
$con,
"INSERT INTO person (faculty_role, instructor_role, person_name) VALUES " .
"(" . $person1_faculty . ", " . $person1_instructor . ", '" . $person1_name . "');"
);
$i++;
}
//print_r($_POST);
}
?>