範例的網址:http://tomchen263.000webhostapp.com/LoginExample_Web/login.html
資料庫格式與資料表:
2張資料表,books與employee
books內容
employee內容
conn_mysql.php:
<?php
$db_link=@mysqli_connect("資料庫位置","資料庫帳號","資料庫密碼");
if(!$db_link){
die("資料庫連線失敗<br>");
}else{
echo"資料庫連線成功<br>";
}
mysqli_query($db_link,"SET NAMES 'utf-8'"); //設定字元集與編碼為utf-8
$seldb=@mysqli_select_db($db_link,"資料庫名稱");
if(!$seldb){
die("資料庫選擇失敗<br>");
}else{
echo"資料庫選擇成功<br>";
}
?>
login.html:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<form method="POST" action="login_php.php">
請輸入帳號:<input type="text" name="username"/><br>
請輸入密碼:<input type="Password" name="password"/><br>
<input type="submit" value="登入"/>
</form>
</body>
</html>
login_php.php:
<?php
header('Content-Type: text/html; charset=utf-8');
$username=$_POST['username'];
$password=$_POST['password'];
require("conn_mysql.php");
$sql_query_login="SELECT * FROM employee where username='$username' AND password='$password'";
$result1=mysqli_query($db_link,$sql_query_login) or die("查詢失敗");
if(mysqli_num_rows($result1)){
$sql_query="SELECT * FROM books";
$result=mysqli_query($db_link,$sql_query);
echo "<table border=1 width=400 cellpadding=5>";
echo "<tr>
<td>書籍編號</td>
<td>書籍名稱</td>
<td>負責員工編號</td>
<td>價錢</td>
</tr>";
while($row=mysqli_fetch_array($result)){
echo "<tr>
<td>$row[0]</td>
<td>$row[1]</td>
<td>$row[2]</td>
<td>$row[3]</td>
</tr>";
}
echo"</table>";
}else{
echo"登入失敗";
}
?>