PHP and MySQL Web development from novice to expert, 8th Day-Create Categories Admin page

Source: Internet
Author: User
Tags getmessage php and mysql

1. Create Categories Admin Page

Mainly includes the following pages:

A. index.php, prepare various variable data. For display page use.

B. categories.html.php, showing categories.

C. form.html.php, which is used to edit or add author pages.

Page suburb fruit:

2. Main process of Categories page

2.1 Is logged in

if (! User_is_login ()) {      include ': /login.html.php ';       Exit ();  }

2.2 Whether you have permission

if {!user_has_role (' content editor ') {      $error = ' content Editor may access this page ... '; c5/>include '. /access.denied.html.php ';       Exit ();  }

2.3 Adding categories to catalogs

//Add Categories  if(isset($_get[' Add '])) {      include $_server[' Document_root ']. ' /php_mysql_web/includes/db.inc.php '; Try {            $pageTitle= ' Add category '; $action= ' Addcategory '; $categoryText= ' '; $id= ' '; $button= ' Add category '; include' Form.html.php '; Exit(); } Catch(pdoexception$e) {          $error= ' Error getting list of jokes to edit ... '.$e-GetMessage (); include $_server[' Document_root ']. ' /php_mysql_web/includes/error.html.php '; Exit(); }  }    //Add Categories  if(isset($_get[' Addcategory ']) &&$_post[' Categorytext ']! = ') {      include $_server[' Document_root ']. ' /php_mysql_web/includes/db.inc.php '; Try {          $sql= ' INSERT into category ' (name) VALUES (: categorytext); '; $s=$pdo->prepare ($sql); $s->bindvalue (': Categorytext ',$_post[' Categorytext ']); $s-execute (); } Catch(pdoexception$e) {          $error= ' Error adding category ... '.$e-GetMessage (); include $_server[' Document_root ']. ' /php_mysql_web/includes/error.html.php '; Exit(); }      Header(' Location:. ')); Exit(); }

2.4 Edit Category Catalogue

//Edit Categories  if(isset($_post[' Action ']) and$_post[' action '] = = ' Edit ') {      include $_server[' Document_root ']. ' /php_mysql_web/includes/db.inc.php '; Try {          $sql= ' SELECT name from category WHERE ID =: ID '; $s=$pdo->prepare ($sql); $s->bindvalue (': Id ',$_post[' ID ']); $s-execute (); $row=$s-fetch (); $pageTitle= ' Edit category '; $action= ' Editcategory '; $categoryText=$row[' Name ']; $id=$_post[' ID ']; $button= ' Update category '; include' Form.html.php '; Exit(); } Catch(pdoexception$e) {          $error= ' Error getting list of category to edit ... '.$e-GetMessage (); include $_server[' Document_root ']. ' /php_mysql_web/includes/error.html.php '; Exit(); }  }    //Edit Categories  if(isset($_get[' Editcategory ']) &&$_post[' Categorytext ']! = ') {      include $_server[' Document_root ']. ' /php_mysql_web/includes/db.inc.php '; Try {          $sql=UPDATE category SET name =: Name WHERE ID =: ID;‘; $s=$pdo->prepare ($sql); $s->bindvalue (': Name ',$_post[' Categorytext ']); $s->bindvalue (': Id ',$_post[' ID ']); $s-execute (); } Catch(pdoexception$e) {          $error= ' Error updating categories ... '.$e-GetMessage (); include $_server[' Document_root ']. ' /php_mysql_web/includes/error.html.php '; Exit(); }            Header(' Location:. ')); Exit(); }
2.5 Delete a category directory
if(isset($_post[' Action ']) and$_post[' action '] = = ' Delete ') {            include $_server[' Document_root ']. ' /php_mysql_web/includes/db.inc.php '; //Delete jokecategory Entries      Try {          $sql= ' DELETE from jokecategory WHERE CategoryID =: Id '; $s=$pdo->prepare ($sql); $s->bindvalue (': Id ',$_post[' ID ']); $s-execute (); } Catch(pdoexception$e) {          $error= ' Error deleting jokecategory ... '.$e-GetMessage (); include $_server[' Document_root ']. ' /php_mysql_web/includes/error.html.php '; Exit(); }                  //Delete category Entries      Try {          $sql= ' DELETE from category WHERE ID =: ID '; $s=$pdo->prepare ($sql); $s->bindvalue (': Id ',$_post[' ID ']); $s-execute (); } Catch(pdoexception$e) {          $error= ' Error deleting category ... '.$e-GetMessage (); include $_server[' Document_root ']. ' /php_mysql_web/includes/error.html.php '; Exit(); }      Header(' Location:. ')); Exit(); }

2.6 Show Category List

//Show all categories in database ...  Try {      include $_server[' Document_root ']. ' /php_mysql_web/includes/db.inc.php '; $QUERYSQL= "SELECT ID, name from category;"; $result=$pdo->query ($QUERYSQL); foreach($result  as $row) {          $categories[] =Array(' id ' = =$row[' ID '], ' name ' = =$row[' Name ']); }                      //if (count ($categories) > 0)      {          $sql= "Select COUNT (*) as RCT from category;"; $s=$pdo->prepare ($sql); $s-execute (); $row=$s-fetch (); $rowCount=$row[' RCT ']; include' Categories.html.php '; }   } Catch(pdoexception$e) {      $error= "Query category failed ...".$e-GetMessage (); include $_server[' Document_root ']. ' /php_mysql_web/includes/error.html.php '; Exit(); }

PHP and MySQL Web development from novice to expert, 8th Day-Create Categories Admin page

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.