Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP PDO-多个x字段的单个查询_Php_Mysql_Pdo_Mysqli - Fatal编程技术网

PHP PDO-多个x字段的单个查询

PHP PDO-多个x字段的单个查询,php,mysql,pdo,mysqli,Php,Mysql,Pdo,Mysqli,我有三个字段,我只需要更新已填充的字段。可能的解决办法如下: <?php if(trim($_POST['field_1'])!='') // query for update field 1 if(trim($_POST['field_2'])!='') // query for update field 2 if(trim($_POST['field_3'])!='') // query for update field 3 ?> 但是这并不是最好的优化

我有三个字段,我只需要更新已填充的字段。可能的解决办法如下:

<?php
if(trim($_POST['field_1'])!='')
   // query for update field 1
if(trim($_POST['field_2'])!='')
   // query for update field 2 
if(trim($_POST['field_3'])!='')
   // query for update field 3
?>


但是这并不是最好的优化,你能给我一个例子说明如何使用mysqli(带bind)或PDO进行单个查询吗?

你可以动态地构建查询

$fields = array();

foreach($_POST as $key => $value) {
    // Only grab whitelisted fields
    if (in_array($key, array('field_1', 'field_2', 'field_3'))) {
        if (!empty(trim($value))) {
            // Using the keys from $_POST assumes they are named after their database counterparts
            $fields[$key] = trim($value);
         }
    }
}

// Grab the keys (fieldnames) so we can use them to build the query
$keys = array_keys($fields);
$sqlFieldsPart = implode(', ', array_map(function($field) {
    return $field . '= :' . $field;
}, $keys));
$sql = sprintf('UPDATE tablename SET %s WHERE somefield=:somefield', $sqlFieldsPart);

$dbh = new PDO('mysql:hostname=localhost;dbname=yourdb', 'username', 'password');
$stmt = $dbh->prepare($sql);

// Modify keys on $fields
$data = array();
foreach ($fields as $key => $value) {
     // Note the colon before the key variable, this is necessary
     // It links to the placeholders in the query
     $data[':' . $key] = $value;
}

// Set the value for the where clause
$data[':somefield'] = 'somevalue';

// Execute the statement, passing the data to the execute function
$stmt->execute($data);

此代码假定您的html字段是以其数据库对应项命名的。如果不是这样,您可以从每个字段的第一个foreach循环中进行硬编码,或者进行某种字段映射。

我不确定
$\u POST
是否只包含相关字段或更多字段,因此我假设您将找到一种方法来隔离
$tmp
数组中的字段并使用它

我还假设您已经使用PDO连接到DB,并将其存储在
$DB

最后,行筛选器(
where
子句)已在
$rowfilter
中构建为字符串

// trim all values
array_map('trim',$tmp);
// eliminate empty string values
$tmp=array_filter($tmp,function($el){return $el!='';});
// build the query string
$fields=array_map(function($el){$el="`$el`=?";},array_keys($tmp));
$fldstr=implode(',',$fields);
$sql="UPDATE `mytable` SET $fldstr WHERE $rowfilter";
// prepare and execute
$stmt = $db->prepare($sql);
$stmt->execute(array_values($tmp));

你的问题呢?做出了哪些改变?这是一个单一的领域?相同类型的查询?我的查询是一个简单的更新。是相同类型的查询(更新)。那
UPDATE
where
子句呢?还是对表中的所有行进行大规模更新?