Pdo插入问题

时间:2014-03-20 17:47:48

标签: php pdo

大家好! 为什么每次我尝试插入与已经插入的具有相同“sid”的东西时,它都不会插入到db中。

例如1st insert = sid:1,sname:cat,title:the giver

第二次插入= sid:1,sname:cat,title:fangirl< - 第二次插入不会作为另一项插入数据库。我甚至没有尝试过任何条件。

这个问题的原因似乎是什么?还有一件事,当我尝试选择其桌子上的所有项目时,它只会显示最多四个项目。如果有新的条目,它将取代旧的条目,但仍然没有对这些事情设置任何条件。

对于show函数:

public function showData($table)
    {
        $q = "SELECT * FROM $table";
        $stmt = $this->con->query($q);
        $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
        return $result;
    }

对于插入功能:

    public function insertbookborrow($sid,$sname,$title,$author,$isbn,$publisher,$language,$genre,$borrow_date,$return_date,$table)
    {
      $q = "INSERT INTO $table SET sid=:sid,sname=:sname,title=:title,author=:author,isbn=:isbn,publisher=:publisher,language=:language, genre=:genre,borrow_date=:borrow_date, return_date=:return_date";
      $stmt = $this->con->prepare($q);
      $stmt->execute(array(':sid'=>$sid,':sname'=>$sname,':title'=>$title,':author'=>$author,':isbn'=>$isbn,':publisher'=>$publisher,':language'=>$language,':genre'=>$genre,':borrow_date'=>$borrow_date,':return_date'=>$return_date));
      if($stmt){
            return true;
            }
          else {
              return false;
          }
    }

对于流程部分

 <?php
    session_start();
    include_once "dbconnection.php";

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

  $code = new 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'];
  $borrow_date = $_POST['borrow_date'];
  $return_date = $_POST['return_date'];

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

  if(isset($_POST['borrow']))
  {
      foreach($result as $row)
      {
          $oldstock=$row['quantity']; 
          if ($oldstock<1) {
            echo "<script type='text/javascript'>alert('No more books left to be reserved.');window.location='bookBorrowlist.php';</script>";
          }
          else
          {
            try{
                  $code->minusbookreserve($quantity,$id,"book_info");
                  $code->insertbookborrow($sid,$sname,$title,$author,$isbn,$publisher,$language,$genre,$borrow_date,$return_date,"borrow_list");

                  echo "<script type='text/javascript'>
                  alert('You may check out the book. Overdue fine = 5 php / day.');
                  window.location='bookBorrowlist.php';
                  </script>";

            }catch(PDOException $e)
              {
                  echo $e->getMessage();
              }

          }
      }

  }
  else {
    echo "<script type='text/javascript'>alert('Something went wrong.');window.location='bookBorrowlist.php';</script>";
  }

?>

0 个答案:

没有答案