php amp mysql web开发6_PHP和MySQL Web开发从新手到高手,第6天-创建joke管理页面

//edit a joke

if (isset($_POST['action']) && $_POST['action'] == 'Edit') {//build list of authors

try{$statement = 'select id, name from author';$result = $pdo->query($statement);foreach($result as $row) {$authors[] = array('id'=>$row['id'], 'name'=>$row['name']);

}

}catch (PDOException $e) {$error = 'Query authors faild...'

.$e->getMessage();include $_SERVER['DOCUMENT_ROOT'].'/PHP_MySQL_Web/includes/error.html.php';exit();

}//build list of categories

try{$statement = 'select distinct categoryid from jokecategory where jokeid=:jokeid';$s = $pdo->prepare($statement);$s->bindValue(':jokeid', $_POST['id']);$s->execute();$idCount = $s->rowCount();while ($row = $s->fetch()) {$categoryids_of_joke[] = $row['categoryid'];

}

}catch (PDOException $e) {$error = 'Query categories faild...'

.$e->getMessage();include $_SERVER['DOCUMENT_ROOT'].'/PHP_MySQL_Web/includes/error.html.php';exit();

}try{$statement = 'select id,name from category;';$result = $pdo->query($statement);foreach($result as $row) {if ($idCount > 0)

{$categories[] = array('id'=>$row['id'], 'name'=>$row['name'], 'selected' => in_array($row['id'], $categoryids_of_joke));

}else{$categories[] = array('id'=>$row['id'], 'name'=>$row['name'], 'selected' => false);

}

}

}catch (Exception $e) {$error = 'Query category faild...'

.$e->getMessage();include $_SERVER['DOCUMENT_ROOT'].'/PHP_MySQL_Web/includes/error.html.php';exit();

}try{$sql = 'SELECT joketext,jokedate,authorid FROM joke WHERE id=:id';$s = $pdo->prepare($sql);$s->bindValue(':id', $_POST['id']);$s->execute();$row = $s->fetch();$jokeText = $row['joketext'];$pageTitle = 'Edit joke';$action = 'editJoke';$id = $_POST['id'];$authorid = $row['authorid'];$button = 'Update joke';include 'form.html.php';exit();

}catch (Exception $e) {$error = 'Error Edit joke ...'

.$e->getMessage();include $_SERVER['DOCUMENT_ROOT'].'/PHP_MySQL_Web/includes/error.html.php';exit();

}

}if (isset($_GET['editJoke']) && $_POST['jokeText'] != '') {if ($_POST['author'] == '' || !isset($_POST['categories'])){$error = 'Please select author and category...';include $_SERVER['DOCUMENT_ROOT'].'/PHP_MySQL_Web/includes/error.html.php';exit();

}//update jokeText

try{$statement = 'update joke set joketext = :joketext, authorid=:authorid where id=:id';$s = $pdo->prepare($statement);$s->bindValue(':joketext', $_POST['jokeText']);$s->bindValue(':authorid', $_POST['author']);$s->bindValue(':id', $_POST['id']);$s->execute();

}catch (Exception $e) {$error = 'Error update joke to databases...'

.$e->getMessage();include $_SERVER['DOCUMENT_ROOT'].'/PHP_MySQL_Web/includes/error.html.php';exit();

}//update joke jokecategory , step 1 delete old items, step 2 insert new items.

// step 1 delete old items

try{$statement = 'delete from jokecategory where jokeid=:jokeid';$s = $pdo->prepare($statement);$s->bindValue(':jokeid', $_POST['id']);$s->execute();

}catch (Exception $e) {$error = 'Error deleting jokecategory to databases...'

.$e->getMessage();include $_SERVER['DOCUMENT_ROOT'].'/PHP_MySQL_Web/includes/error.html.php';exit();

}//step 2 insert new items.

try{$statement = 'insert into jokecategory(jokeid,categoryid) values(:jokeid,:categoryid)';$s = $pdo->prepare($statement);foreach ($_POST['categories'] as $categoryid)

{$s->bindValue(':jokeid', $_POST['id']);$s->bindValue(':categoryid', $categoryid);$s->execute();

}

}catch (Exception $e) {$error = 'Error deleting joke to databases...'

.$e->getMessage();include $_SERVER['DOCUMENT_ROOT'].'/PHP_MySQL_Web/includes/error.html.php';exit();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值