PHP PDO绑定参数冗余

PHP PDO绑定参数冗余,php,mysql,pdo,Php,Mysql,Pdo,我有一个动态查询,如果用户不上传文件,它只会更新其他字段,如果用户上传文件,那么要更新的查询和字段是相同的,但这次它也会更新文件字段。下面的代码确实有效,但我想知道bindParameters是否可以使用一次,然后只添加'pdf_file'字段,而不是将相同的bindParameters放在不同的查询中 $query = 'UPDATE `records` SET `title` = :title, `author`= :author,

我有一个动态查询,如果用户不上传文件,它只会更新其他字段,如果用户上传文件,那么要更新的查询和字段是相同的,但这次它也会更新文件字段。下面的代码确实有效,但我想知道bindParameters是否可以使用一次,然后只添加
'pdf_file'
字段,而不是将相同的bindParameters放在不同的查询中

$query = 'UPDATE `records` SET
            `title` = :title,
            `author`= :author,
            `subject` = :subject,
            `call_no`= :call_no,
            `year` = :year ';

 if(empty($pdf_file)){
        
        $query .='WHERE `record_id` = :id';
        $update=$pdo->prepare($query);
    
        $update->bindParam(':title',$title, PDO::PARAM_STR); 
        $update->bindParam(':author',$author, PDO::PARAM_STR);
        $update->bindParam(':subject',$subject, PDO::PARAM_STR); 
        $update->bindParam(':call_no',$call_no, PDO::PARAM_STR); 
        $update->bindParam(':year',$year, PDO::PARAM_STR); 
        $update->bindParam(':id',$id, PDO::PARAM_STR); 
    
        if($update->execute()){
            echo "nofile_ok";
        }
        $errorMsg = 'nofile_ok';
 }else if($ext == "pdf"){
      // Accept
 }else if($pdf_type == "application/pdf"){
     // Accept
  }else{
     echo "notpdf";
     $errorMsg = "Upload PDF Only.....Check your file extenson";
  }

  if(!isset($errorMsg)){
                    $query .= ',`record_file` = :file_pdf WHERE `record_id` = :id';
                    $update=$pdo->prepare($query);
                    $update->bindParam(':title',$title, PDO::PARAM_STR); 
                    $update->bindParam(':author',$author, PDO::PARAM_STR);
                    $update->bindParam(':subject',$subject, PDO::PARAM_STR); 
                    $update->bindParam(':call_no',$call_no, PDO::PARAM_STR); 
                    $update->bindParam(':year',$year, PDO::PARAM_STR); 
                    $update->bindParam(':id',$id, PDO::PARAM_STR); 
                    $update->bindParam(':file_pdf',$db_pdf, PDO::PARAM_STR);
                    
                    if($update->execute()){         
                        move_uploaded_file($temp,$pdf); //
                        echo "file_ok"; //pass ajax success message
                    }
                }

我不知道为什么这么多开发人员认为您需要到处使用
bindParam()
。对数组使用
execute()

参见文档中的示例#2和#3:

您可以使用参数=>值对定义关联数组,然后如果要添加
:file\u pdf
参数,只需在关联数组中设置一个附加项即可

$query = 'UPDATE `records` SET
            `title` = :title,
            `author`= :author,
            `subject` = :subject,
            `call_no`= :call_no,
            `year` = :year ';

$params = [ 
    'title' => $title,
    'author' => $author,
    'subject' => $subject,
    'call_no' => $call_no,
    'year' => $year,
    'id' => $id
];

if(empty($pdf_file)){
        
    $query .= 'WHERE `record_id` = :id';
    $update=$pdo->prepare($query);
    
    if($update->execute($params)){
        echo "nofile_ok";
    }   
    $errorMsg = 'nofile_ok';
}else if($ext == "pdf"){
    // Accept
}else if($pdf_type == "application/pdf"){
    // Accept
}else{
    echo "notpdf";
    $errorMsg = "Upload PDF Only.....Check your file extenson";
} 

if(!isset($errorMsg)){
    $query .= ',`record_file` = :file_pdf WHERE `record_id` = :id';
    $update=$pdo->prepare($query);

    $params['file_pdf'] = $db_pdf; // set one more parameter
    
    if($update->execute($params)){  
        move_uploaded_file($temp,$pdf); 
        echo "file_ok"; //pass ajax success message 
    }   
}       

顺便说一下,在PDO库的旧版本中,有必要在绑定参数的键上使用
,但现在不再是了。SQL查询中需要
符号来表示命名参数,但在将参数键传递给语句时,参数键中不需要该符号。

不能对不同的SQL代码重复使用同一语句。但是,如果您担心冗余,您可能希望在一个数组中一次性传递所有参数(您选择了最详细的语法,因为没有明确的原因,甚至整数都声明为字符串)。在数组中传递绑定参数?可能吗?