2013-03-18 75 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