重要的是,检查用户在注册时添加的电子邮件,以确认数据库表中不存在该电子邮件。唯一ID。
也是如此我下面使用的代码是我尝试使用的代码,看它是否可以正常工作,在一开始的几分钟后,我能够用他们的10位代码插入新用户。我还能够检查电子邮件是否存在并向用户显示适当的消息,但是我无法检查唯一ID是否存在。但是,由于某种原因,在测试了几分钟并且成功之后,它突然停止了运行,从那时起我就一直坚持下去。到目前为止,它所做的只是插入一个具有唯一ID
[如果数据库中没有记录,它似乎不会改变的记录] ,然后它不会再插入任何记录。之后。我希望有人能够对此提供帮助。谢谢 require 'includes/db_connection.php';
date_default_timezone_set("Europe/London");
for ($randomNumber = mt_rand(0, 9), $i = 1; $i < 10; $i++) {
$randomNumber .= mt_rand(0, 9);
}
if (isset($_POST['create_account'])) :
$errors = [];
$dateAdded_3_mon = strtotime('today + 3 months 20:59:59 P');
$time_formatted = date("Y-m-d H:i:s", $dateAdded_3_mon);
$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
for ($randomNumber = mt_rand(0, 9), $i = 1; $i < 10; $i++) {
$randomNumber .= mt_rand(0, 9);
}
if (!empty($email)) :
$sql = 'SELECT * FROM dates WHERE email = :email';
$stmt = $dsn->prepare($sql);
$stmt->bindValue(':email', $email);
$stmt->execute();
$user = $stmt->fetchColumn();
// $stmt->closeCursor();
if ($user) :
$errors['exists'] = $email . ' - already exists';
else :
$sql = 'INSERT INTO dates (date_until, email, unique_user_id) VALUES (:date_until, :email, :unique_user)';
$stmt = $dsn->prepare($sql);
$stmt->bindValue(':date_until', $time_formatted);
$stmt->bindValue(':email', $email);
$stmt->bindValue(':unique_user', $randomNumber);
$stmt->execute();
$stmt->closeCursor();
endif; // End of $user
else :
$errors['email'] = "Email is required";
endif; // End of !empty($email)
endif; // End of isset($_POST['create_account'])```
<?php
// This function will return a random
// string of specified length
function random_strings($length_of_string)
{
// String of all alphanumeric character
$str_result = '0123456789';
// Shufle the $str_result and returns substring
// of specified length
return substr(str_shuffle($str_result),
0, $length_of_string);
}
// This function will generate
// Random string of length 10
echo random_strings(10);
echo "\n";
// This function will generate
// Random string of length 8
echo random_strings(8);
?>