user3345570
user3345570

Reputation:

PDO CRU are not functioning

I need your help figuring this out. I am trying to have a reserve a book functionality in my project. I don't have any error with this one but my oop functions that contains the pdo statements won't work. Particulary with the insert (values can't be inserted into the database) and update(can't update existing info from the database) part. I don't know why this happens.

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>";
  }

?>

codex_books.php

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;
}

Upvotes: 0

Views: 46

Answers (1)

Marc B
Marc B

Reputation: 360762

Given:

$q = "UPDATE $table SET quantity = ':newstock' where book_title = ':book_title'";
                                                                   ^^^^^^^^^^^

Where's book_title here?

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

You really MUST check return values from your DB calls for boolean FALSE, indicating failure. You're simply assuming everything will always succeed, which is a very BAD way of writing code.

Upvotes: 1

Related Questions