Next, continue to learn: "PHP implementation of the shopping cart function (top)"
7. Implement a management interface
Login interface
Implemented by the following code:
7.1 admin.php
<?php/** * @author Switch * @copyright 2015 * main admin MENU///require_once statements and
The Require statement is exactly the same, except that PHP checks that the file has been included and, if so, does not include it again.
Require_once (' book_sc_fns.php ');
Session_Start ();
if ((@$_post[' username ']) && (@$_post[' passwd '))//attempt to login {$username = $_post[' username '];
$passwd = $_post[' passwd '];
if (Login ($username, $passwd)) {$_session[' admin_user '] = $username;
else {Do_html_header ("Problem:");
echo "<p>you could is logged in.<br/> You must is 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 (); ?>
Functions in 7.2 user_auth_fns.php file login ()
function login ($username, $password)//login
{
$conn = Db_connect ();//Connection Database
if (! $conn) return
0;
Check username uniqueness
$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;
}
Functions in 7.3 user_auth_fns.php files Check_admin_user ()
function Check_admin_user ()//Check whether admin
{
if (isset ($_session[' Admin_user ')) return
true;
else return
false;
Manage the main interface
Implemented by the following code:
Functions in 7.4 output_fns.php files Display_admin_menu ()
function Display_admin_menu ()//Output Administrator 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)//Display button
{
echo "<div align= \" center \ "><a href=\". $target. " \ >
</a></div>";
}
Directory Add
Directory Add Success
you can see a lot of novel directories in the catalog pages
Implemented by the following code:
7.5 insert_category_form.php
<?php
/**
* @author Switch
* @copyright 2015
* Allows administrators to add a table of contents to the database *
* *
//require_ The once statement is exactly the same as the Require statement, except that PHP checks that the file has been contained and, if so, does not include the
require_once (' book_sc_fns.php ') again;
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
* insert a new directory into the database * *
//require_ The once statement is exactly the same as the Require statement, except that PHP checks that the file has been contained and, if so, does not include the
require_once (' book_sc_fns.php ') again;
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." \ "is added to the database.</p>";
}
else
{
echo "<p>category \" ". $catname." \ "Could not being 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 ();
? >
Admin Directory Interface
Directory editing interface-updatable, deleted
Directory Update succeeded
The directory main interface can see that the directory changes successfully
Implemented by the following code:
7.7 edit_category_form.php
<?php
/**
* @author Switch
* @copyright 2015
* Admin Edit Directory form *
* * *
//require_ The once statement is exactly the same as the Require statement, except that PHP checks that the file is already contained and, if so, does not include it again.
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
* Update the directory in the database *
* *
//require_ The once statement is exactly the same as the Require statement, except that PHP checks that the file is already contained and, if so, does not include it again.
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 being 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 * The functions used by the admin script are set/function display_category_form ($category =
'///Show Catalog Form {//If incoming directory is in, enter edit mode $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= "maxlength=" "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=\" ". $cate gory[' catid ']. "
\ "/>"; ?> <input type= "Submit" value= "<?php echo $edit? ' Update ': ' Add ';?> Category "/></form> </td> <?php if ($edit)//Allow deletion of existing directory {echo" <t d> <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 = ')//show book form {//if incoming
Books exist, enter the editing mode $edit = Is_array ($book); ?> <form method= "POST" action= "<?php echo $edit? ' edit_book.php ': ' insert_book.php ';?> ' > <table border= ' 0 ' > <tr> <td>ISBN:</td> & Lt;td><input type= "text" Name= "ISBN" value= "<?php echo $edit? $book [' ISBN ']: ';?> '/></td> </tr> <tr> <td>book title:</td> <TD>&L T;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><in Put type= "text" name= "price" value= "<?php echo $edit? $book [' Price ']: ';?> '/></td> </tr> <tr> <td>Description:</td> <td> <textarea rows= "3" cols= "a" name= "description" ><?php echo $edit? $book [' description ']: ';?></textarea></td> </tr> <tr> <td <?php if (! $edit) {E Cho "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> <for M 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 ()///display Change 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>&l T;input type= "Password" name= "old_passwd" size= "" maxlength= ""/></td> </tr> <tr>password:</td> <td><input type= "password" name= "new_passwd" size= "td>new" maxlength= "/>&" lt;/td> </tr> <tr> <td>repeat new password:</td> <td><input type= "password "Name=" New_passwd2 "size=" "maxlength="/></td> </tr> <tr> <td colspan= "2" align= "ce
Nter "><input type=" Submit "value=" Change Password "/></td> </tr> </table> </form> <br/> <?php} function insert_category ($catname)//directory Insert {$conn = Db_connect ();//Database connection $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)//book Insert {$conn = Db_connect ();//Connect Database $query = "SELECT * from book where isbn= '". $isbn. "'"
;
$result = $conn->query ($query); if (! $result) | |
($result->num_rows!= 0))
return false; $query = INSERT into the books values (' ". $isbn. "', '". $author. "', '". $title. "', '". $catid. "', '". $price. "', '".
$description. "')";
$result = $conn->query ($query);
if (! $result) return false;
else return true; function Update_category ($catid, $catname)//Change directory name {$conn = Db_connect ();//Connect database $query = "Update Categori Es 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 ();//Connection database $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)//delete directory {$conn = Db_connect ();//Connect Database $query = "SELECT * FROM Books where catid= ' ".
$catid. "'";
$result = @ $conn->query ($query); if (! $result) | | (@ $result->num_rows > 0))
If there is a book in the directory, you cannot delete the directory return false; $query = "Delete from categories where catid= '".
$catid. "'";
$result = @ $conn->query ($query);
if (! $result) return false;
else return true; function Delete_book ($ISBN)//delete book {$conn = Db_connect ();//Connect Database $query = "Delete from books where ISBN ='".
$isbn. "'";
$result = @ $conn->query ($query);
if (! $result) return false;
else return true; }?>
7.10 Directory deletion operation, book add, update, delete operation is basically similar to the above operation, here is not demo, you can download code to view
8, extended
This project creates a fairly simple PHP shopping cart system. We can also make a lot of improvements and enhancements to it:
- In a real online store, you may have to set up some order-keeping and implementation systems--a system in which users cannot see orders that have already been booked.
- Customers want to be able to check their order processing without having to contact us. Users should be able to use an authentication method to enable them to view their previous orders, and can also combine operations closely with individual situations. Also more convenient for us to collect some user habits information.
- The picture of the book can be transferred to the image directory of the website through services such as FTP and give them a proper name. You can upload the file to the picture Insert page to make the operation easier.
- You can add user logins, personalized settings, and bibliography recommendations, online reviews, membership rules, inventory level checks, and more. The functionality that can be added is very much.
The above is the PHP implementation of the shopping cart function of all the code, I hope to help you learn.
SOURCE Download: Shopping Cart