如何使用PHP创建一个简单的登录系统?

时间:2015-07-08 04:32:19

标签: php mysql login login-system

我的大学留下了使用PHP创建登录系统的任务(显然,连接到数据库)。问题是我的注册页面没有保存发送到数据库的任何数据。我不知道为什么会这样。

这是我的代码:

<html>
<head>
<link type="css/text" rel="StyleSheet" href="UniXYZ.css"/>
<meta charset="UTF-8"/>
<title>Registro</title>
</head>
<body>
<?php
session_start();
$host="****";
$username="****"; 
$password="****"; 
$db_name="****"; 
$tbl_name="****"; 
mysqli_connect($host,$username,$password,$db_name)or die("cannot connect");
$email=$_POST['email'];
$password=$_POST['password'];
if (filter_var($email, FILTER_VALIDATE_EMAIL)){
$sql="select 'Email' from 'USER' where 'Email'='$email'";
$result=mysqli_query($sql);
$count=mysqli_num_rows($result);
if($count>0){
echo ("<center><h1><font color='red'>El usuario<br><font color='blue'>".$email."<br><font color='red'>ya existe!<br><a href='index.php'>Inicio</a>");
}else{
$sql="INSERT INTO $tbl_name ('Email','PASS')VALUES('$email','$password')";
$result=mysqli_query($sql);
if($result){
header("location:InicioXYZ.php");
}else{
echo "ERROR MySql";
} 
mysqli_close();
}
}else{
echo"<center><h1><font color='red'>Error el mail ingresado no es v&aacute;lido<br><a href='index.php'>Inicio</a>";
}
?>
<img width="1300px" src="http://i.imgur.com/iOfMyLK.png"/>
<hr width="80%"/>
<div class="login">
<h3 class="h3">Datos Procesados</h3>
<div class="imglog"><img src="http://i.imgur.com/YcUAZHH.png"/></div><br><br><br><br>
<h4><a href="InicioXYZ.php" class="registrate">Pulsa aquí para regresar a la pagina de inicio.</a></h3>
</div>
</body>
</html>

1 个答案:

答案 0 :(得分:2)

错误

  1. mysqli_connect()应分配给变量

    $connect = mysqli_connect($host,$username,$db_password,$db_name)or die("cannot connect");

  2. AS @sean建议更改查询

     $sql="select Email from USER where Email='$email'";
     $sql="INSERT INTO $tbl_name (Email, PASS) VALUES ('$email','$password')";
    
  3. $result=mysqli_query($sql);应与数据库连接($connect

    链接
     $result=mysqli_query($connect,$sql);
    
  4. $password在您的代码中分配两次

    $password="****";//change this
    $sql="INSERT INTO $tbl_name (Email,PASS) VALUES ('$email','$password')";
    
  5. 阅读本文

    1. mysqli_query
    2. mysql_connect
    3. 所以Final Well表单代码是

      <?php
          session_start();
          $host="****";
          $username="****";
          $db_password="****";
          $db_name="****";
          $tbl_name="****";
          $connect = mysqli_connect($host,$username,$db_password,$db_name)or die("cannot connect");
      
          $email=$_POST['email'];
          $password=$_POST['password'];
          if (filter_var($email, FILTER_VALIDATE_EMAIL)){
              $sql="select Email from USER where Email='$email'";
              $result = mysqli_query($connect,$sql);
              $count=mysqli_num_rows($result);
              if($count>0)
              {
                  echo ("<center><h1><font color='red'>El usuario<br><font color='blue'>".$email."<br><font color='red'>ya existe!<br><a href='index.php'>Inicio</a>");
              }else
              {
                  $sql="INSERT INTO $tbl_name (Email,PASS) VALUES ('$email','$password')";
                  $result=mysqli_query($connect,$sql);
                  if($result){
                      header("location:InicioXYZ.php");
                  }else{
                      echo "ERROR MySql";
                  }
                  mysqli_close($connect);
              }
          }else
          {
              echo"<center><h1><font color='red'>Error el mail ingresado no es v&aacute;lido<br><a href='index.php'>Inicio</a>";
          }
      ?>
      

      并注意SQL Injection