我是一名初学者,偶然发现了这个制作视频的社交媒体网站并开始研究它。 (https://www.youtube.com/watch?v=EgqVNMTnmDQ&index=36&list=PLA7F9875BD031DC16) 似乎有很多错误,人们评论了解决方案,我尝试修复自己,回到第四个,大多数事情都成功了。
现在我主要在让网站检查服务器上现有的用户名时遇到麻烦,因为即使 mysql 数据库中没有任何内容,当我填写表单并提交时它仍然显示“用户名已获取”。 当然,数据库中不会保存任何内容。
如果你们能帮助我,我将不胜感激
使用代码(复制并粘贴)在 phpmyadmin 上创建表-
如果不存在则创建表
用户
(id
int(11) NOT NULL AUTO_INCRMENT,用户名
varchar(255) NOT NULL,first_name
varchar(255) NOT NULL,last_name
varchar(255) NOT NULL,电子邮件
varchar(255) NOT NULL,密码
varchar(32) NOT NULL,sign_up_date
日期不为空,激活
enum('0', '1') NOT NULL, 主键(id
) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCRMENT=1 ;
-主文件-
<?php
$servername = "localhost";
$username = "root";
$passsword = "";
$databasee = "dbdb";
$con = mysqli_connect($servername, $username, $passsword) or die("Couldn't connect to SQL server");
mysqli_select_db($con, $databasee) or die("Couldn't select DB");
?>
<!doctype html>
<html>
<head>
<title>Findfriends</title>
<link rel="stylesheet" type="text/css" href="./css/style.css">
</head>
<body>
<div class="headerMenu">
<div id="wrapper">
<div class="logo">
<img src="./img/logo.png"></div>
<div class="search_box">
<form action="search.php" method="GET" id="search">
<input type="text" name="q" size="60" placeholder="Search ..."/>
</form>
</div>
<div id="menu">
<a href="#"/>home</a>
<a href="#"/>about</a>
<a href="#"/>sign up</a>
<a href="#"/>sign in</a>
</div>
</div>
</div>
?>
<?php
error_reporting (E_ALL);
ini_set ('display_errors', 1);
$reg = @$_POST['reg'];
$fn = "";
$ln = "";
$un = "";
$em = "";
$em2 = "";
$pswd = "";
$pswd2 = "";
$d = "";
$u_check = "";
$fn = strip_tags(@$_POST['fname']);
$ln = strip_tags(@$_POST['lname']);
$un = strip_tags(@$_POST['username']);
$em = strip_tags(@$_POST['email']);
$em2 = strip_tags(@$_POST['email2']);
$pswd = strip_tags(@$_POST['password']);
$pswd2 = strip_tags(@$_POST['password2']);
$d = date("Y-m-d");
if ($reg) {
if ($fn&&$ln&&$un&&$em&&$em2&&$pswd&&$pswd2) {
$u_check = mysqli_query($con, "SELECT username FROM users WHERE username = '$un' ");
$check = mysqli_num_rows($u_check);
if ($check = 0){
if ($em==$em2) {
if ($pswd==$pswd2) {
if (strlen($un)>25||strlen($fn)>25||strlen($ln)>25) {
echo "The maximum limit for username/first name/lastname in 25 characters!";
}
if (strlen($un)>25||strlen($fn)>25||strlen($ln)>25) {
echo "The maximum limit for username/first name/lastname in 25 characters!";
}
else {
if (strlen($pswd)>30||strlen($pswd)<5) {
echo "Your password must be betwenn 5 and 30 characters long"; }
else {
$pswd = md5($pswd);
$pswd2 = md5($pswd2);
$query = mysqli_query($con, "INSERT INTO users VALUES ('', '$un', '$fn', '$ln', '$em', '$pswd', '$d', '0')");
die("<h2>Welcome to fiendFriends</h2> Login your account to get started...");
}
}
}
} else {echo "email dont match";}
} else {echo "username taken";}
} else {echo "fill in all the fields";}
}
?>
<div style="width: 800px; margin: 0px auto 0px auto;">
<table>
<tr>
<td width="60%" valign="top">
<h2>Sign Up Today!</h2>
</td>
<td width="40%" valign="top">
<h2>Sign Up Below!</h2>
<form action="#" method="POST">
<input type="text" name="fname" size="25" placeholder="First Name" /> <br /><br />
<input type="text" name="lname" size="25" placeholder="Last Name" /> <br /><br />
<input type="text" name="username" size="25" placeholder="Username" /> <br /><br />
<input type="text" name="email" size="25" placeholder="Email" /> <br /><br />
<input type="text" name="email2" size="25" placeholder="Confirm Email" /><br /><br />
<input type="text" name="password" size="25" placeholder="Password" /> <br /><br />
<input type="text" name="password2" size="25" placeholder="Confirm Password" /> <br /><br />
<input type="submit" name="reg" value="Sign Up!">
</form>
</td>
</tr>
</table>
</div>
</body>
</html>
最佳答案
<?php
$servername = "localhost";
$username = "root";
$passsword = "";
$databasee = "csc";
$con = mysqli_connect($servername, $username, $passsword) or die("Couldn't connect to SQL server");
mysqli_select_db($con, $databasee) or die("Couldn't select DB");
?>
<!doctype html>
<html>
<head>
<title>Findfriends</title>
<link rel="stylesheet" type="text/css" href="./css/style.css">
</head>
<body>
<div class="headerMenu">
<div id="wrapper">
<div class="logo">
<img src="./img/logo.png"></div>
<div class="search_box">
<form action="search.php" method="GET" id="search">
<input type="text" name="q" size="60" placeholder="Search ..."/>
</form>
</div>
<div id="menu">
<a href="#"/>home</a>
<a href="#"/>about</a>
<a href="#"/>sign up</a>
<a href="#"/>sign in</a>
</div>
</div>
</div>
<?php
error_reporting (E_ALL);
ini_set ('display_errors', 1);
$reg = @$_POST['reg'];
$fn = "";
$ln = "";
$un = "";
$em = "";
$em2 = "";
$pswd = "";
$pswd2 = "";
$d = "";
$u_check = "";
$fn = strip_tags(@$_POST['fname']);
$ln = strip_tags(@$_POST['lname']);
$un = strip_tags(@$_POST['username']);
$em = strip_tags(@$_POST['email']);
$em2 = strip_tags(@$_POST['email2']);
$pswd = strip_tags(@$_POST['password']);
$pswd2 = strip_tags(@$_POST['password2']);
$d = date("Y-m-d");
if ($reg)
{
$u_check = mysqli_query($con, "SELECT username FROM users WHERE username = '$un' ");
$check = mysqli_num_rows($u_check);
if($check!=0)
{
echo "user name already exiest..";
}
else
{
if ($fn&&$ln&&$un&&$em&&$em2&&$pswd&&$pswd2)
{
$flag=0;
if (strlen($un)>25||strlen($fn)>25||strlen($ln)>25)
{
echo "The maximum limit for username/first name/lastname in 25 characters!";
$flag=1;
}
if ($em!=$em2)
{
echo "email dont match";
$flag=1;
}
if ($pswd!=$pswd2)
{
echo "password and conform password dont match";
$flag=1;
}
else
{
if (strlen($pswd)>30||strlen($pswd)<5)
{
echo "Your password must be betwenn 5 and 30 characters long";
$flag=1;
}
}
if($flag==0)
{
$pswd = md5($pswd);
$query = mysqli_query($con, "INSERT INTO users VALUES ('', '$un', '$fn', '$ln', '$em', '$pswd', '$d', '0')");
die("<h2>Welcome to fiendFriends</h2> Login your account to get started...");
}
}
else{echo "required all field";}
}
}
?>
<div style="width: 800px; margin: 0px auto 0px auto;">
<table>
<tr>
<td width="60%" valign="top">
<h2>Sign Up Today!</h2>
</td>
<td width="40%" valign="top">
<h2>Sign Up Below!</h2>
<form action="#" method="POST">
<input type="text" name="fname" size="25" placeholder="First Name" /> <br /><br />
<input type="text" name="lname" size="25" placeholder="Last Name" /> <br /><br />
<input type="text" name="username" size="25" placeholder="Username" /> <br /><br />
<input type="text" name="email" size="25" placeholder="Email" /> <br /><br />
<input type="text" name="email2" size="25" placeholder="Confirm Email" /><br /><br />
<input type="text" name="password" size="25" placeholder="Password" /> <br /><br />
<input type="text" name="password2" size="25" placeholder="Confirm Password" /> <br /><br />
<input type="submit" name="reg" value="Sign Up!">
</form>
</td>
</tr>
</table>
</div>
</body>
</html>
关于php - 网站未正确检查数据库中的现有用户名或存储数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35519191/