2014-03-25 79 views
0

我試圖插入到數據庫使用PDO,但完成腳本和測試後,我沒有看到任何輸入插入數據庫,我試過從錯誤返回PDO但沒有。我不確定發生了什麼SQLite準備好的陳述沒有插入

我更新了參數名稱,它們似乎沒有改變任何代碼的結果。似乎q()函數只是有問題。

數據庫初始化,看起來像:

function __construct() { 
     if (! file_exists(dbpath . DB)) { 
      $this->db = true; 
      $this->open(dbpath . DB, SQLITE3_OPEN_CREATE|SQLITE3_OPEN_READWRITE) or $this->db = false; 
      if ($this->db == false) { 
       return false; 
      } 
      $table = "CREATE TABLE link_hits(id INTEGER PRIMARY KEY AUTOINCREMENT, link TEXT NOT NULL, hits INT NOT NULL, date_added datetime default current_timestamp)";  
      $this->exec($table); 
     } else { 
      $this->db = true; 
      $this->open(dbpath . DB, SQLITE3_OPEN_CREATE|SQLITE3_OPEN_READWRITE) or $this->db = false; 
      if ($this->db == false) { 
       return false; 
      } 
     } 
    } 

預處理語句功能

function q ($q, $v) { 
     if ($this->db) { 
      $this->securedb = $this->prepare($q); 
      foreach ($v as $k=>$vv) { 
       if (is_numeric($vv)) { 
        $this->securedb->bindValue($k, $vv, SQLITE3_INTEGER); 
       } else { 
        $this->securedb->bindValue($k, $vv, SQLITE3_TEXT); 
       } 
      } 
      $this->errorInfo = $this->errorInfo(); 
      return (($this->handle = $this->execute()) == true) ? $this->handle : false; 
     } 
     return false; 
    } 

添加鏈接功能

function addlink ($link) { 

     if ($this->linkexists($link)) { 
      return false; 
     } 

     $this->que = "INSERT INTO link_hits (link, hits) VALUES (:link, :hits)"; 
     $this->input = array(
          ':link' => $link, 
          ':hits' => 0 
          ); 
     return ($this->q($this->que, $this->input)) ? true : false; 

    } 

我這不是正確形成我的發言?我遵循了幾個教程。我真的很習慣MySQL,但不會在這種情況下訪問它。 :(

即使我linkexists函數拋出一個錯誤是的,鏈接是存在的,我強迫它與普通查詢

function linkexists($link) { 

     $this->que = "SELECT link FROM link_hits WHERE type='table' AND link=':link'"; 
     $this->input = array(
        ':link' => $link 
       ); 
     return (($this->handle = $this->q($this->que, $this->input)) == true) ? true : false; 

    } 
+0

爲什麼你在你的綁定變量旁邊附加';' –

+0

在幾個例子中看到它只是一個模式任何東西都可以使用IE * {|} * key;:; ...至少遠到我可以告訴 – WASasquatch

+0

在'linkexists'語句中有很多引號; PDO會插入這些au tomatically。試試這個: 'SELECT link FROM link_hits WHERE type =:type AND link =:link' - 但是對於你的主要問題我沒有任何線索。 :) – fboes

回答

0

Parameter names不能以分號結束:。

$this->que = "INSERT ... VALUES (:link, :hits)"; 
+0

我改變了參數,但仍然是相同的情況。即使我的linkexiss拋出一個錯誤。 – WASasquatch