I have this dynamic query that if the user doesn't upload a file it will only update the other fields, then if the user uploads a file, the query and fields to be updated are the same but this time it also updates the file - field. The code below actually works but I'm wondering if the bindParameters can be used once then just add the 'pdf_file' field instead of putting the same bindParamters on different queries.
$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
}
}