我正在尝试使用条件IF EXISTS UPDATE ELSE INSERT的查询,并使用for循环一次发送多个记录。其发送值的代码是:
if(isset($_POST['edit'])){
$eOffId = $_POST['off'];
$startHr = $_POST['stHr'];
$startMi = $_POST['st-mi'];
$startMe = $_POST['st-me'];
$endHr = $_POST['en-hr'];
$endMi = $_POST['en-mi'];
$endMe = $_POST['en-me'];
$fieldCount = count($_POST['days']);
$days = $_POST['days'];
$users->editOffice($eOffId, $loc, $sp, $instructor_id);
for($i= 0; $i <= (count($days)); $i++) {
$daysOfWeek = implode(', ', $days[$i]);
$starts = $startHr[$i].":".$startMi[$i]." ".$startMe[$i];
$ends = $endHr[$i].":".$endMi[$i]." ".$endMe[$i];
$dbId = $_POST['time'][$i];
$eOffId = $_POST['off'];
$users->editTimings($dbID, $daysOfWeek, $starts, $ends, $eOffId );
}
}
MYSQL查询
public function editTimings($dbId, $daysOfWeek, $starts, $ends, $eOffId ){
$editTimings_query = $this->db->prepare("INSERT INTO `dyc_instructor_office_hours` (`office_hour_id`, `days`, `start_time`, `end_time`, `office_location_id`) VALUES (:dbId, :daysOfWeek, :starts, :ends, :eOffId) ON DUPLICATE KEY UPDATE `days` = :daysOfWeek, `start_time` = :starts, `end_time` = :ends, `office_location_id` = :eOffId");
$editTimings_query->bindValue(':dbId', $dbId, PDO::PARAM_INT);
$editTimings_query->bindValue(':daysOfWeek', $daysOfWeek, PDO::PARAM_STR);
$editTimings_query->bindValue(':starts', $starts, PDO::PARAM_STR);
$editTimings_query->bindValue(':ends', $ends, PDO::PARAM_STR);
$editTimings_query->bindValue(':eOffId', $eOffId, PDO::PARAM_INT);
try{
$editTimings_query->execute();
}catch(PDOException $e){
}
}
问题是,它只插入记录而不是更新,查询很好,因为我通过更新一条记录单独测试它但是当我在代码中使用它时它只是插入记录而不是更新已经存在的记录。
您的建议将受到高度赞赏。提前谢谢。
谢谢你, 亲切的问候。