PHP使用类方法更新多行

时间:2017-11-18 02:28:23

标签: php mysql

我有一个类方法inserMultiplerecords,即时尝试将其转换为更新多个记录的工作。这有可能吗?

public function insertMultiple($tableName, $data){
    //Will contain SQL snippets.
    $rowsSQL = array();

    //Will contain the values that we need to bind.
    $toBind = array();

    //Get a list of column names to use in the SQL statement.
    $columnNames = array_keys($data[0]);

    //Loop through our $data array.
    foreach($data as $arrayIndex => $row){
        $params = array();
        foreach($row as $columnName => $columnValue){
            $param = ":" . $columnName . $arrayIndex;
            $params[] = $param;
            $toBind[$param] = $columnValue; 
        }
        $rowsSQL[] = "(" . implode(", ", $params) . ")";
    }

    //Construct our SQL statement
    $sql = "INSERT INTO `$tableName` (" . implode(", ", $columnNames) . ") VALUES " . implode(", ", $rowsSQL);

    //Prepare our PDO statement.
    $pdoStatement = $this->prepare($sql);

    //Bind our values.
    foreach($toBind as $param => $val){
        $pdoStatement->bindValue($param, $val);
    }
    //Execute our statement (i.e. insert the data).
    return $pdoStatement->execute();
}

任何人都可以帮我转换它来更新记录吗?提前谢谢

0 个答案:

没有答案