使用PHP MYSQLI创建注册和登录页面

时间:2015-03-31 11:24:18

标签: php login mysqli registration

以下代码不会将记录插入数据库。单击表单上的注册按钮后,它表示我没有输入任何字段。你能否提出任何可以使我的代码能够运作的建议,谢谢。

if(isset($_POST['register']))
{
    $user_name=$_POST['name'];
    $user_pass=$_POST['pass'];
    $user_email=$_POST['email'];

    $query="SELECT * FROM `user` WHERE user_email='$user_email'";
    $result=mysqli_query($dbcon,$query);

    if(mysqli_num_rows($result)>0){
        echo "<script>alert('Email already exist in our database, Please try another one!')</script>";
    }

    if (!$_POST['user_name']) { 
        $errEnterUserName = 'Please enter username';
    }

    if (!$_POST['user_pass']) {
        $errEnterPass = 'Please enter password';
    }

    if (!$_POST['user_email']) { 
        $errEnterEmail = 'Please enter email';
    }
    else {
        $insert_app=("INSERT INTO `user`(user_name,user_pass,user_email) 
         VALUE ('$user_name','$user_pass','$user_email')"); 

        $run_insert = mysqli_query($dbcon,$insert_app);

        if($run_insert) {
            echo "succesfull";
        }
    }
}

2 个答案:

答案 0 :(得分:0)

请更改此内容:可能这些更改应该对您有用

if (!$_POST['name']) {   // instead of $_POST['user_name']
    $errEnterUserName = 'Please enter username';
    }

      if (!$_POST['pass']) {  // instead of $_POST['user_pass']
        $errEnterPass = 'Please enter password';
    }

     if (!$_POST['email']) {  // instead of $_POST['user_email']
        $errEnterEmail = 'Please enter email';
    }

答案 1 :(得分:0)

<?php
if(isset($_POST['btn-signup']))
{
 $uname = $MySQLi_CON->real_escape_string(trim($_POST['name']));
 $email = $MySQLi_CON->real_escape_string(trim($_POST['email']));
 $upass = $MySQLi_CON->real_escape_string(trim($_POST['pass']));

 $new_password = password_hash($upass, PASSWORD_DEFAULT);

 $check_email = $MySQLi_CON->query("SELECT email FROM user WHERE user_email='$email'");
 $count=$check_email->num_rows;

 if($count==0){


  $query = "INSERT INTO user(user_name,user_email,user_pass) VALUES('$uname','$email','$new_password')";


  if($MySQLi_CON->query($query))
  {
   $msg = "successfully registered !";
  }
  else
  {
   $msg = "error while registering !";
  }
 }
 else{
  $msg = "sorry email already taken !";
 }

 $MySQLi_CON->close();
}
?>

这篇文章可能对您有所帮助

Login Signup using PHP MySQLi