简体   繁体   English

无法插入数据库-PHP

[英]Unable to insert into database - PHP

I have an ajax which triggers a function called which runs an update and insert query. 我有一个ajax,它会触发一个称为运行更新和插入查询的函数。 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. 下面的代码还显示了我正确的insert查询。

What could be the issue with my code why the update query works and insert query doesn't work? 为什么我的代码可能会出现问题,为什么更新查询有效而插入查询无效?

users.php 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);

    }

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));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM