Php PDO CRU不工作

Php PDO CRU不工作,php,pdo,Php,Pdo,我需要你帮我弄清楚这件事。我正试图在我的项目中保留一本书的功能。我没有任何错误,但是包含pdo语句的oop函数将无法工作。特别是插入(不能将值插入数据库)和更新(不能从数据库更新现有信息)部分。我不知道为什么会这样 bookReserve.php <?php session_start(); include_once "../styles/header-menu-out.php"; include_once "dbconnection.php";

我需要你帮我弄清楚这件事。我正试图在我的项目中保留一本书的功能。我没有任何错误,但是包含pdo语句的oop函数将无法工作。特别是插入(不能将值插入数据库)和更新(不能从数据库更新现有信息)部分。我不知道为什么会这样

bookReserve.php

    <?php
    session_start();
    include_once "../styles/header-menu-out.php";
    include_once "dbconnection.php";


    function __autoload($class){
    include_once("../main/".$class.".php");}

  $code = new codex_books();

  $sname = $_POST['sname'];
  $sid = $_POST['sid'];
  $id = $_POST['id'];
  $title = $_POST['title'];
  $author = $_POST['author'];
  $isbn = $_POST['isbn'];
  $publisher = $_POST['publisher'];
  $language = $_POST['language'];
  $genre = $_POST['genre'];
  $quantity = $_POST['quantity'];
  $date_to_be_borrow = $_POST['date_to_be_borrow'];

  $result = $code->bookreserve($id,"book_info");

  if(isset($_POST['reserve']))
  {
      foreach($result as $row)
      {
          echo $oldstock=$row['quantity']; 
      }

      echo $newstock = $oldstock-1;

      $code->minusbookreserve($quantity, $newstock,"book_info");
      $code->insertbookreserve($sid,$sname,$title,$author,$isbn,$publisher,$language,$genre,$quantity,$date_to_be_borrow,"reserve_list");
      // echo "<script type='text/javascript'>alert('Successfully Reserved.');window.location='bookReservelist.php';</script>";
  }
  else {
    echo "<script type='text/javascript'>alert('Something went wrong.');window.location='bookReservelist.php';</script>";
  }

?>
public function minusbookreserve($quantity, $newstock, $table)
{
  $q = "UPDATE $table SET quantity = ':newstock' where book_title = ':book_title'";
  $stmt = $this->con->prepare($q);
  $stmt->execute(array(':newstock'=>$newstock, ':quantity'=>$quantity));
   if($stmt){
    return true;
    }
  else {
      return false;
  }
}

public function insertbookreserve($sid,$sname,$title,$author,$isbn,$publisher,$language,$genre,$quantity,$date_to_be_borrow,$table)
{
  $q = "INSERT INTO $table SET sid= :sid ,sname=:sname,title=:title,author=:author,isbn=:isbn,publisher=:publisher,language=:language, genre=:genre, quantity=:quantity, date_to_be_borrow=:date_to_be_borrow";
  $stmt = $this->con->prepare($q);
  $stmt->execute(array(':sid'=>$sid,':sname'=>$sname,':title'=>$title,':author'=>$author,':isbn'=>$isbn,':publisher'=>$publisher,':language'=>$language, ':genre'=>$genre,':quantity'=>$quantity,':date_to_be_borrow'=>$date_to_be_borrow));
  return true;
}
鉴于:

书名在哪里

$stmt->execute(array(':newstock'=>$newstock, ':quantity'=>$quantity));

您确实必须检查DB调用的返回值是否为布尔值
FALSE
,这表示失败。你只是假设一切都会成功,这是一种非常糟糕的代码编写方式。

删除
SET quantity=':newstock'where book\u title=':book\u title'
中的引号,理解为
SET quantity=:newstock where book\u title=:book\u title
“不起作用”是一种模糊的解释方式。什么不起作用?期望的结果是什么?究竟发生了什么?PDO使用哪种错误模式?我已经照你说的做了。。它给了我这个错误:PDOStatement::execute()[]:SQLSTATE[HY093]:无效的参数编号:参数未定义您有
newstock
book\u title
但您正在以
newstock
数量
的形式执行数组。这里似乎有点不平衡@user3345570,这很可能是无效参数错误的原因。@Mike对此表示抱歉,先生。我已经编辑过了。啊,对。我已经进行了更正,但它仍然给我这个错误。SQLSTATE[HY093]
$stmt->execute(array(':newstock'=>$newstock, ':quantity'=>$quantity));