Php 如何在数据库中插入随机数不重复
我有一个注册,每个注册的人都有一个唯一的用户id。这个用户id不应该重复,这样每个人都有一个唯一的id。然后用户id被插入到两个不同的表中。但是,每当一个新人在数据库中注册用户id的号码时,总是返回0 这是我的密码:Php 如何在数据库中插入随机数不重复,php,html,mysqli,Php,Html,Mysqli,我有一个注册,每个注册的人都有一个唯一的用户id。这个用户id不应该重复,这样每个人都有一个唯一的id。然后用户id被插入到两个不同的表中。但是,每当一个新人在数据库中注册用户id的号码时,总是返回0 这是我的密码: $userid = ''; function randomDigits($length){ $numbers = range(0,10000000); shuffle($numbers); for($i = 0; $i < $length; $i++){ gl
$userid = '';
function randomDigits($length){
$numbers = range(0,10000000);
shuffle($numbers);
for($i = 0; $i < $length; $i++){
global $digits;
$userid .= $numbers[$i];
}
return $userid;
}
$query = "INSERT INTO Users (user_id, user_type, name, lastname, email, phone, address, apt, city, state, zip, username, password) VALUES ('$userid','parent','$name', '$lastname', '$email', '$phone', '$address', '$apt', '$city', '$state', '$zip', '$username', '$hash')";
$result = mysqli_query($query);
$sql = "INSERT INTO dancers (user_id, name, dancer_name, dancer_middlename, dancer_lastname, dancer_age, dancer_dob, dancer_number, school_code, teacher_name, class_location, date_enrolled, date_comp) VALUES ('$userid', '$name', '$dancer_name', '$dancer_middlename', '$dancer_lastname', '$dancer_age', '$dancer_dob', '$dancer_number', '$schoolcode', '$teacher', '$location', '$date_enrolled', '$date_comp')";
$result2 = mysqli_query($sql);
if(!$result = $con->query($query)){
die('there was an error running query [' . $con->error . ']');
}else {
// header("location: thankyou.html");
}
if(!$result2 = $con->query($sql)){
die('there was an error running query [' . $con->error . ']');
}else {
header("location: thankyou.html");
}
}
舞者桌
但这应该是:
用户表
舞者桌
用答案编辑2
我在达米安·巴勃罗·冈萨雷斯的回答中使用了D
以下是正确的代码:
$userId = rand(1,9999999);
$check_userId ="select count(*) count from Users where user_id = " .
$userId;
while ($row['count'] > 0);
$query = "INSERT INTO Users (user_id, user_type, name, lastname, email, phone, address, apt, city, state, zip, username, password) VALUES ('$userid','parent','$name', '$lastname', '$email', '$phone', '$address', '$apt', '$city', '$state', '$zip', '$username', '$hash')";
$result = mysqli_query($query);
$sql = "INSERT INTO dancers (user_id, name, dancer_name, dancer_middlename, dancer_lastname, dancer_age, dancer_dob, dancer_number, school_code, teacher_name, class_location, date_enrolled, date_comp) VALUES ('$userid', '$name', '$dancer_name', '$dancer_middlename', '$dancer_lastname', '$dancer_age', '$dancer_dob', '$dancer_number', '$schoolcode', '$teacher', '$location', '$date_enrolled', '$date_comp')";
$result2 = mysqli_query($sql);
if(!$result = $con->query($query)){
die('there was an error running query [' . $con->error . ']');
}else {
// header("location: thankyou.html");
}
if(!$result2 = $con->query($sql)){
die('there was an error running query [' . $con->error . ']');
}else {
header("location: thankyou.html");
}
}
接近A
在我看来,正如雷伊建议的那样,最好使用一个自动递增字段,它为您涵盖了所有内容,所以您不必担心这些事情
为此,请尝试此MySQL命令。ALTER TABLE Users修改列user\u id INT自动增量。之后,您只需插入其他字段,用户id将始终是唯一的:
$query = "INSERT INTO Users (user_type, name, lastname, email, phone, address, apt, city, state, zip, username, password) VALUES ('parent','$name', '$lastname', '$email', '$phone', '$address', '$apt', '$city', '$state', '$zip', '$username', '$hash')";
$result = mysqli_query($query);
方法B:
插入下一个可用的id:
$query = "INSERT INTO Users (user_id, user_type, name, lastname, email, phone, address, apt, city, state, zip, username, password) SELECT max(user_id)+1,'parent','$name', '$lastname', '$email', '$phone', '$address', '$apt', '$city', '$state', '$zip', '$username', '$hash' from Users";
$result = mysqli_query($query);
看看我做了什么:我使用了插入字段选择,没有值。它将在读取的同时插入id。这要求用户id为数字
方法C
正如我看到的,您使用引号表示用户id,可能是一个字符串字段
1如果用户id为字符串,则使用
基于当前时间(以微秒为单位)获取带前缀的唯一标识符
2然后,为了确定,检查它是否未被使用
do {
$uniq = uniqid();
$query ="select count(*) count from Users where user_id = '" . $uniq ."'";
//execute and do a fetch
} while ($row['count'] > 0);
do {
$uniq = rand(1,999999);
$query ="select count(*) count from Users where user_id = " . $uniq;
//execute and do a fetch
} while ($row['count'] > 0);
它将继续尝试,直到id真正唯一为止。这要求user_id是字符串字段,就像varchar一样,因为uniqid返回字母和数字
方法D
与C类似,但为数字
1如果用户_id为整数,则使用
rand-生成一个随机整数
2然后,为了确定,检查它是否未被使用
do {
$uniq = uniqid();
$query ="select count(*) count from Users where user_id = '" . $uniq ."'";
//execute and do a fetch
} while ($row['count'] > 0);
do {
$uniq = rand(1,999999);
$query ="select count(*) count from Users where user_id = " . $uniq;
//execute and do a fetch
} while ($row['count'] > 0);
它将为您可以使用下面函数的唯一随机数设置。您可以根据需要传递长度
public static function generateUniqueId($length = 7) {
$salt = "abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ23456789";
srand(); // start the random generator
$userId = ""; // set the inital variable
for ($i = 0; $i < $length; $i++) { // loop and create userId
$userId .= substr($salt, rand() % strlen($salt), 1);
}
return $userId;
}
为什么不使用自动递增id?请查看我的EditA方法不适用于我,因为我在编辑的问题中提到了这个原因。方法B与方法A不起作用的原因相同。因为id是一个整数,所以我使用了方法D,它对我非常有效。因此,我认为方法C也能很好地工作。非常感谢。
do {
$uniq = rand(1,999999);
$query ="select count(*) count from Users where user_id = " . $uniq;
//execute and do a fetch
} while ($row['count'] > 0);
public static function generateUniqueId($length = 7) {
$salt = "abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ23456789";
srand(); // start the random generator
$userId = ""; // set the inital variable
for ($i = 0; $i < $length; $i++) { // loop and create userId
$userId .= substr($salt, rand() % strlen($salt), 1);
}
return $userId;
}