我正在尝试将多个值插入一个表中。首先,我让用户选择instructors
的数量。根据初始选择,jquery显示与其对应的输入显示数。因此,如果用户选择两个教师,则会出现两个输入字段。我想两个输入字段的值插入到mysql数据库中。现在,没有插入任何东西,我不确定原因。 SITE
<script type="text/javascript">
$(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', 'inst_fname_' + newNum).attr('name', 'inst_fname_' + newNum).attr('placeholder', 'Instructor #' + newNum + ' First Name');
newSection.children(':nth-child(2)').children(':first').attr('id', 'inst_lname_' + newNum).attr('name', 'inst_lname_' + 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>
</head>
<body>
<form action="index.php" method="post">
Instructor:
<ul id="pq_entry_1" class="clonedSection">
<li>
<input id="inst_fname_1" name="inst_fname_1" placeholder="Instructor #1 - First Name" type="text" />
</li>
<li>
<input id="inst_lname_1" name="inst_lname_1" placeholder="Last Name" type="text" />
</li>
</ul>
<input type='button' id='btnAdd' value='add another instructor' />
<input type='button' id='btnDel' value='delete instructor' />
</br>
<input value="SAVE" type="submit">
<?php
$db_con = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$db_insert3 = "INSERT INTO instructor (`instructor_fname`, `instructor_lname`)";
$db_insert3 .= " VALUES ('" . $_POST['inst_fname_1'] ."', '" . $_POST['inst_lname_1'] ."')";
mysqli_query($db_con, $db_insert3);
?>
</form>
答案 0 :(得分:1)
将输入值存储在数组中。就像这样:
<input id="inst_fname_1" name="inst_fname_1[]" placeholder="Instructor #1 - First Name" type="text" />
<input id="inst_lname_1" name="inst_lname_1[]" placeholder="Last Name" type="text" />
在服务器端,使用foreach
从数组中检索值并将其插入数据库。
foreach($_POST['inst_fname_1'] as $fname){
// insert query
}
注意:您的插入查询应位于表单之外,并使用isset
检查表单是否已提交。
更新了 html:
<form action="" method="post">
Instructor:
<ul id="pq_entry_1" class="clonedSection">
<li>
<input id="inst_fname_1" name="inst_fname_1[]" placeholder="Instructor #1 - First Name" type="text" />
</li>
<li>
<input id="inst_lname_1" name="inst_lname_1[]" placeholder="Last Name" type="text" />
</li>
</ul>
<input type='button' id='btnAdd' value='add another instructor' />
<input type='button' id='btnDel' value='delete instructor' />
</br>
<input value="SAVE" type="submit" name="submit">
</form>
注意:此处我将表单action
字段留空,因为我的php代码位于同一页面的顶部。
更新了 javascript:
newSection.children(':first').children(':first').attr('id', 'inst_fname_' + newNum)
.attr('name', 'inst_fname_1[]').attr('placeholder', 'Instructor #' + newNum + ' First Name');
newSection.children(':nth-child(2)').children(':first').
attr('id', 'inst_lname_' + newNum).attr('name', 'inst_lname_1[]');
更新了 php :
<?php
if(isset($_POST['submit'])){
$db_con = new mysqli(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
if ($db_con->connect_errno) {
printf("Connect failed: %s\n", $db_con->connect_error);
exit();
}
for($i=0;$i<count($_POST['inst_fname_1']);$i++){
$db_insert3 = "INSERT INTO instructor (instructor_fname,instructor_lname)
VALUES ('".$_POST['inst_fname_1'][$i]."','".$_POST['inst_lname_1'][$i]."')";
$db_con->query($db_insert3);
}
}
?>