如何将我的登录/注册与MySQL数据库连接?

时间:2019-01-30 15:09:43

标签: html mysql

我开始编码我的网站,我们将用它与朋友开展业务,但我不知道如何将“登录/注册”页面连接到mySQL数据库(在具有Apache服务器和MySQL服务器的xampp控制面板上运行)。 任何解决方案都将有所帮助。谢谢。

登录:

 <link rel="stylesheet" type="text/css" href="loginstyle.css">
<form action="action_page.php">
<div class="container">
    <label for="uname"><b>Username</b></label>
    <input type="text" placeholder="Enter Username" name="uname" required>

    <label for="psw"><b>Password</b></label>
    <input type="password" placeholder="Enter Password" name="psw" required>

    <button type="submit">Login</button>
    <label>
      <input type="checkbox" checked="checked" name="remember"> Remember me
    </label>
  </div>

  <div class="container" style="background-color:#f1f1f1">
    <button type="button" class="cancelbtn">Cancel</button>
    <span class="psw">Forgot <a href="#">password?</a></span>
  </div>
</form>

注册:

<link rel="stylesheet" type="text/css" href="registerstyle.css">
<form action="action_page.php">
  <div class="container">
    <h1>Register</h1>
    <p>Please fill in this form to create an account.</p>
    <hr>

    <label for="email"><b>Email</b></label>
    <input type="text" placeholder="Enter Email" name="email" required>

    <label for="psw"><b>Password</b></label>
    <input type="password" placeholder="Enter Password" name="psw" required>

    <label for="psw-repeat"><b>Repeat Password</b></label>
    <input type="password" placeholder="Repeat Password" name="psw-repeat" required>
    <hr>

    <p>By creating an account you agree to our <a href="#">Terms & Privacy</a>.</p>
    <button type="submit" class="registerbtn">Register</button>
  </div>

  <div class="container signin">
    <p>Already have an account? <a href="#">Sign in</a>.</p>
  </div>
</form>

1 个答案:

答案 0 :(得分:1)

您将需要添加PHP

    Connect.php
<?php
/* Database connection settings */
$host = '';
$user = '';
$pass = '';
$db = '';
$mysqli = new mysqli($host,$user,$pass,$db) or die($mysqli->error);
?>

将其添加到其他页面,扩展名为.php,并将操作文件更改为Login.php

的login.php     

session_start();
include("db.php");
if (isset($_POST['uname']) && isset ($_POST['pse'])) { 
$username = $mysqli->escape_string($_POST['pse']);
$result = $mysqli->query("SELECT * FROM TABLE WHERE username='$username'");

if ( $result->num_rows == 0 ){
    $_SESSION['message'] = "User with that email doesn't exist!";
       echo '<script language="javascript">';
echo 'alert("'.$_SESSION['message'].'")';
echo '</script>';
    }

else { 
    $user = $result->fetch_assoc();

if ( password_verify($_POST['Password'], $user['password']) ) {


    $_SESSION['email'] = $user['email'];


    $_SESSION['active'] = $user['active'];


    $_SESSION['logged_in'] = true;

    header("location: System/index.php");
}
else {
    $_SESSION['message'] = "You have entered wrong password!";
   echo '<script language="javascript">';
echo 'alert("'.$_SESSION['message'].'")';
echo '</script>';
    }
}
}
?>

将其添加到其他页面,扩展名为.php,并将操作文件更改为Register.php

Register.php

    <?php 

        session_start();
        include("db.php");
    if (isset($_POST['email']) && isset ($_POST['psw']) && isset ($_POST['psw-repeat'])){


    $email = $mysqli->escape_string($_POST['email']);
    $password = $mysqli->escape_string(password_hash($_POST['password'], PASSWORD_BCRYPT));
    $password-repeat= $mysqli->escape_string(password_hash($_POST['password'], PASSWORD_BCRYPT));


    $result = $mysqli->query("SELECT * FROM TABLE WHERE email='$email'") or die($mysqli->error());
    if ( $result->num_rows > 0 ) {

        $_SESSION['message'] = 'User with this email already exists!';
          header("location: error.php");


    }

if( $password !== $password-repeat){

        $_SESSION['message'] = 'Password dont match';
          header("location: error.php");
}
    else { 


        $sql = "INSERT INTO TABLE( email, password)"
            . "VALUES ('$first_name','$last_name','$email','$password', '$hash')";
header("location: index.php");
}

?>