我一直在制作登录/注册系统。我遇到的唯一问题是如何使用它以便用户无法注册重复的用户名。我环顾四周,看过很多例子,但我的代码没有用。
我想阻止用户在我的网页上注册时使用相同的用户名。任何帮助表示赞赏。
MySQL表:
Create_User:
ID | upload_img | fullname | username | role | password
1 | <img> | Billy | billyuser | User | test123
2 | <img> | Admin One | adminone | Admin | adminonpass
HTML:
<form action="create_user.php" method="post" enctype="multipart/form-data">
Profile Photo (Optional)<br>
(Max file size:2MB, file type:jpeg, jpg, png)<br><br>
<input type="file" name="fileToUpload" id="fileToUpload" required/><br><br>
Full Name:
<input type="text" name="fname" id="fname" size="40" maxlength="70" placeholder="Please enter your full name"><br><br>
Username:
<input type="text" name="uname" id="uname" size="35" maxlength="62" placeholder="Please enter your username"><br><br>
Role:
Customer<input type="radio" name="role" id="role" value="Customer">
Administrator<input type="radio" name="role" id="role" value="Admin"><br><br>
Password: <br>
<input type="password" name="pword" id="pword" size="35" pattern=".{6,}" placeholder="Password" title="Six or more characters"><br><br>
<input type="password" name="cfmpword" id="cfmpword" size="35" placeholder="Confirm Password"><br><br>
<button type="submit" name="signbtn" style="border:none; background:white; padding:0px">
<img src="button/Sign%20up.png">
</button><br>
<a href="signin.html">Back to Sign In</a>
</form>
腓:
<?php
if (isset($_FILES["fileToUpload"]["name"])) {
$file=$_FILES["fileToUpload"]["name"];
$target="uploadfile/" . $file;
$fname=$_POST['fname'];
$uname=$_POST['uname'];
$role=$_POST['role'];
$pword=$_POST['pword'];
$conn=mysqli_connect("localhost", "root", "" , "SportFacility");
$sql_insert = "INSERT into create_user (upload_img, fullname, username, role, password)
values ('$file', '$fname' , '$uname' , '$role' , '$pword' )";
$result=mysqli_query($conn, $sql_insert);
$allowedType=array("image/jpeg", "image/jpg", "image/png");
if(in_array ($_FILES["fileToUpload"]["type"] ,$allowedType))
{
echo "<script type='text/jscript'>alert('File type is acceptable')</script>";
}
else
{
echo "<script type='text/jscript'>alert('Invalid file type')</script>";
exit();
}
if($_FILES["fileToUpload"]["size"] < 2000000)
{
echo "<script type='text/jscript'>alert('File size is acceptable')</script>";
}
else
{
echo "<script type='text/jscript'>alert('File is too large')</script>";
exit();
}
$directoryfile=move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target);
if($result)
{
header("Location: login.html");
}
else
{
header("Location: register.html");
}
mysqli_close($conn);
}
if (isset($_POST['uname']))
$uname=$_POST['uname'];
$conn=mysqli_connect("localhost", "root", "" , "SportFacility");
$sqluser="SELECT username FROM create_user WHERE username='$uname' ";
$qresult=mysqli_query($conn, $sqluser);
$count=mysqli_num_rows($qresult);
if($count)
{
echo "Username is already taken";
}
else
{ }
?>
答案 0 :(得分:0)
首先,使用UNIQUE
索引或约束来防止数据库中出现重复的用户名。在MySQL中,这是通过以下命令完成的:
ALTER TABLE `tableName` ADD UNIQUE `indexName` ( `columnName` )
这将防止您的数据库处于错误状态(即,具有相同名称的多个用户)。但是你需要在你的PHP代码中主动处理这个问题,以防止用户收到丑陋的MySQL错误消息,你可以通过首先检查是否使用简单的SELECT 1 FROM tableName WHERE column = value
来查看用户名来实现这一点。
最后,永远不要使用字符串连接!使用参数化!有人可以通过在HTML表单中输入SQL命令来轻松破解您的网站!
答案 1 :(得分:0)
如果您想检查重复的用户名,那么在将用户名插入表格之前,您的第一步是检查重复。
你的php文件
$conn=mysqli_connect("localhost", "root", "" , "SportFacility");
if (isset($_FILES["fileToUpload"]["name"])) {
if (isset($_POST['uname']))
$uname=$_POST['uname'];
$sqluser="SELECT username FROM create_user WHERE username='$uname' ";
$qresult=mysqli_query($conn, $sqluser);
$count=mysqli_num_rows($qresult);
if($count > 0)
{
echo "Username is already taken";
}
else
{
$file=$_FILES["fileToUpload"]["name"];
$target="uploadfile/" . $file;
$fname=$_POST['fname'];
$uname=$_POST['uname'];
$role=$_POST['role'];
$pword=$_POST['pword'];
$sql_insert = "INSERT into create_user (upload_img, fullname, username, role, password)
values ('$file', '$fname' , '$uname' , '$role' , '$pword' )";
$result=mysqli_query($conn, $sql_insert);
$allowedType=array("image/jpeg", "image/jpg", "image/png");
if(in_array ($_FILES["fileToUpload"]["type"] ,$allowedType))
{
echo "<script type='text/jscript'>alert('File type is acceptable')</script>";
}
else
{
echo "<script type='text/jscript'>alert('Invalid file type')</script>";
exit();
}
if($_FILES["fileToUpload"]["size"] < 2000000)
{
echo "<script type='text/jscript'>alert('File size is acceptable')</script>";
}
else
{
echo "<script type='text/jscript'>alert('File is too large')</script>";
exit();
}
$directoryfile=move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target);
if($result)
{
header("Location: login.html");
}
else
{
header("Location: register.html");
}
mysqli_close($conn);
}