博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PHP和MySQL Web开发从新手到高手,第8天-创建categories管理页面
阅读量:6982 次
发布时间:2019-06-27

本文共 5181 字,大约阅读时间需要 17 分钟。

1. 创建categories管理页面

  主要包含以下几个页面:

  A. index.php, 准备各种变量数据.供展示页面使用.

  B. categories.html.php, 显示categories.

  C. form.html.php, 用于编缉或添加作者的页面.

页面郊果:

 

2. categories页面的主要流程

2.1 是否已登录

if (!user_is_login()){      include '../login.html.php';      exit();  }

2.2 是否有权限

if (!user_has_role('Content Editor')){      $error = 'only Content Editor may access this page...';      include '../access.denied.html.php';      exit();  }

2.3 添加分类目录

// 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 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 删除分类目录
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 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();  }

转载于:https://www.cnblogs.com/BigBigLiang/p/4934243.html

你可能感兴趣的文章
List创建,添加,读取,遍历,修改,移除等方法
查看>>
Maven构建springMVC+spring+MyBatis项目
查看>>
猴子选大王
查看>>
3249 搭积木
查看>>
POJ2749:Building roads——题解
查看>>
[SpringMVC]定义多个前缀映射的问题
查看>>
高中时的口头禅
查看>>
C++ 虚函数表解析
查看>>
[SCOI2009]windy数
查看>>
Struts2--Action属性接收参数
查看>>
2012年科技新闻背后的大数字
查看>>
报价单内,同一物料只允许一条行价格记录
查看>>
leetcode 283. Move Zeroes
查看>>
自己的php函数库
查看>>
HDU Problem 1599 find the mincost route 【Floyd求最小环】
查看>>
HDU2017多校联合 contest 1
查看>>
基于DevExpress实现对PDF、Word、Excel文档的预览及操作处理(转载自:http://www.cnblogs.com/wuhuacong/p/4175266.html)...
查看>>
range
查看>>
[Noi2002]Savage 题解
查看>>
特征选择, 经典三刀(转)
查看>>