php實(shí)現(xiàn)購(gòu)物車(chē)功能(下)
接著上篇繼續(xù)學(xué)習(xí): 《php實(shí)現(xiàn)購(gòu)物車(chē)的功能(上)》
7、實(shí)現(xiàn)一個(gè)管理界面

登錄界面
由以下代碼實(shí)現(xiàn):
7.1 admin.php
<?php
/**
* @author switch
* @copyright 2015
* 主管理菜單
*/
//require_once語(yǔ)句和require語(yǔ)句完全相同,唯一區(qū)別是PHP會(huì)檢查該文件是否已經(jīng)被包含過(guò),如果是則不會(huì)再次包含。
require_once('book_sc_fns.php');
session_start();
if((@$_POST['username']) && (@$_POST['passwd'])) //嘗試登陸
{
$username = $_POST['username'];
$passwd = $_POST['passwd'];
if(login($username,$passwd))
{
$_SESSION['admin_user'] = $username;
}
else
{
do_html_header("Problem:");
echo "<p>You could not be logged in.<br />
You must be logged in to view this page.</p>";
do_html_URL('login.php','Login');
do_html_footer();
exit;
}
}
do_html_header("Administration");
if(check_admin_user())
{
display_admin_menu();
}
else
{
echo "<p>You are not authorized to enter the administration area.</p>";
do_html_URL('login.php','Login');
}
do_html_footer();
?>
7.2 user_auth_fns.php文件中的函數(shù)login()
function login($username,$password) //登錄
{
$conn = db_connect(); //連接數(shù)據(jù)庫(kù)
if(!$conn)
return 0;
//檢查用戶名唯一性
$query = "select * from admin where username='". $username ."'
and password = sha1('". $password ."')";
$result = $conn ->query($query);
if(!$result)
return 0;
if($result ->num_rows > 0)
return 1;
else
return 0;
}
7.3 user_auth_fns.php文件中的函數(shù)check_admin_user()
function check_admin_user() //檢查是否是管理員
{
if(isset($_SESSION['admin_user']))
return true;
else
return false;
}

管理主界面
由以下代碼實(shí)現(xiàn):
7.4 output_fns.php文件中的函數(shù)display_admin_menu()
function display_admin_menu() //輸出管理員菜單
{
?>
<br />
<a href="index.php">Go to main site</a><br />
<a href="insert_category_form.php">Add a new category</a><br />
<a href="insert_book_form.php">Add a new book</a><br />
<a href="change_password_form.php">Change admin password</a><br />
<?php
}
function display_button($target,$image,$alt) //顯示按鈕
{
echo "<div align= \" center \"><a href=\"". $target ."\">
<img src=\"images/". $image .".gif\"
alt=\"". $alt ."\" border = \" 0 \" height = \" 50 \"
width = \" 135 \" /></a></div>";
}

目錄添加
目錄添加成功
目錄頁(yè)中可以看出多了Novel目錄
由以下代碼實(shí)現(xiàn):
7.5 insert_category_form.php
<?php
/**
* @author switch
* @copyright 2015
* 允許管理員向數(shù)據(jù)庫(kù)中添加一個(gè)目錄的表格
*/
//require_once語(yǔ)句和require語(yǔ)句完全相同,唯一區(qū)別是PHP會(huì)檢查該文件是否已經(jīng)被包含過(guò),如果是則不會(huì)再次包含
require_once('book_sc_fns.php');
session_start();
do_html_header();
if(check_admin_user())
{
display_category_form();
do_html_URL("admin.php","Back to administrtion menu");
}
else
{
echo "<p>You are not authorized to enter the administation area.</p>";
}
do_html_footer();
?>
7.6 insert_category.php
<?php
/**
* @author switch
* @copyright 2015
* 向數(shù)據(jù)庫(kù)中插入新目錄
*/
//require_once語(yǔ)句和require語(yǔ)句完全相同,唯一區(qū)別是PHP會(huì)檢查該文件是否已經(jīng)被包含過(guò),如果是則不會(huì)再次包含
require_once('book_sc_fns.php');
session_start();
do_html_header("Adding a category");
if(check_admin_user())
{
if(filled_out($_POST))
{
$catname =$_POST['catname'];
if(insert_category($catname))
{
echo "<p>Category \"". $catname ."\" was added to the database.</p>";
}
else
{
echo "<p>Category \"". $catname ."\" could not be added to the database.</p>";
}
}
else
{
echo "<p>You have not filled out the form. Please try again.</p>";
}
do_html_URL("admin.php","Back to administration menu");
}
else
{
echo "<p>You are not authorised to view this page.</p>";
}
do_html_footer();
?>

管理員目錄界面

目錄編輯界面-可更新,刪除

目錄更新成功

目錄主界面可以看到該目錄更改成功
由以下代碼實(shí)現(xiàn):
7.7 edit_category_form.php
<?php
/**
* @author switch
* @copyright 2015
* 管理員編輯目錄的表單
*/
//require_once語(yǔ)句和require語(yǔ)句完全相同,唯一區(qū)別是PHP會(huì)檢查該文件是否已經(jīng)被包含過(guò),如果是則不會(huì)再次包含。
require_once('book_sc_fns.php');
session_start();
do_html_header("Edit category");
if(check_admin_user())
{
if($catname = get_category_name($_GET['catid']))
{
$catid = $_GET['catid'];
$cat = compact('catname','catid');
display_category_form($cat);
}
else
{
echo "<p>Could not retrieve category details.</p>";
}
do_html_URL("admin.php","Back to administration menu");
}
else
{
echo "<p>You are not authorized to enter the administration area.</p>";
}
do_html_footer();
?>
7.8 edit_category.php
<?php
/**
* @author switch
* @copyright 2015
* 更新數(shù)據(jù)庫(kù)中的目錄
*/
//require_once語(yǔ)句和require語(yǔ)句完全相同,唯一區(qū)別是PHP會(huì)檢查該文件是否已經(jīng)被包含過(guò),如果是則不會(huì)再次包含。
require_once('book_sc_fns.php');
session_start();
do_html_header("Updating category");
if(check_admin_user())
{
if(filled_out($_POST))
{
if(update_category($_POST['catid'],$_POST['catname']))
{
echo "<p>Category was updated.</p>";
}
else
{
echo "<p>Category could not be updated.</p>";
}
}
else
{
echo "<p>you have not filled out the form. Please try again.</p>";
}
do_html_URL("admin.php","Back to administration menu");
}
else
{
echo "<p>You are not authorised to view this page.</p>";
}
do_html_footer();
?>
7.9 admin_fns.php
<?php
/**
* @author switch
* @copyright 2015
* 管理腳本使用的函數(shù)集合
*/
function display_category_form($category = '') //顯示目錄表單
{
//如果傳入存在目錄,進(jìn)入編輯模式
$edit = is_array($category);
?>
<form method="post" action="<?php echo $edit ? 'edit_category.php' :'insert_category.php'; ?>">
<table border="0">
<tr>
<td>Category Name:</td>
<td><input type="text" name="catname" size="40" maxlength="40" value="<?php echo $edit ? $category['catname'] : ''; ?>"/></td>
</tr>
<tr>
<td <?php if(!$edit){echo "colspan=2";} ?> align="center">
<?php
if($edit)
{
echo "<input type=\"hidden\" name=\"catid\" value=\"". $category['catid'] ."\" />";
}
?>
<input type="submit" value="<?php echo $edit ? 'Update' : 'Add'; ?> Category"/></form>
</td>
<?php
if($edit) //允許刪除存在目錄
{
echo "<td>
<form method=\"post\" action=\"delete_category.php\">
<input type=\"hidden\" name=\"catid\" value=\"". $category['catid'] ."\" />
<input type=\"submit\" value=\"Delete category\" />
</form></td>";
}
?>
</tr>
</table>
<?php
}
function display_book_form($book = '') //顯示圖書(shū)表單
{
//如果傳入圖書(shū)存在,進(jìn)入編輯模式
$edit = is_array($book);
?>
<form method="post" action="<?php echo $edit ? 'edit_book.php' : 'insert_book.php'; ?>">
<table border="0">
<tr>
<td>ISBN:</td>
<td><input type="text" name="isbn" value="<?php echo $edit ? $book['isbn'] : ''; ?>" /></td>
</tr>
<tr>
<td>Book Title:</td>
<td><input type="text" name="title" value="<?php echo $edit ? $book['title'] : ''; ?>" /></td>
</tr>
<tr>
<td>Book Author:</td>
<td><input type="text" name="author" value="<?php echo $edit ? $book['author'] : ''; ?>"/></td>
</tr>
<tr>
<td>Category:</td>
<td>
<select name="catid">
<?php
$cat_array = get_categories();
foreach($cat_array as $thiscat)
{
echo "<option value=\"". $thiscat['catid'] ."\"";
if(($edit) && ($thiscat['catid'] == $book['catid']))
{
echo " selected";
}
echo ">". $thiscat['catname'] ."</option>";
}
?>
</select>
</td>
</tr>
<tr>
<td>Price:</td>
<td><input type="text" name="price" value="<?php echo $edit ? $book['price'] : ''; ?>" /></td>
</tr>
<tr>
<td>Description:</td>
<td><textarea rows="3" cols="50" name="description"><?php echo $edit ? $book['description'] : ''; ?></textarea></td>
</tr>
<tr>
<td <?php if (!$edit) { echo "colspan=2"; }?> align="center">
<?php
if ($edit)
echo "<input type=\"hidden\" name=\"oldisbn\" value=\"".$book['isbn']."\" />";?>
<input type="submit" value="<?php echo $edit ? 'Update' : 'Add'; ?> Book" /></form></td>
<?php
if ($edit)
{
echo "<td>
<form method=\"post\" action=\"delete_book.php\">
<input type=\"hidden\" name=\"isbn\" value=\"".$book['isbn']."\" />
<input type=\"submit\" value=\"Delete book\"/>
</form></td>";
}
?>
</td>
</tr>
</table>
</form>
<?php
}
function display_password_form() //顯示更改密碼表單
{
?>
<br />
<form action="change_password.php" method="post">
<table width="250" cellpadding="2" cellspacing="0" bgcolor="#cccccc">
<tr>
<td>Old password:</td>
<td><input type="password" name="old_passwd" size="16" maxlength="16"/></td>
</tr>
<tr>
<td>New password:</td>
<td><input type="password" name="new_passwd" size="16" maxlength="16"/></td>
</tr>
<tr>
<td>Repeat new password:</td>
<td><input type="password" name="new_passwd2" size="16" maxlength="16"/></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Change password"/></td>
</tr>
</table>
</form>
<br />
<?php
}
function insert_category($catname) //目錄插入
{
$conn = db_connect(); //數(shù)據(jù)庫(kù)連接
$query = "select *
from categories
where catname='". $catname ."'";
$result = $conn ->query($query);
if((!$result) || ($result ->num_rows != 0))
return false;
$query = "insert into categories values
('','". $catname ."')";
$result = $conn ->query($query);
if(!$result)
return false;
else
return true;
}
function insert_book($isbn,$title,$author,$catid,$price,$description) //圖書(shū)插入
{
$conn = db_connect(); //連接數(shù)據(jù)庫(kù)
$query = "select * from books
where isbn='". $isbn ."'";
$result = $conn ->query($query);
if((!$result) || ($result ->num_rows != 0))
return false;
$query = "insert into books values
('". $isbn ."','". $author ."','". $title ."',
'". $catid ."','". $price ."','". $description ."')";
$result = $conn ->query($query);
if(!$result)
return false;
else
return true;
}
function update_category($catid,$catname) //更改目錄名稱
{
$conn = db_connect(); //連接數(shù)據(jù)庫(kù)
$query = "update categories
set catname='". $catname ."'
where catid='". $catid ."'";
$result = @$conn ->query($query);
if(!$result)
return false;
else
return true;
}
function update_book($oldisbn,$isbn,$title,$author,$catid,$price,$description)
{
$conn = db_connect(); //連接數(shù)據(jù)庫(kù)
$query = "update books
set isbn='". $isbn ."',
title='". $title ."',
author='". $author ."',
catid='". $catid ."',
price ='". $price ."',
description='". $description ."'
where isbn='". $oldisbn ."'";
$result = @$conn ->query($query);
if(!$result)
return false;
else
return true;
}
function delete_category($catid) //刪除目錄
{
$conn = db_connect(); //連接數(shù)據(jù)庫(kù)
$query = "select *
from books
where catid='". $catid ."'";
$result = @$conn ->query($query);
if((!$result) || (@$result ->num_rows > 0)) //如果該目錄有圖書(shū),無(wú)法刪除該目錄
return false;
$query = "delete from categories
where catid='". $catid ."'";
$result = @$conn ->query($query);
if(!$result)
return false;
else
return true;
}
function delete_book($isbn) //刪除圖書(shū)
{
$conn = db_connect(); //連接數(shù)據(jù)庫(kù)
$query = "delete from books
where isbn='". $isbn ."'";
$result = @$conn ->query($query);
if(!$result)
return false;
else
return true;
}
?>
7.10 目錄刪除操作,圖書(shū)添加,更新,刪除操作基本與上述操作差不多,這里就不在演示,可以下載代碼查看
8、擴(kuò)展
本項(xiàng)目創(chuàng)建了一個(gè)相當(dāng)簡(jiǎn)單的PHP購(gòu)物車(chē)系統(tǒng)。我們還可以對(duì)它進(jìn)行許多改進(jìn)和提高:
- 在真正的在線商店,可能必須建立一些訂單記錄和實(shí)施系統(tǒng)——在這個(gè)系統(tǒng)中,用戶無(wú)法看到已經(jīng)預(yù)定了的訂單。
- 顧客希望在不必與我們聯(lián)系的前提下就能檢查到他們的訂單處理情況。用戶應(yīng)當(dāng)可以通過(guò)一種身份驗(yàn)證方式使之能夠查看自己以前的訂單,并且也可以將操作與個(gè)人情況緊密地結(jié)合起來(lái)。也更方便我們收集一些用戶習(xí)慣信息。
- 圖書(shū)的圖片可以通過(guò)FTP之類(lèi)的服務(wù)傳輸?shù)皆摼W(wǎng)站的圖像目錄并給它們?nèi)∫粋€(gè)合適的名字。可以把文件上載到圖片插入頁(yè),以使該操作方便一些。
- 可以添加用戶登錄、個(gè)性化設(shè)置以及書(shū)目推薦、在線評(píng)論、會(huì)員制度、庫(kù)存級(jí)別檢查等。可以添加的功能是非常多的。
以上就是php實(shí)現(xiàn)購(gòu)物車(chē)功能的全部代碼,希望對(duì)大家的學(xué)習(xí)有所幫助。
源碼下載:購(gòu)物車(chē)
- php+pdo實(shí)現(xiàn)的購(gòu)物車(chē)類(lèi)完整示例
- php實(shí)現(xiàn)保存周期為1天的購(gòu)物車(chē)類(lèi)
- php實(shí)現(xiàn)購(gòu)物車(chē)產(chǎn)品刪除功能(2)
- php實(shí)現(xiàn)產(chǎn)品加入購(gòu)物車(chē)功能(1)
- php實(shí)現(xiàn)購(gòu)物車(chē)功能(以大蘋(píng)果購(gòu)物網(wǎng)為例)
- php實(shí)現(xiàn)簡(jiǎn)單加入購(gòu)物車(chē)功能
- PHP實(shí)現(xiàn)添加購(gòu)物車(chē)功能
- php實(shí)現(xiàn)購(gòu)物車(chē)功能(上)
- php實(shí)現(xiàn)商城購(gòu)物車(chē)的思路和源碼分析
相關(guān)文章
php使用PDO操作MySQL數(shù)據(jù)庫(kù)實(shí)例
這篇文章主要介紹了php使用PDO操作MySQL數(shù)據(jù)庫(kù),實(shí)例分析了PDO的開(kāi)啟與針對(duì)MySQL數(shù)據(jù)庫(kù)的增刪改查等基本操作方法,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2014-12-12
對(duì)text數(shù)據(jù)類(lèi)型不支持代碼頁(yè)轉(zhuǎn)換 從: 1252 到: 936
錯(cuò)誤的提示同樣是不能從text的轉(zhuǎn)換問(wèn)題:這主要是由于數(shù)據(jù)庫(kù)在設(shè)計(jì)的時(shí)候的數(shù)據(jù)類(lèi)型存在Text——而我們采用的是中文操作系統(tǒng)。檢查數(shù)據(jù)庫(kù)的腳本,修改Text為ntext。支持unicode。2011-04-04
phpMyAdmin通過(guò)密碼漏洞留后門(mén)文件
今天小編就為大家分享一篇關(guān)于phpMyAdmin通過(guò)密碼漏洞留后門(mén)文件,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-11-11
PHP fgetcsv 定義和用法(附windows與linux下兼容問(wèn)題)
PHP fgetcsv() 函數(shù)從文件指針中讀入一行并解析 CSV 字段。與PHP fgets() 類(lèi)似,不同的是 PHP fgetcsv() 解析讀入的行并找出 CSV 格式的字段,然后返回一個(gè)包含這些字段的數(shù)組2012-05-05
SublimeText3配置PHP函數(shù)追蹤定位插件
這篇文章主要介紹了SublimeText3的PHP函數(shù)追蹤定位插件ctags和codeBeautifier,對(duì)SublimeText3感興趣的同學(xué),可以多研究下2021-04-04
Windows下XDebug 手工配置與使用說(shuō)明
XDebug 是一個(gè) php 代碼的調(diào)試工具, 對(duì)程序員調(diào)試程序有很大的幫助, 下面簡(jiǎn)單說(shuō)下 XDebug 在 Windows 下的配置.2010-07-07

