0

我正在尝试通过数组将多个值插入 MySQL,但它不起作用或传递错误消息,所以我不确定我哪里出错了。任何帮助,将不胜感激。

这是我调用函数的地方

$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();
}
4

2 回答 2

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);

    ...

}
于 2013-03-18T22:38:25.297 回答
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'));
于 2013-03-19T06:56:01.937 回答