0

由于某种原因,这不起作用,相同的循环适用于 SELECT 语句,但不适用于 INSERT 语句。

 $sql = "INSERT INTO users(user_id,password) VALUES(?,?)";
 $data = array();
 $data[1] = "1";
 $data[2] = "password";
 public function insert($sql,$data){
    $newQuery = $this->db->prepare($sql);
    //pass $value as a reference to the array item
    foreach ($data as $key => &$value) {  
    // bind the variable to the statement
        $newQuery->bindParam($key,$value); 
    } 
    $newQuery->execute();
}
4

0 回答 0