//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();
}
}