2013-03-18 68 views
0

我試圖通過數組插入多個值到MySQL中,但它不工作或傳遞錯誤消息,所以我不知道我要去哪裏出錯。任何幫助,將不勝感激。PHP mysqli插入命令

這裏就是我所說的功能

$testArrayList = array(); 
      $testArrayList[] = 'Account_idAccount'; 
      $testArrayList[] = 'firstName'; 
      $testArrayList[] = 'lastName'; 
      $testArrayValues = array(); 
      $testArrayValues[] = $idAccount; 
      $testArrayValues[] = $firstName; 
      $testArrayValues[] = $lastName; 
      $dbManager->insertValues("User", $testArrayList, $testArrayValues); 

現在,這裏是功能可按被稱爲insertValues。

 public function insertValues($table, $cols, $values) { 
    foreach ($cols as $col) 
     $colString .= $col.','; 
    foreach ($values as $value) 
    { 
     $valueAmount .= '?,'; 
     $valueType .= 's'; 
     $valueParam .= $value.","; 
    } 
    $colString = substr($colString, 0, -1); 
    $valueAmount = substr($valueAmount, 0, -1); 
    $valueParam = substr($valueParam, 0, -1); 

    $mysqli = new mysqli(DBHOST, DBUSER, DBPASSWORD, DBDATABASE); 
    $sql = "INSERT INTO $table ($colString) VALUES($valueAmount)"; 
    /* Prepared statement, stage 1: prepare */ 
    if (!($stmt = $mysqli->prepare($sql))) { 
     echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error; 
    } 
    print_r($valueParam); 
    /* Prepared statement, stage 2: bind and execute */ 
    if (!$stmt->bind_param("$valueType", $valueParam)) { 
     echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error; 
    } 

    if (!$stmt->execute()) { 
     echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error; 
    } 
    /* explicit close recommended */ 
    $stmt->close(); 
    $mysqli->close(); 
} 
+1

那你得到什麼錯誤? – 2013-03-18 21:57:07

+0

我沒有收到任何錯誤。我只知道它不工作,因爲我在數據庫中看不到我的值。我注意到,之前我會圍繞 - > bindParam函數引用引號,以至於我會得到錯誤,現在我擁有它的方式我沒有得到錯誤,但我也沒有任何值數據庫。 – AlexHeuman 2013-03-18 22:02:16

+1

你有沒有做過任何調試? – 2013-03-18 22:02:52

回答

1

有一堆錯誤出現的,這裏有一個重寫的版本,你的功能應該工作:

public function insertValues($table, array $cols, array $values) { 

    $mysqli = new mysqli(DBHOST, DBUSER, DBPASSWORD, DBDATABASE); 

    $colString = implode(', ', $cols); // x, x, x 
    $valString = implode(', ', array_fill(0, count($values), '?')); // ?, ?, ? 

    $sql = "INSERT INTO $table ($colString) VALUES($valString)"; 
    if (!$stmt = $mysqli->prepare($sql)) 
     echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error; 

    foreach ($values as $v) 
     if (!$stmt->bind_param('s', $v)) 
      echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error; 

    if (!$stmt->execute()) 
     echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error; 

    $stmt->close(); 
    $mysqli->close(); 

} 

你應該還有一次是在構造函數,而不是每種方法初始化的mysqli連接:

public function __construct() { 
    $this->mysqli = new mysqli(DBHOST, DBUSER, DBPASSWORD, DBDATABASE); 
} 

public function __destruct() { 
    $this->mysqli->close(); 
} 

而且它,你創建一個合適的函數來處理這些錯誤,如很好

public function showError($message, object $obj) { 
    echo "$message: (" . $obj->errno . ") " . $obj->error; 
} 

導致你這個清潔版本功能:

public function insertValues($table, $cols, $values) { 

    ... 

    if (!$stmt = $mysqli->prepare($sql)) 
     $this->showError("Prepare failed", $mysqli); 

    foreach ($values as $v) 
     if (!$stmt->bind_param('s', $v)) 
      $this->showError("Binding parameters failed", $stmt); 

    if (!$stmt->execute()) 
     $this->showError("Execute failed", $stmt); 

    ... 

} 
+0

非常感謝您重寫我的功能。不幸的是它仍然無法正常工作。我正在絞盡腦汁在這一張上。 – AlexHeuman 2013-03-18 22:47:11

+0

@AlexHeuman,恐怕這不是他們的功能問題。 – Shoe 2013-03-18 22:48:19

+0

我很高興我現在知道,並感謝您的詳細解答。它也似乎$ values數組被打印到屏幕上,但我不打印在任何地方。無論如何,感謝您的幫助。 – AlexHeuman 2013-03-18 22:57:31

0

我已經重寫功能讓您可以清楚瑟爲什麼你使用bind_param()是錯誤的。

這個版本只是一個例子,它僅適用於2列!

function insertValues($table, array $cols, array $values) { 

     $mysqli = new mysqli('localhost', 'petr', null,'test'); 

     $colString = implode(', ', $cols); // x, x, x 
     $valString = implode(', ', array_fill(0, count($values), '?')); // ?, ?, ? 

     $sql = "INSERT INTO $table ($colString) VALUES($valString)"; 
     if (!$stmt = $mysqli->prepare($sql)) 
      echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error; 

     list($a,$b) = $values; 
     // params $a and $b must exists during $stmt execution, therefore you can't use foreach with temproray variable 
     if (!$stmt->bind_param('ss', $a, $b)) 
       echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error; 

     if (!$stmt->execute()) 
      echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error; 

     $stmt->close(); 
     $mysqli->close(); 

    } 

這工作:

insertValues('test',array('firstName','lastName'),array('Jan Amos','Komensky')); 
+0

謝謝,我想我可以使用bind_param中的一個字符串,但我錯了。謝謝你的幫助。 – AlexHeuman 2013-03-19 23:48:09

+1

不要對「有用」的箭頭表示感謝和讚揚:) – Petr 2013-03-20 11:50:19