drwpbrv668670 2019-07-03 14:06
浏览 46
已采纳

无法插入数据库 - PHP

I have an ajax which triggers a function called which runs an update and insert query. When i check from my db, i realise only the update query is running but then, the insert query never runs. The code below also shows my insert query to be correct.

What could be the issue with my code why the update query works and insert query doesn't work?

users.php

$('#click').click(function () {
        var age = 12
        $.ajax({
            url: '<?php echo base_url('users/demand'); ?>',
            data: '&age='+age,
            type: 'POST'
        }).done(function (result) {
           var obj = $.parseJSON(result);
            $.each(obj, function (index, element) {

              $.ajax({
              url: '<?php echo base_url('users/supply'); ?>',
             data: '&number='+element.phone+'&email='+element.email+'&age='+element.age,
              type: 'POST'
              }).done(function (result) {
                     //refresh page for changes    
              });
          });

        });

    });



function supply()
    {
       $value =  $_POST['age'];

       $query="update table SET `status` = 0 WHERE age IN ($value)";
       $this->db->query($query);

       $save_query="INSERT INTO table(`status`)VALUES ($value)";
       $this->db->query($save_query);

    }
  • 写回答

1条回答 默认 最新

  • dongpao1918 2019-07-03 14:42
    关注

    Try to add this between your query

    $this->db->reset_query();
    

    - using this your query object is reset

    Like:

    function supply()
        {
           $value =  $_POST['age'];
    
           $query="update table SET `status` = 0 WHERE age IN ($value)";
           $this->db->query($query);
           $this->db->reset_query();
           $save_query="INSERT INTO table(`status`)VALUES ($value)";
           $this->db->query($save_query);
    
        }
    

    OR

    function supply() {
        $value = $this->input->post('age');
        $this->db->where_in("age", $value);
        $this->db->update("table", array("status" => 0));
    
        $this->db->reset_query();
        $this->db->insert("table", array("status" => $value));
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测