Php PDO-动态查询构建

Php PDO-动态查询构建,php,mysql,input,pdo,Php,Mysql,Input,Pdo,是否有可能为PDO构建一个动态查询 说明: 我有以下功能: 作用 saveContent($roundId,$answer,$question_de,$question_en,$files_1_1,$files_1_2,$files_2_1,$files_2_2,$files_3_1,$files_3_2) { $sql_pictures = " UPDATE ".DBPREFIX."pictures SET

是否有可能为PDO构建一个动态查询

说明:
我有以下功能:
作用

saveContent($roundId,$answer,$question_de,$question_en,$files_1_1,$files_1_2,$files_2_1,$files_2_2,$files_3_1,$files_3_2) {

        $sql_pictures = "
            UPDATE ".DBPREFIX."pictures
            SET
                file_1_static=:files_1_1,
                file_1_animation=:files_1_2,
                file_2_static=:files_2_1,
                file_2_animation=:files_2_2,
                file_3_static=:files_3_1,
                file_3_animation=:files_3_2
            WHERE
                roundId=:roundId";
        try {
            $db = self::getConnection();
            $stmt_pictures = $db->prepare($sql_pictures);

            $stmt_pictures->bindParam("roundId", $roundId);
            $stmt_pictures->bindParam("files_1_1", $$question_de);
            $stmt_pictures->bindParam("files_1_2", $question_en);
            $stmt_pictures->bindParam("files_2_1", $roundId);
            $stmt_pictures->bindParam("files_2_2", $$question_de);
            $stmt_pictures->bindParam("files_3_1", $question_en);
            $stmt_pictures->bindParam("files_3_2", $question_en);

            $stmt_pictures->execute();
            $db = null;
            return true;
        } catch(PDOException $e) {
            echo $e->getMessage();
        }

    }  
重要的部分在“sql_图片”中。我通过向函数中传递必要的数据来调用此函数,如下所示:

if (isset($_POST['save'])) {        
            $saveContent = $helper->saveContent(
                $_POST['roundId'],
                $_POST['answer'],
                $_POST['question_de'],
                $_POST['question_en'],
                $_FILES["files_1_1"],
                $_FILES["files_1_2"],
                $_FILES["files_2_1"],
                $_FILES["files_2_2"],
                $_FILES["files_3_1"],
                $_FILES["files_3_2"]
            );  
        }
正如你们中的一些人可能注意到的,它是用于多文件上传的。
现在主要的问题是,当字符串为空(未设置)或值未更改(即$files\u 1\u 1未更改或为空)时,我不想更新字段

我怎样才能做到这一点

其他问题:
使用它可能更好吗

<input type="file" name="files[]" id="file" />  

而不是

<input type="file" name="files_1_2" id="file" />

对于每个输入字段?
那么我如何重构函数呢?我在上面坐了好几个小时,但没有任何线索。一个提示就好了!:)


谢谢

对于插入/更新查询,动态查询构建是一项非常简单的任务。只需创建一个键名等于字段名和相应值的数组,然后在循环中处理它们

当字符串为空(未设置)或值未更改时,我不想更新字段

最后一个不重要。如果值没有更改-表中的值也不会更改。
对于其他值,使用适合您的值(即不为空)创建一个数组,然后使用helper函数动态创建SET语句。例如,您可以使用

其他问题:

就我个人而言,我更喜欢
文件\u 1、文件\u 2、文件\u 3…
的方式,因为它允许我使用简单的foreach迭代
$\u文件
数组