这里有趣的问题。Select2 - 写入多重选择到数据库中,双记录
我正在使用Select2来允许用户填充具有多个值的选择框,然后将其写入数据库表中。但是,在将值插入表中时,我注意到选择字段的最后一个值始终写入两次。我怀疑foreach
循环有问题,但不知道如何解决这个问题。
选择字段是单击SAVE按钮后模式的一部分通过AJAX发送到我的ajax.php文件,其中插入处理。同样的方法在整个网站上多次部署没有问题,只有在其字段为multiple
时才会出现问题。
HTML
<!-- Department -->
<label>Department Name:</label>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-bars"></i></span>
<input type="text" class="form-control" id="addDeptName" name="addDeptName" />
</div>
<!-- /.Department -->
<p> </p>
<!-- Positions -->
<label>Department Positions:</label>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-briefcase"></i></span>
<select class="form-control select2" style="width:100%;" id="addDeptPositions" name="addDeptPositions" multiple>
<option value="Option1">Option1</option>
<option value="Option2">Option2</option>
<option value="Option3">Option3</option>
<option value="Option4">Option4</option>
</select>
</div>
<!-- /.positions -->
JS
// ADD NEW RECORD TO DATABASE
$("#NewDepartmentButton").click(function() {
$("#addDeptName").focus();
// check that input fields are not empty
if($("#addDeptName").val()!="" && $("#addDeptPositions").val()!="") {
$.ajax({
url: "../../plugins/MySQL/ajax_action.php",
type: "POST",
//async: true,
data: { action:"new_department",Department_Name:$("#addDeptName").val(),Department_Positions:$("#addDeptPositions").val()}, // form data to post goes here as a json object
dataType: "html",
success: function(data) {
$('#department_output').html(data);
drawVisualization();
},
});
} else {
//notify the user they need to enter data
alert("Please enter a valid Department Name.");
return;
}
// close modal and refresh page
$('#NewDepartmentModal').modal('hide');
setTimeout(function(){location.reload()}, 2000);
// Reload Datatables
//$('#department_table').DataTable().ajax.reload();
// refresh entire website
//location.reload();
return;
});
PHP
if(isset($_POST['action']) && ($_POST['action']=='new_department')) {
// include connection details
include 'connect_db.php';
//Open a new connection to the MySQL server
$db = new mysqli($dbhost,$dbuser,$dbpass,$dbname);
//Output any connection error
if ($db->connect_error) {
die('Error : ('. $db->connect_errno .') '. $db->connect_error);
}
// get variables and sanitize
$addDeptName = mysqli_real_escape_string($db,$_POST['Department_Name']);
$addDeptPositions = $_POST['Department_Positions'];
// create new record
foreach ($addDeptPositions as $c) {
$sql = "INSERT INTO `qci_departments` (`Department`,`Positions`) VALUES ('".$addDeptName."', '".mysqli_real_escape_string($db, $c)."')";
$db->query($sql);
}
if (!$db->query($sql)) {
echo "
<div class=\"alert alert-danger alert-dismissible\">
<button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-hidden=\"true\">×</button>
<h4><i class=\"icon fa fa-ban\"></i> Error!</h4>
There was an error while excuting this query.<br />
(" . $db->errno . ") " . $db->error . "
</div>";
}
echo "
<div class=\"alert alert-success alert-dismissible\">
<button type=\"button\" class=\"close\" data-dismiss=\"alert\" aria-hidden=\"true\">×</button>
<h4><i class=\"icon fa fa-check\"></i> Alert!</h4>
Success, record updated successfully. Refreshing database now...
</div>";
//close connection
$db->close();
}
编辑:选择值看起来 问题似乎涉及到,因为这个数组提交的是什么样的:
Array ([0] => Accounting Manager [1] => Accounts Receivable Officer) Array ([0] => Accounting Manager [1] => Accounts Receivable Officer)
如果位置“客户经理”和“应收账款官”,从多个领域的选择
编辑2: addded一个return;
的JS代码
打印阵列'$ addDeptPositions'并粘贴问题。如果添加,也粘贴select2 jQuery代码。 – RJParikh
如果您使用select with multiple选项,您是否认为'name =“addDeptPositions”'应该是'name =“addDeptPositions []”' –
@HappyCoding当通过'