2017-08-11 121 views
2

我在這小小的一段代碼stucked:PHP PDO - INSERT INTO與bindParam不起作用

我已經使用PDO來選擇的東西從我的數據庫,但將東西
$fruit_object = new fruit(1234, 'apple', 'red apple');  

try { 
    $dbh = new PDO('mysql:host=localhost;charset=utf8;dbname=database', 'user', 'password'); 
    $exc = $dbh->prepare("INSERT INTO fruit(type, name) VALUES (:type, :name);");        
    //$exc->bindParam(':id', $fruit_object->id, PDO::PARAM_INT); 
    $exc->bindParam(':type', $fruit_object->type, PDO::PARAM_STR); 
    $exc->bindParam(':name', $fruit_object->name, PDO::PARAM_STR); 
    $exc->execute(); 
    $dbh = null; 
    $exc = null; 
} catch (PDOException $e) { 
    //this function just do an echo with static content 
    $this->error_database($e); 
    $dbh = null; 
    $exc = null; 
    die(); 
} 

,這是行不通的。用戶只能訪問INSERT的東西 - 我已經在我的後端成功地嘗試過了。

因此,這裏的錯誤:

Fatal error: Uncaught Error: Cannot access private property fruit_object::$type 

這是我fruit_object類:

<?php 


class fruit 
{ 
private $id; 
private $type; 
private $name; 

function __construct($id, $type, $name) 
{ 
    $this->id = $id; 
    $this->type = $type; 
    $this->name = $name; 

} 

function __toString() 
{ 
    return $this->name; 
} 
} 

馬丁:

INSERT INTO fruit (id, type, name) VALUES (DEFAULT, 'apple', 'red apple'); 

我的數據庫是MySQL服務器上運行 - 是這是什麼原因?我必須使用問號(?)嗎?

謝謝你,路易斯

+1

什麼是錯誤? – Dekel

+0

顯示沒有錯誤。 – louis12356

+1

當你指示它......你是否這樣做時,PDO只會拋出異常?如果沒有,去研究它。 – CBroe

回答

2

回答問題1:

PHP PDO - INSERT INTO與bindParam不起作用

If you're inserting an ID into an auto increment field and you've already inserted then it will cause a MySQL error (dupliate A_I field value) – Martin


Yeah i know that, I am using the DEFAULT keyword in my real statement. – louis12356


explain; default keyword for what? – Martin


There is a SQL keyword 'DEFAULT' which automaticly counts the ID up. – louis12356


您應該爲而不是爲您的自動增量(id)列提供一個值。看起來像是你給MySQL指令通過PDO變量這將永不工作。這是因爲PDO使用準備好的陳述等變量只會變量,並且永遠不會指令

MySQL中的Default關鍵字是MySQL程序的指令。該指令將被忽略,不僅因爲它是不允許的,但也因爲你傳遞一個STRING值PDO INSERT聲稱它應該是一個INT

$exc->bindParam(':id', $fruit_object->id, PDO::PARAM_INT); 

如果$fruit_object->id == "DEFAULT"這是不是一個整數;所以PDO不會運行查詢。

解決方案

自動遞增值根本不需要插入,忽略它們:

INSERT INTO fruit (id, type, name) VALUES (DEFAULT, 'apple', 'red apple'); 
:你正在試圖運行什麼

try { 
    $dbh = new PDO('mysql:host=localhost;charset=utf8;dbname=database', 'user', 'password'); 
    $exc = $dbh->prepare("INSERT INTO fruit(type, name) VALUES (:type, :name);");        
    // $exc->bindParam(':id', $fruit_object->id, PDO::PARAM_INT); 
    $exc->bindParam(':type', $fruit_object->type, PDO::PARAM_STR); 
    $exc->bindParam(':name', $fruit_object->name, PDO::PARAM_STR); 
    $exc->execute(); 
    $dbh = null; 
    $exc = null; 
} 

但由於PDO的安全限制(忽略String/Int數據類型問題)究竟是正在運行的是:

INSERT INTO fruit (id, type, name) 
    VALUES (<int var> "DEFAULT", <str var> "apple", <str var> "red apple"); 

所以你想插入字符串變量「默認」爲整列在MySQL


回答問題2:

Fatal error: Uncaught Error: Cannot access private property fruit_object::$type 

這是由於您的課程將此類型的值設置爲Private而不是Public,這意味着該值無法顯示在cla的外部SS(這是一個輕微的過於簡單,但時間緊迫我!)

你需要做的是兩種:

  • 設置你的價值觀前往的交通到是public
  • OR,打造setter and getter methods成你的班級,所以你可以隨時抽出這些私人價值,只要你想(哦,女主人!)。

所以:

class fruit 
{ 
    private $id; 
    private $type; 
    private $name; 

    /*** 
    * Getter 
    ***/ 
    function getType() 
    { 
     return $this->type; 
    } 

    /*** 
    * Setter 
    ***/ 
    function setType($value){ 
     $this->type = $value; 
    } 
} 
在PDO

然後:

$exc->bindParam(':type', $fruit_object->getType(), PDO::PARAM_STR); 

這將輸出你的價值的腳本。

如果你想有一個更簡單的方法,你可以簡單地替換private $name;public $name;,然後命名變量的值是從類的外部訪問:

$exc->bindParam(':name', $fruit_object->name, PDO::PARAM_STR); 
+0

是的,我不想在上面的例子中做到這一點,但在我的真實代碼。我前幾次瞭解到,「DEFAULT」關鍵字可以在ORACLE SQL中找到。我會回來的。 – louis12356

+0

@ louis12356它本來可以在某些SQL中工作,但PDO非常安全,並且不會將指令值插入任何SQL,無論在任何地方。這就是爲什麼它比'mysql_'更安全。它也是錯誤的數據類型(字符串不是int),MySQL也不像Oracle那樣使用DEFAULT。你只是試着用駕駛拖拉機的知識駕駛摩托車。完全不同。 – Martin

+0

這就是爲什麼我在這裏。 – louis12356

2

你缺少右大括號準備和分號

$exc = $dbh->prepare("INSERT INTO fruit(id, type, name) VALUES (:id, :type, :name)"); 

如果沒有的話工作添加此行如果用它來檢查錯誤

print_r($exc->errorInfo()); 
+0

這就是對的,但它仍然無法正常工作。 – louis12356

+0

現在,你是否收到任何錯誤或警告?還要確保錯誤報告在 –

0

( ?)將是下一個代碼

$exc = $dbh->prepare("INSERT INTO fruit(id, type, name) VALUES (?, ?, ?)");        
$exc->bindParam(1, $fruit_object->id, PDO::PARAM_INT); 
$exc->bindParam(2, $fruit_object->type, PDO::PARAM_STR); 
$exc->bindParam(3, $fruit_object->name, PDO::PARAM_STR); 
$exc->execute(); 
+0

好的,謝謝。 – louis12356

+0

這個答案沒有達到任何效果 – Martin