gpt4 book ai didi

php - 生成随机函数的 id 后,如何使用 PHP 循环检查 mysql 数据库中是否存在该 ID?

转载 作者:行者123 更新时间:2023-11-29 22:41:22 25 4
gpt4 key购买 nike

我有一个名为“具有唯一 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函数返回数据,如果返回错误我需要多次点击保存客户按钮然后我会返回成功然后数据成功保存到数据库。

但我希望用户只单击一次,然后数据就会插入。

如何实现这个,请帮忙!

编辑:这是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!";
}

关于php - 生成随机函数的 id 后,如何使用 PHP 循环检查 mysql 数据库中是否存在该 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29328689/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com