我是OOP和PDO的新手,我的功能有问题。每当我点击更新按钮时,它都不会更新,而是为我创建一个新成员。
public function updateAll($data){
try {
$r = $this->db->query("UPDATE addmember SET(
Pic = '".$data[0]."',
Firstname= '".$data[1]."',
Lastname= '".$data[2]."',
Age= '".$data[3]."',
Gender= '".$data[4]."',
Phonenumber='".$data[5]."',
Location='".$data[6]."',
Member='".$data[7]."',
Department='".$data[8]."'
WHERE id='".$data[9]."'
)");
if ($r) {
return true;
}
else{
return false;
}
} catch (PDOException $e) {
echo $e->getMessage();
}
}
答案 0 :(得分:1)
未经测试但您可以将PDO用于此类预备语句。
public function updateAll($data=false){
try {
if( !$data ) return false;
$sql='update `addmember` set
`pic`=:pic,
`firstname`=:firstname,
`lastname`=:lastname,
`age`=:age,
`gender`=:gender,
`phonenumber`=:phonenumber,
`location`=:location,
`member`=:member,
`department`=:department
where id=:id;';
$params=array(
':pic' => $data[0],
':firstname' => $data[1],
':lastname' => $data[2],
':age' => $data[3],
':gender' => $data[4],
':phonenumber' => $data[5],
':location' => $data[6],
':member' => $data[7],
':department' => $data[8],
':id' => $data[9]
);
$stmt=$this->db->prepare( $sql );
foreach( $params as $param => $value )$stmt->bindParam( $param, $value );
return $stmt->execute();
/*
or, easier and quicker would be to ignore binding params and simply do:-
return $stmt->execute( $params );
*/
} catch( PDOException $e ) {
echo $e->getMessage();
}
}