$sql = "select * from instalmentsdetails_ where studentFeeId='$savestudentfees' AND instalmentName='$instlnamefdtls'";
$query = $this->db->query($sql);
if($query->num_rows() > 0){
echo($id);
$this->db->where('instalmentsDetailsId',$id);
$this->db->update('instalmentsdetails_',$instldata);
}else{
echo($id);
$id='';
echo($id);
$sql= "INSERT INTO instalmentsdetails_` (`instalmentsDetailsId`, `studentFeeId`, `instalmentName`, `amount`, `dueDate`, `status`) VALUES ('$id', '$savestudentfees', '$instlnamefdtls', '$amtfdtls', '2011-10-06', '1')";
$id=$this->db->insert_id();
$query = $this->db->query($sql);
return $query;
}
return $id;
此查询首先检查是否存在任何行,如果存在,它将更新旧记录,否则将创建新记录,但由于某种原因,即使在查询返回num_row() > 0
。它是codeigniter中的模型
答案 0 :(得分:1)
您是否检查过$query->num_rows()
?的输出,例如echo $query->num_rows()
你可以这样做,并在PHP中保存麻烦
ALTER TABLE teami_db ADD UNIQUE INDEX(studentFeeId, instalmentName);
然后你可以像这样执行ON DUPLICATE KEY UPDATE查询。
INSERT INTO `instalmentsdetails_teami` (
`instalmentsDetailsId`,
`studentFeeId`,
`instalmentName`,
`amount`,
`dueDate`,
`status`
) VALUES (
'$id',
'$savestudentfees',
'$instlnamefdtls',
'$amtfdtls',
'2011-10-06',
'1'
) ON DUPLICATE KEY UPDATE
`instalmentsDetailsId` = VALUES(`instalmentsDetailsId`),
`studentFeeId` = VALUES(`studentFeeId`),
`instalmentName` = VALUES(`instalmentName`),
`amount` = VALUES(`amount`),
`dueDate` = VALUES(`dueDate`),
`status` = VALUES(`status`)
http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html