douzi2778 2011-10-05 05:24
浏览 24
已采纳

任何人都可以告诉这个查询有什么问题吗?

    $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;

This query first checks if there are any rows present, if there is, it is going to update the old record, otherwise it is going to create a new record, but for some reason it does not work as expected even when the query returns num_row() > 0.It's a model in codeigniter

  • 写回答

1条回答 默认 最新

  • douzhi4991 2011-10-05 05:35
    关注

    Have you checked the output of $query->num_rows()?, eg echo $query->num_rows()

    You could do this instead and save the bother in PHP

    ALTER TABLE teami_db ADD UNIQUE INDEX(studentFeeId, instalmentName);
    

    Then you can perform an ON DUPLICATE KEY UPDATE query like so.

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号