首頁  >  文章  >  後端開發  >  關於php實現購物車的程式碼二

關於php實現購物車的程式碼二

不言
不言原創
2018-06-19 17:25:161727瀏覽

這篇文章主要介紹了php實現購物車功能的全部程式碼,提出了需求分析、解決方案、資料庫的創建,幫助大家輕輕鬆松實現購物車功能,有興趣的小夥伴們可以參考一下

接著上篇繼續學習:《關於php實作購物車的程式碼一》

#7、實作一個管理介面


登入介面

由下列程式碼實作:
7.1 admin.php

<?php 
 
/** 
 * @author switch 
 * @copyright 2015 
 * 主管理菜单 
 */ 
 //require_once语句和require语句完全相同,唯一区别是PHP会检查该文件是否已经被包含过,如果是则不会再次包含。 
 require_once(&#39;book_sc_fns.php&#39;); 
 
 session_start(); 
 
 if((@$_POST[&#39;username&#39;]) && (@$_POST[&#39;passwd&#39;])) //尝试登陆 
 { 
 $username = $_POST[&#39;username&#39;]; 
 $passwd = $_POST[&#39;passwd&#39;]; 
 
 if(login($username,$passwd)) 
 { 
 $_SESSION[&#39;admin_user&#39;] = $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(&#39;login.php&#39;,&#39;Login&#39;); 
 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(&#39;login.php&#39;,&#39;Login&#39;); 
 } 
 do_html_footer(); 
?>

#7.2 user_auth_fns.php檔案中的函數login()

function login($username,$password) //登录 
 { 
 $conn = db_connect(); //连接数据库 
 
 if(!$conn) 
 return 0; 
 
 //检查用户名唯一性 
 $query = "select * from admin where username=&#39;". $username ."&#39; 
  and password = sha1(&#39;". $password ."&#39;)"; 
 $result = $conn ->query($query); 
 
 if(!$result) 
 return 0; 
 
 if($result ->num_rows > 0) 
 return 1; 
 else 
 return 0; 
 }

7.3 user_auth_fns.php檔案中的函數check_admin_user()

#user()

#user()

function check_admin_user() //检查是否是管理员 
 { 
 if(isset($_SESSION[&#39;admin_user&#39;])) 
 return true; 
 else 
 return false; 
 }


管理主介面

由下列程式碼實作:

7.4 output_fns.php檔案中的函數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 "<p align= \" center \"><a href=\"". $target ."\"> 
 <img src=\"images/". $image .".gif\" 
 alt=\"". $alt ."\" border = \" 0 \" height = \" 50 \" 
 width = \" 135 \" /></a></p>"; 
 }


#目錄新增
#目錄新增成功

目錄頁中可以看出多了Novel目錄


由以下程式碼實現:

7.5 insert_category_form.php

<?php 
 
/** 
 * @author switch 
 * @copyright 2015 
 * 允许管理员向数据库中添加一个目录的表格 
 */ 
 //require_once语句和require语句完全相同,唯一区别是PHP会检查该文件是否已经被包含过,如果是则不会再次包含 
 require_once(&#39;book_sc_fns.php&#39;); 
 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 
 * 向数据库中插入新目录 
 */ 
 //require_once语句和require语句完全相同,唯一区别是PHP会检查该文件是否已经被包含过,如果是则不会再次包含 
 require_once(&#39;book_sc_fns.php&#39;); 
 session_start(); 
 
 do_html_header("Adding a category"); 
 if(check_admin_user()) 
 { 
 if(filled_out($_POST)) 
 { 
 $catname =$_POST[&#39;catname&#39;]; 
 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(); 
?>

管理員目錄介面


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


目錄更新成功



目錄主介面可以看到該目錄變更成功


由下列程式碼實作:

7.7 edit_category_form.php

<?php 
 
/** 
 * @author switch 
 * @copyright 2015 
 * 管理员编辑目录的表单 
 */ 
 //require_once语句和require语句完全相同,唯一区别是PHP会检查该文件是否已经被包含过,如果是则不会再次包含。 
 require_once(&#39;book_sc_fns.php&#39;); 
 session_start(); 
 
 do_html_header("Edit category"); 
 if(check_admin_user()) 
 { 
 if($catname = get_category_name($_GET[&#39;catid&#39;])) 
 { 
 $catid = $_GET[&#39;catid&#39;]; 
 $cat = compact(&#39;catname&#39;,&#39;catid&#39;); 
 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 
 * 更新数据库中的目录 
 */ 
 //require_once语句和require语句完全相同,唯一区别是PHP会检查该文件是否已经被包含过,如果是则不会再次包含。 
 require_once(&#39;book_sc_fns.php&#39;); 
 session_start(); 
 
 do_html_header("Updating category"); 
 if(check_admin_user()) 
 { 
 if(filled_out($_POST)) 
 { 
 if(update_category($_POST[&#39;catid&#39;],$_POST[&#39;catname&#39;])) 
 { 
 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 
 * 管理脚本使用的函数集合 
 */ 
 function display_category_form($category = &#39;&#39;) //显示目录表单 
 { 
 //如果传入存在目录,进入编辑模式 
 $edit = is_array($category); 
 ?> 
 <form method="post" action="<?php echo $edit ? &#39;edit_category.php&#39; :&#39;insert_category.php&#39;; ?>"> 
 <table border="0"> 
  <tr> 
  <td>Category Name:</td> 
  <td><input type="text" name="catname" size="40" maxlength="40" value="<?php echo $edit ? $category[&#39;catname&#39;] : &#39;&#39;; ?>"/></td> 
  </tr> 
  <tr> 
  <td <?php if(!$edit){echo "colspan=2";} ?> align="center"> 
  <?php 
  if($edit) 
  { 
   echo "<input type=\"hidden\" name=\"catid\" value=\"". $category[&#39;catid&#39;] ."\" />"; 
  } 
  ?> 
  <input type="submit" value="<?php echo $edit ? &#39;Update&#39; : &#39;Add&#39;; ?> Category"/></form> 
  </td> 
  <?php 
  if($edit) //允许删除存在目录 
  { 
  echo "<td> 
   <form method=\"post\" action=\"delete_category.php\"> 
   <input type=\"hidden\" name=\"catid\" value=\"". $category[&#39;catid&#39;] ."\" /> 
   <input type=\"submit\" value=\"Delete category\" /> 
   </form></td>"; 
  } 
  ?> 
  </tr> 
 </table> 
 <?php 
 } 
 
 function display_book_form($book = &#39;&#39;) //显示图书表单 
 { 
 //如果传入图书存在,进入编辑模式 
 $edit = is_array($book); 
 ?> 
 
 <form method="post" action="<?php echo $edit ? &#39;edit_book.php&#39; : &#39;insert_book.php&#39;; ?>"> 
 <table border="0"> 
 <tr> 
  <td>ISBN:</td> 
  <td><input type="text" name="isbn" value="<?php echo $edit ? $book[&#39;isbn&#39;] : &#39;&#39;; ?>" /></td> 
 </tr> 
 <tr> 
  <td>Book Title:</td> 
  <td><input type="text" name="title" value="<?php echo $edit ? $book[&#39;title&#39;] : &#39;&#39;; ?>" /></td> 
 </tr> 
 <tr> 
  <td>Book Author:</td> 
  <td><input type="text" name="author" value="<?php echo $edit ? $book[&#39;author&#39;] : &#39;&#39;; ?>"/></td> 
 </tr> 
 <tr> 
  <td>Category:</td> 
  <td> 
  <select name="catid"> 
  <?php 
  $cat_array = get_categories(); 
  foreach($cat_array as $thiscat) 
  { 
   echo "<option value=\"". $thiscat[&#39;catid&#39;] ."\""; 
   if(($edit) && ($thiscat[&#39;catid&#39;] == $book[&#39;catid&#39;])) 
   { 
   echo " selected"; 
   } 
   echo ">". $thiscat[&#39;catname&#39;] ."</option>"; 
  } 
  ?> 
  </select> 
  </td> 
 </tr> 
 <tr> 
  <td>Price:</td> 
  <td><input type="text" name="price" value="<?php echo $edit ? $book[&#39;price&#39;] : &#39;&#39;; ?>" /></td> 
 </tr> 
 <tr> 
  <td>Description:</td> 
  <td><textarea rows="3" cols="50" name="description"><?php echo $edit ? $book[&#39;description&#39;] : &#39;&#39;; ?></textarea></td> 
 </tr> 
 <tr> 
  <td <?php if (!$edit) { echo "colspan=2"; }?> align="center"> 
  <?php 
  if ($edit) 
  echo "<input type=\"hidden\" name=\"oldisbn\" value=\"".$book[&#39;isbn&#39;]."\" />";?> 
  <input type="submit" value="<?php echo $edit ? &#39;Update&#39; : &#39;Add&#39;; ?> Book" /></form></td> 
  <?php 
  if ($edit) 
  { 
  echo "<td> 
   <form method=\"post\" action=\"delete_book.php\"> 
   <input type=\"hidden\" name=\"isbn\" value=\"".$book[&#39;isbn&#39;]."\" /> 
   <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(); //数据库连接 
 
 $query = "select * 
  from categories 
  where catname=&#39;". $catname ."&#39;"; 
 $result = $conn ->query($query); 
 if((!$result) || ($result ->num_rows != 0)) 
 return false; 
 
 $query = "insert into categories values 
 (&#39;&#39;,&#39;". $catname ."&#39;)"; 
 $result = $conn ->query($query); 
 if(!$result) 
 return false; 
 else 
 return true; 
 } 
 
 function insert_book($isbn,$title,$author,$catid,$price,$description) //图书插入 
 { 
 $conn = db_connect(); //连接数据库 
 
 $query = "select * from books 
  where isbn=&#39;". $isbn ."&#39;"; 
 $result = $conn ->query($query); 
 if((!$result) || ($result ->num_rows != 0)) 
 return false; 
 
 $query = "insert into books values 
 (&#39;". $isbn ."&#39;,&#39;". $author ."&#39;,&#39;". $title ."&#39;, 
 &#39;". $catid ."&#39;,&#39;". $price ."&#39;,&#39;". $description ."&#39;)"; 
 
 
 $result = $conn ->query($query); 
 if(!$result) 
 return false; 
 else 
 return true; 
 } 
 
 function update_category($catid,$catname) //更改目录名称 
 { 
 $conn = db_connect(); //连接数据库 
 
 $query = "update categories 
  set catname=&#39;". $catname ."&#39; 
  where catid=&#39;". $catid ."&#39;"; 
 $result = @$conn ->query($query); 
 if(!$result) 
 return false; 
 else 
 return true; 
 } 
 
 function update_book($oldisbn,$isbn,$title,$author,$catid,$price,$description) 
 { 
 $conn = db_connect(); //连接数据库 
 
 $query = "update books 
  set isbn=&#39;". $isbn ."&#39;, 
  title=&#39;". $title ."&#39;, 
  author=&#39;". $author ."&#39;, 
  catid=&#39;". $catid ."&#39;, 
  price =&#39;". $price ."&#39;, 
  description=&#39;". $description ."&#39; 
  where isbn=&#39;". $oldisbn ."&#39;"; 
 $result = @$conn ->query($query); 
 if(!$result) 
 return false; 
 else 
 return true; 
 } 
 
 function delete_category($catid) //删除目录 
 { 
 $conn = db_connect(); //连接数据库 
 
 $query = "select * 
  from books 
  where catid=&#39;". $catid ."&#39;"; 
 $result = @$conn ->query($query); 
 if((!$result) || (@$result ->num_rows > 0)) //如果该目录有图书,无法删除该目录 
 return false; 
 
 $query = "delete from categories 
  where catid=&#39;". $catid ."&#39;"; 
 $result = @$conn ->query($query); 
 if(!$result) 
 return false; 
 else 
 return true; 
 } 
 
 function delete_book($isbn) //删除图书 
 { 
 $conn = db_connect(); //连接数据库 
 
 $query = "delete from books 
  where isbn=&#39;". $isbn ."&#39;"; 
 $result = @$conn ->query($query); 
 if(!$result) 
 return false; 
 else 
 return true; 
 } 
?>

 7.10 目錄刪除操作,圖書添加,更新,刪除操作基本上與上述操作差不多,這裡就不在演示,可以下載代碼查看

8、擴展
    本項目創建了一個相當簡單的PHP購物車系統。我們還可以對它進行許多改進和改進:
  • 在真正的線上商店,可能必須建立一些訂單記錄和實施系統——在這個系統中,用戶無法看到已經預定了的訂單。

  • 顧客希望在不必與我們聯繫的前提下就能檢查到他們的訂單處理情況。使用者應可以透過一種身份驗證方式使之能夠查看自己先前的訂單,並且也可以將操作與個人情況緊密地結合起來。也更方便我們收集一些使用者習慣資訊。

  • 書籍的圖片可以透過FTP之類的服務傳輸到該網站的圖像目錄並給它們一個合適的名字。可以把文件上載到圖片插入頁,以使該操作方便一些。

  • 可以新增使用者登入、個人化設定以及書目推薦、線上評論、會員制度、庫存等級檢查等。可以添加的功能是非常多的。

以上就是本文的全部內容,希望對大家的學習有所幫助,更多相關內容請關注PHP中文網!

相關推薦:
關於php實作購物車的程式碼一

### ###

以上是關於php實現購物車的程式碼二的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn