网站未正确检查数据库中的现有用户名或存储数据


Website does not properly check for existing username into the database or store data

我是一个初学者,偶然发现了这个制作视频的社交媒体网站并开始研究它。( https://www.youtube.com/watch?v=EgqVNMTnmDQ&index=36&list=PLA7F9875BD031DC16 )似乎有很多错误,人们评论了解决方案,我尝试修复自己回到第四,大多数事情都有效。

现在我主要在使网站检查服务器上的现有用户名时遇到麻烦,因为即使 mysql 数据库中没有任何内容,当我填写表单并提交时,它仍然显示"用户名已占用"。当然,数据库中不会保存任何内容。

如果你们能帮助我,将不胜感激

  • 使用的代码(复制和粘贴)在phpmyadmin上创建表-

    如果不存在,则创建表 usersid int(11) 不为空AUTO_INCREMENT, username 瓦尔查尔(255) 不为空, first_name 瓦尔查尔(255) 不为空, last_name 瓦尔查(255) 不为空, email 瓦尔查尔(255) 不为空, password 瓦尔查尔(32) 不为空, sign_up_date日期不为空, activated 枚举('0', '1') 不为空,主键 ( id )) 引擎=InnoDB 默认字符集=拉丁语1 AUTO_INCREMENT=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>

if ($check = 0 )不正确。您正在以这种方式分配。您想使用==进行比较

请改为执行以下操作:

if ($check == 0 )