我有一个注册,每个注册的人都会得到一个唯一的用户ID。该用户ID不应重复,以便每个人都有唯一的ID。然后将用户ID插入2个不同的表中。但是,每次有新用户在数据库中注册用户ID的号码时,总是返回0。
这是我的代码:
$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");
}
}
编辑
最初,我确实使用了自动递增功能,但这对我不起作用。问题是,是的,它会为Users表自动递增,但是我还需要将Users表中的SAME用户ID也插入到Dancers表中。
该舞者表需要具有自己的唯一ID和用户ID。这很重要,因为用户可以删除舞者及其页面,所以在发明删除某人的过程中,自动递增是行不通的。 因此,最初,当我为用户使用自动增量功能时,它会如何插入,而删除了3个。
用户表
user-id | user-type | name | etc |
1 | parent | Ashley | etc |
2 | parent | Liz | etc |
4 | parent | Chris | etc |
舞者桌
id | user-id | name | dancer name |
1 | 0 | Ashley | Ben |
2 | 0 | Liz | Tom |
3 | 0 | Chris | Meg |
但这应该是这样:
用户表
user-id | user-type | name | etc |
1 | parent | Ashley | etc |
2 | parent | Liz | etc |
4 | parent | Chris | etc |
舞者桌
id | user-id | name | dancer name |
1 | 1 | Ashley | Ben |
2 | 2 | Liz | Tom |
3 | 4 | Chris | Meg |
使用答案编辑2
我在达米安·帕勃罗·冈萨雷斯(DamiánPabloGonzález)给出的答案中使用了接近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");
}
}
答案 0 :(得分:1)
对于唯一的随机数,您可以使用以下功能。您可以根据需要传递长度。
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;
}
答案 1 :(得分:0)
(我认为最好)正如Wreigh所建议的那样,最好使用自动递增字段,它会为您覆盖所有内容,因此您不必担心这些事情。
为此,请尝试以下MySQL命令。 NSString *const AVCaptureSessionPresetHigh;
NSString *const AVCaptureSessionPresetMedium;
NSString *const AVCaptureSessionPresetLow;
。之后,您只需插入其他字段,user_id将始终是唯一的:
<%= form_for @moderator, url: 'path for your create action for moderator controller' do |u| %>
插入下一个可用的ID:
ALTER TABLE Users MODIFY COLUMN user_id INT auto_increment
看看我做了什么:我使用$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);
,而没有$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。这要求user_id为数字。
(如我所见,user_id用引号括起来,也许是一个字符串字段)
1)如果user_id是字符串,请使用uniqid()
生成一个非常相似的唯一ID。根据当前时间(以微秒为单位)获取带前缀的唯一标识符。
2)然后,请确定是否未使用它。
INSERT (fields) SELECT
它将一直尝试直到id真正唯一为止。这要求user_id是字符串字段,例如varchar,因为uniqid()返回字母和数字。
(类似于C,但为数字)
1)如果user_id是整数,则使用rand()
生成一个非常相似的唯一ID。rand —生成随机整数
2)然后,请确定是否未使用它。
VALUES
会的