【发布时间】:2016-08-15 17:10:34
【问题描述】:
该脚本已经可以正常工作,但我想插入一个仅在用户名尚未使用时才允许的命令。
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();
}
【问题讨论】:
-
在将其插入数据库之前,请检查是否使用发布的用户名进行选择,如果查询返回值 0,您可以插入,否则会出现错误消息而不插入。
-
select count(*) from YOUR_TABLE where YOUR_CONDITION -
在 else 部分中,您在从表中选择数据后将其设置为 if else。
if($count==0){// Insert Coding goes here } else {//Error message goes here}. -
你能在我现有的代码中插入你的代码吗?谢谢