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