1.3用戶登錄
用戶登錄成功后跳轉到商品顯示頁面
1.3.1設計界面
1、新建一個login.php頁面,用來做用戶的登錄
2、登錄業務原理
通過輸入的用戶名和密碼查詢對應的記錄,表示登陸成功,否則登錄失敗
SQL語句如下:
$sql = "select * from users where username = '$username' and password='$password'";
3、業務邏輯的實現
源代碼為:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>無標題文檔</title> <link rel="stylesheet" type="text/css" href="styles/common.css"/> </head> <body> <?php if (isset($_POST['button'])){ $username=$_POST['username'];//得到用戶輸入的用戶名 $password=$_POST['password'];//密碼 mysql_connect('127.0.0.1:3306','root','')or die(mysql_error());//連接數據庫 mysql_select_db('mvc_study');//選擇數據庫 mysql_query('set names utf8'); $sql = "select * from users where username = '$username' and password='$password'"; $rs=mysql_query($sql); if(mysql_num_rows($rs)==1){//如果數據庫的行數為1則成功否則失敗 echo '登陸成功'; }else{ echo '登錄失敗'; } } ?> <form id="form1" name="form1" method="post" action=""> <table width="254" border="1" align="center"> <tr> <th colspan="2" scope="col">用戶登錄</th> </tr> <tr> <td width="70">用戶名</td> <td width="168"><label for="username"></label> <input type="text" name="username" id="username" /></td> </tr> <tr> <td>密碼</td> <td><input type="password" name="password" id="password" /></td> </tr> <tr> <td colspan="2" align="center"><label for="password"> <input type="submit" name="button" id="button" value="提交" /> </label></td> </tr> </table> </form> </body> </html>
1.4 管理員之管理頁面
為了便於管理,在站點下新建一個文件夾(admin),用來存放管理員的管理頁面。
在admin文件夾中,新建一個admin.php頁面,用來做管理員的管理頁面。
在頁面中導入外部樣式
頁面效果和前台顯示頁面效果(showgoods.php)是一樣的,多了三個鏈接,“添加商品”,“修改”,“刪除”
具體代碼參見admin下的admin.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>無標題文檔</title> <link rel="stylesheet" type="text/css" href="../styles/common.css"/> <style type="text/css"> table{ width:980px; } </style> </head> <body> <?php //連接數據庫 mysql_connect('localhost','root','') or die (mysql_error()); mysql_select_db('data'); mysql_query('set names utf8'); $rs = mysql_query('select * from products'); ?> <a>添加商品</a> <table width="980"> <tr> <th>編號</th> <th>商品名稱</th><th>規格</th><th>庫存量</th> <th>圖片</th> <th>網址</th><th>修改</th><th>刪除</th> <?php while($rows = mysql_fetch_assoc($rs)){ echo '<tr>'; echo '<td>'.$rows['proID'].'</td>'; echo '<td>'.$rows['proname'].'</td>'; echo '<td>'.$rows['proguige'].'</td>'; echo '<td>'.$rows['promount'].'</td>'; echo '<td>'.$rows['proimages'].'</td>'; echo '<td>'.$rows['proweb'].'</td>'; echo '<td><input type = "button" value="修改"</td>' ; echo '<td><input type = "button" value="刪除"</td>' ; echo '</tr>'; } ?> </tr> </body> </html>
common.css代碼為:
@charset "utf-8"; table,th,td{ border:#000 solid 1px; } table{ margin:auto; font-size:14px; } tr{ height:35px; }