我正在为我的用户创建一个多步骤表单。他们将被允许更新任何或所有字段。所以,我需要发送值,检查它们是否已设置,如果是,则运行UPDATE
. 这是我到目前为止所拥有的:
public function updateUser($firstName, $lastName, $streetAddress, $city, $state, $zip, $emailAddress, $industry, $password, $public = 1,
$phone1, $phone2, $website,){
$updates = array(
'firstName' => $firstName,
'lastName' => $lastName,
'streetAddress' => $streetAddress,
'city' => $city,
'state' => $state,
'zip' => $zip,
'emailAddress' => $emailAddress,
'industry' => $industry,
'password' => $password,
'public' => $public,
'phone1' => $phone1,
'phone2' => $phone2,
'website' => $website,
);
这是我的 PDO(嗯,开始尝试)
$sth = $this->dbh->prepare("UPDATE user SET firstName = "); //<---Stuck here
$sth->execute();
$result = $sth->fetchAll(PDO::FETCH_ASSOC);
return $result;
基本上,我怎样才能创建UPDATE
语句,以便它只更新数组中不是的项目NULL
?
我想过foreach
像这样运行一个循环:
foreach($updates as $key => $value) {
if($value == NULL) {
unset($updates[$key]);
}
}
prepare
但是如果我不确定这些值,我将如何编写声明?
如果我完全错了,请指出我正确的方向。谢谢。