1. 程式人生 > >html頁面通過http訪問mysql數據庫中的內容,實現用戶登錄的功能

html頁面通過http訪問mysql數據庫中的內容,實現用戶登錄的功能

默認 存在 apache ron username 問題 登錄界面 ble rom

需求:

  通過html編寫用戶登錄頁面,頁面內容包括用戶名、密碼和登錄按鈕,點擊登錄後訪問login.php文件,使用按鈕默認的submit提交用戶名和密碼,在login.php中訪問mysql數據庫,查找是否存在用戶名和密碼均相同的項,然後通過判斷$mysqli->num_rows是否為0,為0時表示數據庫中沒有相同的數據,通過echo向html頁面返回‘失敗’的信息,反之則返回‘成功’

源碼:

login.html

<!DOCTYPE html>
<html>
<head>
<title>用戶登錄界面</title>
<meta charset="UTF-8" />
</head>
<body>
<filedset>
<legend>用戶登錄界面</legend>
<form id=‘myform‘ action=‘login.php‘ method=‘post‘>
<table>
<tr>
<td>用戶名:</td>
<td><input type=‘text‘ id=‘userName‘ name=‘username‘></td>
</tr>
<tr>
<td>密 碼:</td>
<td><input type=‘text‘ id="passWord" name=‘password‘></td>
</tr>
<tr>
<td></td>
<td><input type=‘submit‘ value=‘登錄‘></td>
</tr>
</table>

</form>
</filedset>
</body>
</html>

login.php

<?php
header(‘Content-Type:text/html; charset=utf-8;‘);
$username=$_POST[‘username‘];
$password=$_POST[‘password‘];
$mysqli=new mysqli(‘127.0.0.1‘,‘root‘,‘‘,‘day1‘,‘3306‘);
$sql="select * from people where username=‘$username‘ AND password=‘$password‘";
$mysqli->query("SET NAMES UTF8");
$result=$mysqli->query($sql);
if($result->num_rows!=0){
echo ‘成功‘;
}else{
echo ‘錯誤‘;
}
$mysqli->close();
?>

結果圖:

初始界面

技術分享圖片

錯誤信息填寫

技術分享圖片

錯誤信息登錄後返回的結果

技術分享圖片

正確填寫信息

技術分享圖片

正確信息返回結果

技術分享圖片

使用到的工具:記事本(註意代碼中的編碼問題,將後綴名.txt改為.php或者.html時要記得另存為並選擇編碼為utf-8),XMAPP(集成mysql和apache,歐鵬瀏覽器)

html頁面通過http訪問mysql數據庫中的內容,實現用戶登錄的功能