2
我有一个动态行,最终用户可以在需要时附加额外的行。在插入之前,我还通过查询现有数据表将重复项限制在数据库中。如果用户输入序列号001然后提交,并尝试提交另一个序列号001,则系统返回错误。我现在遇到的问题是,如果用户同时在两个不同的行上输入序列号001,则限制将被忽略。有没有办法来防止这个PHP/AJAX?或者这会落在js上?请网站一些简单的例子。提前致谢!发布数据前防止重复条目
$(document).ready(function(){
\t var counter = 2;
$('.add-row').click(function() {
$(".item_form").append('<tr><td><input type="text" name="serialnoa[]" placeholder="serial no.' +
\t counter + '"/></td></tr>');
\t \t \t counter++; });
$('.del-row').click(function() {
if($(".item_form tr").length != 2)
{
$(".item_form tr:last-child").remove();
counter--;
}
else
{
alert("You cannot delete first row");
}
\t });
});
</script>
<!DOCTYPE html>
<html class="no-js" lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="_css/inventory.css?v=<?=time();?>">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
</head>
<body>
<table class="item_form">
<tr>
<th>serial no.</th>
</tr>
<tr>
<td><input type="text" placeholder="serial no.1" name="serialnoa[]"></td>
</tr>
</table>
<table>
<tr>
\t <td><a href="#" class="add-row"><div>+ Row</div></td>
\t <td><a href="#" class="del-row"><div>- Row</div></td>
\t \t \t \t \t \t \t \t \t
</tr>
</table>
</body>
</html>
您是否尝试在数据库中为该列设置唯一索引? –
如果我这样做,它会阻止它是的,但如果我没有错误它不会返回一个错误。所以最终用户不会知道发生了什么。他们将很可能认为一切正常,而不知道他们在插入第二个项目编号犯了一个错误。 – bongoloids
@MohitBhardwaj谢谢你是唯一能够提出任何建议的人。我想我找到了一个解决方案。使用count(array_unique($阵列))<数($阵列) – bongoloids