我有一个名为Customers with Unique ID的数据库,所以每次从php插入数据库时,我都需要从一个随机函数为新客户生成一个新的ID,但我需要检查生成的ID是否存在于数据库中。
这是php的随机函数:
function randomDigits($length){
$digits = "";
$numbers = range(0,9);
shuffle($numbers);
for($i = 0;$i < $length;$i++)
$digits .= $numbers[$i];
return $digits;
}
我试着通过这样的随机长度:
$cusid = randomDigits(10);
$generate_customer_id = "";
$query_customer_id = dbQuery("SELECT customer_id FROM customers WHERE customer_id = '$cusid'");
$count_customer_id = $query_customer_id->rowCount();
if($count_customer_id == 1)
{
$generate_customer_id = randomDigits(10);
}
else
{
$generate_customer_id = $cusid;
}
$query = dbQuery("INSERT INTO customers(`customer_id`,`username`,`password`,`email`,`first_name`,`last_name`,`dob`,`id_card`,`family_book_id`,`address`,`district`,`province`,`phone`,`mobile`,`father_name`,`mother_name`,`bank_account_no`,`bank_account_name`) VALUES('$generate_customer_id','$username','$password','$email','$firstname','$lastname','$dob','$idcard','$familybook','$address','$district','$province','$homephone','$mobilephone','$fathername','$mothername','$bankno','$bankname')");
if($query == true)
{
$insert_referrer = dbQuery("INSERT INTO referrers (`parent_id`, `customer_id`, `category_id`, `created`,`modified`) VALUES(NULL, '$generate_customer_id', 2, NOW(), NOW())");
if($insert_referrer == true)
{
echo "OK";
}
else
{
echo "There is an error please check your information and try again referrers!";
}
}
else
{
echo "There is an error please check your information and try again customers!";
}
但是数据从我的AJAX函数返回,如果返回错误,我需要多次点击保存客户按钮,然后我会返回成功,然后数据成功保存到数据库。
但我希望用户只点击一次,然后数据就会插入。
如何实现这一点,请帮助!
EDiT:这是AJAX函数:
$.ajax({
type: "POST",
url: "../ajax/admin/sales/ajaxAddSalesParent.php",
data: dataString,
dataType: "html",
cache: false,
success: function(responseMessage){
if(responseMessage == "OK")
{
swal({
title: "Success",
text: "Your requested has been completed!",
type: "success",
showCancelButton: false,
confirmButtonClass: "btn-success",
closeOnConfirm: false
},
function(isConfirm) {
if (isConfirm) {
window.location.href = "sales_members.php";
}
});
}
else
{
$.alert({
title: '<h3 style="color: red;">Error!</h3>',
content: responseMessage,
confirm: function(){
}
});
}
}
});
如果您在数据库上运行以下选择:
select floor(1000000000 + rand() * 8999999999) as rndID,
(select customer_id from customers where customer_id=rndID) as uniq;
那么您将收到一个10位的随机数,希望uniq为NULL。
所以实际上只需要在while循环中执行这个查询,直到字段uniq
为NULL,并且在rndID
字段中有唯一的随机数。
我并没有你们的测试环境,但我认为以下解决方案应该有效:
// get random customer id
$generate_customer_id = 0;
do {
if ($result = dbQuery("select floor(1000000000 + rand() * 8999999999) as rndID, (select customer_id from customers where customer_id=rndID) as uniq")) {
if ($row = mysql_fetch_assoc($result)) {
if ($row['uniq']!=$row['rndID'])
$generate_customer_id = $row['rndID'];
}
}
} while($generate_customer_id == 0);
$query = dbQuery("INSERT INTO customers(`customer_id`,`username`,`password`,`email`,`first_name`,`last_name`,`dob`,`id_card`,`family_book_id`,`address`,`district`,`province`,`phone`,`mobile`,`father_name`,`mother_name`,`bank_account_no`,`bank_account_name`) VALUES('$generate_customer_id','$username','$password','$email','$firstname','$lastname','$dob','$idcard','$familybook','$address','$district','$province','$homephone','$mobilephone','$fathername','$mothername','$bankno','$bankname')");
if($query == true)
{
$insert_referrer = dbQuery("INSERT INTO referrers (`parent_id`, `customer_id`, `category_id`, `created`,`modified`) VALUES(NULL, '$generate_customer_id', 2, NOW(), NOW())");
echo ($insert_referrer) ? "OK" : "There is an error please check your information and try again referrers!";
}
else
{
echo "There is an error please check your information and try again customers!";
}