Редактируете вы тут что? Сначала надо получить и менять ИД т.е. первичный ключ у записи нехорошо.
class Book{
protected $db;
public function __construct($db){
$this->db = $db;
}
private function error($message){
throw new Exception($message);
}
public function create(array $post){
$param = array('name', 'author_id', 'price');
$add = array();
foreach($param as $val){
if(!empty($post[$val])){
$add[':'.$val] = trim($post[$val]);
}else{
$this->error('Отсутствует значение: '.$val);
}
}
$stmt = $this->db->prepare("INSERT INTO `book` (`date_add`, `name`, `author_id`, `price`) VALUE (NOW(), :name, :author_id, :price)");
return $stmt->execute($add);
}
public function update(array $post){
if(empty($post['id']) && !is_numeric($post['id'])){
$this->error('empty ID');
}
$e= array();
$param = array('id', 'name', 'author_id', 'price');
foreach($param as $val){
//...
}
$stmt = $this->db->prepare("UPDATE `book` SET ........ WHERE `id` = :id");
return $stmt->execute($e);
}
public function delete($id){
$stmt = $this->db->prepare("DELETE FROM `book` WHERE `id` = ?");
return $stmt->execute(array($id));
}
private function select(array $param){
$where = array();
$z = array();
if(!empty($param['id'])){
$where[] = "`id` = :id";
$z[':user'] = $param['author_id'];
}
if(!empty($param['author_id'])){
$where[] = "`author_id` = :author_id";
$z[':author_id'] = $param['author_id'];
}
$query = "SELECT * FROM `book`";
if(!empty($where)){
$query .= " WHERE ".implode(' AND ', $where);
}
$stmt = $this->db->prepare($query);
$stmt->execute($z);
return $stmt->fetchAll();
}
public function showAll(){
return $this->select(array());
}
public function showOne($id){
return $this->select(array('id' => $id));
}
}
class Book2 extends Book{
public function showOnlyAuthor($id){
return $this->select(array('author_id' => $id));
}
}