该脚本已经工作正常,但我想插入一个仅在用户名尚未使用时才允许的命令。
if (isset($_POST['submit'])) {
$firstname = htmlentities($_POST['firstname'], ENT_QUOTES);
$lastname = htmlentities($_POST['lastname'], ENT_QUOTES);
$position = htmlentities($_POST['position'], ENT_QUOTES);
$username = htmlentities($_POST['username'], ENT_QUOTES);
$password = htmlentities($_POST['password_two'], ENT_QUOTES);
$uniqid = uniqid('', true);
if ( $firstname == '' || $lastname == '' || $position == '' || $username == '' || $password == '') {
$error = 'ERROR: Please fill in all required fields!';
renderForm($error, $firstname, $lastname, $position, $username, $password);
} else {
if ($stmt = $connection->prepare("INSERT INTO employee (uniqid, firstname, lastname, position, username, password) VALUES (?, ?, ?, ?, ?, ?)")) {
$stmt->bind_param("ssssss", $uniqid, $firstname, $lastname, $position, $username, $password);
$stmt->execute();
$stmt->close();
} else {
echo "ERROR: Could not prepare SQL statement.";
}
header("Location: regemployee.php");
}
} else {
renderForm();
}
最佳答案
使用户名在数据库中唯一,然后当您尝试再次将相同的用户名插入数据库时,插入将出错。
或者,您可以执行 SELECT * FROMEmployee WHERE username = ?
并检查结果是否 > 0。
然后你就会知道它已经存在。
关于php - HTML PHP - 如何检查用户名是否已经存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38952254/