【问题标题】:Not able to save increment values in mysql无法在mysql中保存增量值
【发布时间】:2017-12-05 05:20:54
【问题描述】:

在保存数据时,我也在尝试保存价值id。我可以使用自动增量选项来做到这一点,但出于某种原因,我不想在数据库中使用它。

甚至,我可以保存第一条记录,但之后它会重复以前的 id。请帮我解决这个问题。非常感谢

控制器

public function ajax_add()
{
    $this->_validate();
    $data = array(
        'VillageName'=> $this->input->post('VillageName'),
        'VillageCode'=> $this->input->post('VillageCode'),                              
        'VillageId_save'=> $this->input->post('VillageId_save'),                                
        'VillageType'=> $this->input->post('VillageType'),
        'BlockId'=> $this->input->post('BlockId'),
        'CreatedBy' => $this->input->post('CreatedBy'),
    );
    $insert = $this->village->save($data);
    echo json_encode(array("status" => TRUE));
}

public function get_lastvillageid()
{
    $data = $this->village->getLastInserted();
     echo json_encode($data[0]['VillageId']);
}

查看

function save_vid()
{
  var id = $('#VillageId_save').val();
     $.ajax({
            type:'POST',
            dataType:'json',
            url:'Village/get_lastvillageid',
            data:{'id':id},
            success:function(response){    
                console.log("Data is : "+response);
            console.log("Data is : "+ JSON.stringify(response));
               data = JSON.parse(response);
                $("#VillageId_save").val(data);
            }
    });
}

<input type="hidden" value="<?php
    foreach ($getLastInserted as $row) {              
        echo $row['VillageId']+1;
    }?>" name="VillageId_save" id ="VillageId_save"/>

【问题讨论】:

    标签: php mysql json ajax codeigniter


    【解决方案1】:

    使用 PHP,

    使用mysql获取最后插入的id并增加“1”然后插入它。如果没有记录集默认为“1”。

    例如

    /* Set DB Connection. */
    $select_last_id = "select max(id) as auto_inc from table name";
    $res = mysqli_query($mysqli,$select_last_id);
    if(mysqli_num_rows($res)>0){
        $row = mysqli_fetch_array($res);
        $new_increment_id = intval($row['auto_inc'])+1;
    }
    else{
        $new_increment_id = 1;
    }
    

    【讨论】:

    • 我想通过 ajax 做到这一点。否则它将创建一个重复的 id。
    猜你喜欢
    • 2011-01-14
    • 2017-07-31
    • 2011-05-25
    • 1970-01-01
    • 2011-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多