简体   繁体   English

使用PDO更新数组

[英]UPDATE an array using PDO

I'm creating a multi-step form for my users. 我正在为我的用户创建一个多步骤表单。 They will be allowed to update any or all the fields. 他们将被允许更新任何或所有字段。 So, I need to send the values, check if they are set and if so, run an UPDATE . 所以,我需要发送值,检查它们是否已设置,如果是,则运行UPDATE Here is what I have so far: 这是我到目前为止:

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,

);

Here is my PDO (well, the beginning attempt) 这是我的PDO(嗯,开始尝试)

    $sth = $this->dbh->prepare("UPDATE user SET firstName = "); //<---Stuck here
    $sth->execute();
    $result = $sth->fetchAll(PDO::FETCH_ASSOC);
    return $result; 

Basically, how can I create the UPDATE statement so it only updates the items in the array that are not NULL ? 基本上,我如何创建UPDATE语句,以便它只更新数组NULL

I thought about running a foreach loop like this: 我想过运行这样的foreach循环:

    foreach($updates as $key => $value) {
        if($value == NULL) {
            unset($updates[$key]);
        }
    }

but how would I write the prepare statement if I'm unsure of the values? 但如果我不确定这些值,我该如何编写prepare声明呢?

If I'm going about this completely wrong, please point me in the right direction. 如果我完全错了,请指出我正确的方向。 Thanks. 谢谢。

First of all, use array_filter to remove all NULL values: 首先,使用array_filter删除所有NULL值:

$updates = array_filter($updates, function ($value) {
    return null !== $value;
});

Secondly, bind parameters, that makes your live a lot easier: 其次,绑定参数,使您的生活更轻松:

$query = 'UPDATE table SET';
$values = array();

foreach ($updates as $name => $value) {
    $query .= ' '.$name.' = :'.$name.','; // the :$name part is the placeholder, e.g. :zip
    $values[':'.$name] = $value; // save the placeholder
}

$query = substr($query, 0, -1).';'; // remove last , and add a ;

$sth = $this->dbh->prepare($query);

$sth->execute($values); // bind placeholder array to the query and execute everything

// ... do something nice :)

The below can be optimized: 以下可以优化:

$i = 0; $query = array();
foreach($updates as $key => $value) {
   if ($value != NULL) {
      $query[] = "{$key} = :param_{$i}";
      $i++;
   }
}

if (! empty($query)) {
  $finalQuery = implode(",", $query);
  $sth = $this->dbh->prepare('UPDATE user SET ' . $finalQuery);

  $i = 0; 
  foreach($updates as $key => $value) {
   if ($value != NULL) {
      $sth->bindParam(':param_'.$i, $value, PDO::PARAM_STR);
      $i++;
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM