PHP和MySQL Web开发从新手到高手,第7天-创建author管理页面
生活随笔
收集整理的這篇文章主要介紹了
PHP和MySQL Web开发从新手到高手,第7天-创建author管理页面
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1. 創(chuàng)建author管理頁面
? 主要包含以下幾個頁面:
? A. index.php, 準(zhǔn)備各種變量數(shù)據(jù).供展示頁面使用.
? B. authors.html.php, 顯示author.
? C. form.html.php, 用于編緝或添加作者的頁面.
頁面郊果:
?
2. author頁面的主要流程
2.1 是否已登錄
if (!user_is_login()){include '../login.html.php';exit();}2.2 是否有權(quán)限
if (!user_has_role('Content Editor')){$error = 'only Content Editor may access this page...';include '../access.denied.html.php';exit();}2.3 添加作者
// add authorsif (isset($_GET['add'])) {include $_SERVER['DOCUMENT_ROOT'].'/PHP_MySQL_Web/includes/db.inc.php';$pageTitle = 'Add author';$action = 'addForm';$author_name = '';$author_email = '';$id = '';$author_password = '';$button = 'Add author';try { $result = $pdo->query('SELECT id, description FROM role');foreach ($result as $row){$roles[] = array('id'=>$row['id'],'description'=>$row['description'],'selected'=>false);}} 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();} include 'form.html.php';exit();}// add authorsif (isset($_GET['addForm']) && $_POST['author_name'] != '') {include $_SERVER['DOCUMENT_ROOT'].'/PHP_MySQL_Web/includes/db.inc.php';// add authortry {$sql = 'INSERT INTO author(name,email) VALUES(:name,:email);';$s = $pdo->prepare($sql); $s->bindValue(':name', $_POST['author_name']);$s->bindValue(':email', $_POST['author_email']); $s->execute();} catch (PDOException $e) {$error = 'Error adding author...'.$e->getMessage();include $_SERVER['DOCUMENT_ROOT'].'/PHP_MySQL_Web/includes/error.html.php';exit();}$authorid = $pdo->lastInsertId();// add passwordif ($_POST['author_password'] != ''){ try {$sql = 'UPDATE author SET password = :password where id = :authorid;';$s = $pdo->prepare($sql);$password = get_md5_password($_POST['author_password']);$s->bindValue(':password', $password); $s->bindValue(':authorid', $authorid); $s->execute(); } catch (PDOException $e) {$error = 'Error updating password...'.$e->getMessage();include $_SERVER['DOCUMENT_ROOT'].'/PHP_MySQL_Web/includes/error.html.php';exit();} }// insert rolesif (isset($_POST['roles'])){foreach ($_POST['roles'] as $role){try {$role_role .= $role;$sql = 'INSERT INTO authorrole(authorid, roleid) VALUES (:authorid,:roleid);';$s = $pdo->prepare($sql);$s->bindValue(':authorid', $authorid);$s->bindValue(':roleid', $role);$s->execute();} catch (PDOException $e) {$error = 'Error insertting role...'.$e->getMessage();include $_SERVER['DOCUMENT_ROOT'].'/PHP_MySQL_Web/includes/error.html.php';exit();}}}header('Location: .');exit();}2.4 編輯作者
// edit authorsif (isset($_POST['action']) and $_POST['action'] == 'Edit') {include $_SERVER['DOCUMENT_ROOT'].'/PHP_MySQL_Web/includes/db.inc.php';// get list of authorstry {$sql = 'SELECT name, email FROM author WHERE id = :id';$s = $pdo->prepare($sql);$s->bindValue(':id', $_POST['id']);$s->execute();} catch (PDOException $e) {$error = 'Error getting list of roles to edit...'.$e->getMessage();include $_SERVER['DOCUMENT_ROOT'].'/PHP_MySQL_Web/includes/error.html.php';exit();}// get list of rolestry {$sql = 'SELECT authorid, roleid FROM authorrole WHERE authorid = :authorid';$s = $pdo->prepare($sql);$s->bindValue(':authorid', $_POST['id']);$s->execute(); $result = $s->fetchAll(); } catch (PDOException $e) {$error = 'Error getting list of roles to edit...'.$e->getMessage();include $_SERVER['DOCUMENT_ROOT'].'/PHP_MySQL_Web/includes/error.html.php';exit();} $roles_valid[] = array();foreach ($result as $row){$roles_valid[] = $row['roleid'];}// get roles belong to this author. try {$result = $pdo->query('SELECT id, description FROM role');foreach ($result as $row){$roles[] = array('id'=>$row['id'],'description'=>$row['description'],'selected'=>in_array($row['id'], $roles_valid));}} 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();}// get author info.try {$sql = 'SELECT name, email FROM author WHERE id = :id';$s = $pdo->prepare($sql);$s->bindValue(':id', $_POST['id']);$s->execute();$row = $s->fetch();$pageTitle = 'Edit author';$action = 'editForm';$author_name = $row['name'];$author_email = $row['email'];$id = $_POST['id'];$button = 'Update author';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();}}// edit authorsif (isset($_GET['editForm'])) {include $_SERVER['DOCUMENT_ROOT'].'/PHP_MySQL_Web/includes/db.inc.php';// update author infotry {$sql = 'UPDATE author SETname = :name,email = :emailWHERE id = :id;';$s = $pdo->prepare($sql);$s->bindValue(':name', $_POST['author_name']);$s->bindValue(':email', $_POST['author_email']);$s->bindValue(':id', $_POST['id']);$s->execute();} catch (PDOException $e) {$error = 'Error getting list of jokes to delete...'.$e->getMessage();include $_SERVER['DOCUMENT_ROOT'].'/PHP_MySQL_Web/includes/error.html.php';exit();}// add passwordif ($_POST['author_password'] != ''){try {$sql = 'UPDATE author SET password = :password where id = :authorid;';$s = $pdo->prepare($sql);$password = get_md5_password($_POST['author_password']);$s->bindValue(':password', $password);$s->bindValue(':authorid', $_POST['id']);$s->execute();} catch (PDOException $e) {$error = 'Error updating password...'.$e->getMessage();include $_SERVER['DOCUMENT_ROOT'].'/PHP_MySQL_Web/includes/error.html.php';exit();}}// delete old valuetry {$sql = 'DELETE FROM authorrole WHERE authorid = :authorid;';$s = $pdo->prepare($sql); $s->bindValue(':authorid', $_POST['id']); $s->execute();} catch (PDOException $e) {$error = 'Error deleting authorrole...'.$e->getMessage();include $_SERVER['DOCUMENT_ROOT'].'/PHP_MySQL_Web/includes/error.html.php';exit();}// insert new valuetry {// insert rolesif (isset($_POST['roles'])){foreach ($_POST['roles'] as $role){try {$sql = 'INSERT INTO authorrole(authorid, roleid) VALUES (:authorid,:roleid);';$s = $pdo->prepare($sql);$s->bindValue(':authorid', $_POST['id']);$s->bindValue(':roleid', $role);$s->execute();} catch (PDOException $e) {$error = 'Error insertting role...'.$e->getMessage();include $_SERVER['DOCUMENT_ROOT'].'/PHP_MySQL_Web/includes/error.html.php';exit();}}}} catch (PDOException $e) {$error = 'Error insertting authorrole...'.$e->getMessage();include $_SERVER['DOCUMENT_ROOT'].'/PHP_MySQL_Web/includes/error.html.php';exit();}header('Location: .');exit();}2.6 刪除作者
if (isset($_POST['action']) and $_POST['action'] == 'Delete') { include $_SERVER['DOCUMENT_ROOT'].'/PHP_MySQL_Web/includes/db.inc.php';// get jokes belonging to authortry {$sql = 'SELECT id FROM joke WHERE authorid = :id';$s = $pdo->prepare($sql);$s->bindValue(':id', $_POST['id']);$s->execute(); } catch (PDOException $e) {$error = 'Error getting list of jokes to delete...'.$e->getMessage();include $_SERVER['DOCUMENT_ROOT'].'/PHP_MySQL_Web/includes/error.html.php';exit();}$result = $s->fetchAll();// delete joke category entriestry {$sql = 'DELETE FROM jokecategory WHERE jokeid = :jokeid';$s = $pdo->prepare($sql);// delete the joke by idforeach ($result as $row) {$jokeId = $row['id'];$s->bindValue(':jokeid', $jokeId);$s->execute();}} catch (PDOException $e) {$error = 'Error deleting jokecategory...'.$e->getMessage();include $_SERVER['DOCUMENT_ROOT'].'/PHP_MySQL_Web/includes/error.html.php';exit();}// delete jokes belongto authortry {$sql = 'DELETE FROM joke WHERE authorid = :id';$s = $pdo->prepare($sql);$s->bindValue(':id', $_POST['id']);$s->execute();} catch (PDOException $e) {$error = 'Error deleting joke...';include $_SERVER['DOCUMENT_ROOT'].'/PHP_MySQL_Web/includes/error.html.php';exit();}// delete author rolestry {$sql = 'DELETE FROM authorrole WHERE authorid = :authorid';$s = $pdo->prepare($sql);$s->bindValue(':authorid', $_POST['id']);$s->execute();} catch (PDOException $e) {$error = 'Error deleting authorrole...'.$e->getMessage();include $_SERVER['DOCUMENT_ROOT'].'/PHP_MySQL_Web/includes/error.html.php';exit();}// delete authortry {$sql = 'DELETE FROM author WHERE id = :id';$s = $pdo->prepare($sql);$s->bindValue(':id', $_POST['id']);$s->execute();} catch (PDOException $e) {$error = 'Error deleting author...'.$e->getMessage();include $_SERVER['DOCUMENT_ROOT'].'/PHP_MySQL_Web/includes/error.html.php';exit();}header('Location: .');exit();}2.7 顯示作者列表
// select all authors.try {include $_SERVER['DOCUMENT_ROOT'].'/PHP_MySQL_Web/includes/db.inc.php';$querySql = "SELECT id, name, email FROM author;";$result = $pdo->query($querySql); foreach ($result as $row) {$authors[] = array('id'=>$row['id'], 'name'=>$row['name'], 'email'=>$row['email']); } if (count($authors) > 0) {$sql = "SELECT table_rows AS RCT FROM information_schema.tables WHERE table_name = 'author';";$s = $pdo->prepare($sql);$s->execute();$row = $s->fetch();$rowCount = $row['RCT'];include $_SERVER['DOCUMENT_ROOT'].'/PHP_MySQL_Web/admin/authors/authors.html.php';} else {} } catch (PDOException $e) {$error = "query author failed...".$e->getMessage();include $_SERVER['DOCUMENT_ROOT'].'/PHP_MySQL_Web/includes/error.html.php';exit();}轉(zhuǎn)載于:https://www.cnblogs.com/BigBigLiang/p/4934236.html
總結(jié)
以上是生活随笔為你收集整理的PHP和MySQL Web开发从新手到高手,第7天-创建author管理页面的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: spring-data-jpa 查询视图
- 下一篇: This function has no