PHP注冊頁面連接mysql數據庫的方法
通過在注冊頁面源碼中添加以下代碼即可連接到MySQL數據庫
<!DOCTYPE html><html><head>
<meta charset="UTF-8">
<title>登錄</title></head><body>
<p>
<b>登錄</b>
<form action="login.php" method="post">
<p><span>用戶名:</span><input type="text" name="user" required="" /></p>
<p><span>密碼:</span><input type="password" name="pass"/ required=""></p>
<p><input type="reset" name=""/> <input type="submit" name=""/></p>
</form>
<a href="register.html">新用戶注冊</a>
</p></body></html>
login.php
<meta charset="utf-8"><?php
$conn = mysqli_connect('localhost','root','password','db_name') or die('數據庫連接失敗');
$conn->query("SET NAMES 'UTF8'");
$user = $_POST['user'];
$pass = $_POST['pass'];
$sql="SELECT * FROM users where user='{$user}' and pass='{$pass}'";
$result=$conn->query($sql);
$row = mysqli_num_rows($result);
//若表中存在輸入的用戶名和密碼,row=1;若表中用戶名不存在或密碼錯誤,則row=0
if($row == 1){
echo $row['user']."登陸成功!";
}
else{
echo"登錄失敗,請重新登錄!";
}?>