php实现购物车功能(下)(2)

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

7.10 目录删除操作,图书添加,更新,删除操作基本与上述操作差不多,这里就不在演示,可以下载代码查看

8、扩展
本项目创建了一个相当简单的PHP购物车系统。我们还可以对它进行许多改进和提高:

在真正的在线商店,可能必须建立一些订单记录和实施系统——在这个系统中,用户无法看到已经预定了的订单。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/b45e15f095ab8b814e90c11ef38eaa4c.html