1

MySQLテーブルにデータを挿入するために使用しようとしているPDOがいくつかあります。

private function addResource() {
    include('./dbconnect.php');
    $pdo = new PDO("mysql:host=$db_host;dbname=$db_name;", $db_user, $db_password);
    $stmt = $pdo->prepare('INSERT INTO Resources VALUES (?, $title, $url, $_SESSION[\'tblUserID\'');
    $stmt->bindParam(1, $title);
    $stmt->bindParam(2, $url);
    $stmt->bindParam(3, $_SESSION['tblUserID']);
    $stmt->execute();
    if ($stmt->rowCount() != 1)
        throw new Exception('Could not add resource');
    $status = true;
}

テーブルをチェックするたびに、何も挿入されていません。どうして?

編集: ページの上部に session_start() があります。

4

2 に答える 2

0

これを試して:

private function addResource() {
      include('./dbconnect.php');
      try{
          $pdo = new PDO("mysql:host=$db_host;dbname=$db_name;", $db_user, $db_password);
          $stmt = $pdo->prepare('INSERT INTO Resources VALUES (:title, :url, :userid)';
          $stmt->bindParam(':title', $title);
          $stmt->bindParam(':url', $url);
          $stmt->bindParam(':userid', $_SESSION['tblUserID']);
          $stmt->execute();
          if ($stmt->rowCount() != 1)
            throw new Exception('Could not add resource');
          $status = true;
          }
       }catch (Exception $e){
          echo $e->getMessage();
          exit;
       }
    }

参照: http://php.net/manual/en/pdo.prepared-statements.php

于 2013-05-06T17:24:21.543 に答える