无法从html中的sql检索最后插入的id

时间:2019-07-04 12:31:55

标签: php html sql

我正在尝试显示数据库中一些值到我的html,并调用最后插入的ID


    $(document).on('click', '#confirmOverage', function(e){
            e.preventDefault();
            $(this).attr("disabled",true);

            var overageID = $(this).attr('overageValue');

            if(overageID !== ""){
                swal({
                    title: "Are You Sure ?",
                    text: "Do you really want to confirm this overage deposit ?",
                    icon: "info",
                    buttons: true,
                    dangerMode: true,
                    closeOnClickOutside: false,
                })
                .then((willDelete) => {
                    if (willDelete) {
                        $.ajax({
                            url: '<?=$fn;?>',
                            method: 'POST',
                            dataType: 'text',
                            data: {
                                oi: overageID
                            },
                            success: function(successResponse){
                                if(successResponse !== "" && successResponse.includes("SUCCESS:") === true){
                                   successResponse = successResponse.replace("SUCCESS: ",""); 

                                    swal({
                                        title: "SUCCESS !",
                                        text: successResponse,
                                        icon: "success",
                                        closeOnClickOutside: false,
                                    });
                                }
                                else{
                                    if(successResponse.includes("SORRY !") === true){
                                        successResponse = successResponse.replace("SORRY !","");
                                    }
                                    else if(successResponse.includes("SORRY!") === true){
                                        successResponse = successResponse.replace("SORRY!","");
                                    }
                                    else if(successResponse.includes("Sorry!") === true){
                                        successResponse = successResponse.replace("Sorry!","");
                                    }
                                    else if(successResponse.includes("Sorry,") === true){
                                        successResponse = successResponse.replace("Sorry,","");
                                    }
                                    swal({
                                        title: "SORRY !",
                                        text: successResponse,
                                        icon: "error",
                                        closeOnClickOutside: false,
                                    });
                                }
                            }
                        });                  
                    }
                    else{
                        $(this).attr("disabled",false);
                    }
                });
            }
            else{
                swal({
                    title: "SORRY !",
                    text: "The Overage Parameter is required. Please Check and try again. Thank you",
                    icon: "error",
                    closeOnClickOutside: false,
                }); 
                $(this).attr("disabled",false);
            }
        })

但是当我使用它时,结果没有显示,我的代码是否有问题,如何从sql中检索最后插入的id?

2 个答案:

答案 0 :(得分:0)

尝试此查询

$sql = "SELECT * from registers ORDER BY ID DESC limit 1 ";

它只会给您一个结果,该结果将是最后插入的ID。 但是ID必须是自动递增的。

答案 1 :(得分:0)

要检索表中的最后一个ID,您需要:

group: 64