【问题标题】:Getting error even though records inserting correctly即使记录正确插入也会出错
【发布时间】:2014-02-27 09:09:56
【问题描述】:

为什么我的 jquery 中出现错误,即使我的记录插入到数据库中?我在这段代码中添加了一些更改,我将其转换为 mysqli 函数。但问题是错误信息出来了?这是为什么呢?

错误消息来自我的脚本下方

这是我的代码

脚本

$(document).ready(function(){

    $("#save").click(function(){
        ajax("save");
    });

    $("#add_new").click(function(){
        $(".entry-form").fadeIn("fast");    
    });

    $("#close").click(function(){
        $(".entry-form").fadeOut("fast");   
    });

    $("#cancel").click(function(){
        $(".entry-form").fadeOut("fast");   
    });

    $(".del").live("click",function(){
        if(confirm("Do you really want to delete this record ?")){
            ajax("delete",$(this).attr("id"));
        }
    });

    function ajax(action,id){
        if(action =="save")
            data = $("#userinfo").serialize()+"&action="+action;
        else if(action == "delete"){
            data = "action="+action+"&item_id="+id;
        }

        $.ajax({
            type: "POST", 
            url: "ajax.php", 
            data : data,
            dataType: "json",
            success: function(response){
                if(response.success == "1"){
                    if(action == "save"){
                        $(".entry-form").fadeOut("fast",function(){
                            $(".table-list").append("<tr><td>"+response.cat_name+"</td><td>"+response.cat_code+"</td><td>"+response.letter+"</td><td><a href='#' id='"+response.row_id+"' class='del'>Delete</a></td></tr>");   
                            $(".table-list tr:last").effect("highlight", {
                                color: '#4BADF5'
                            }, 0000);
                        }); 
                        $(".entry-form input[type='text']").each(function(){
                            $(this).val("");
                        });                     
                    }else if(action == "delete"){
                        var row_id = response.item_id;
                        $("a[id='"+row_id+"']").closest("tr").effect("highlight", {
                            color: '#4BADF5'
                        }, 0000);
                        $("a[id='"+row_id+"']").closest("tr").fadeOut();
                    }
                }else{
                    alert("unexpected error occured, Please check your database connection");
                }
            },
            error: function(res){
                alert("Unexpected error! Try again.");
            }
        });
    }
});

AJAX

<?php
error_reporting(0);
$mysqli = new mysqli("localhost", "root", "", "2015");
if(isset($_POST) && count($_POST)){
    $cat_name = $mysqli->real_escape_string($_POST["cat_name"]);
    $cat_code = $mysqli->real_escape_string($_POST["cat_code"]);
    $letter = $mysqli->real_escape_string($_POST["letter"]);
    //$phone = mysql_real_escape_string($_POST['phone']);
    $item_id = $_POST['item_id'];
    $action = $_POST['action'];

    if($action == "save"){
        $result = $mysqli->query("insert into category values('','".$cat_name."','".$cat_code."','".$letter."')");
        $lid = mysqli_insert_id();
        if($lid){
            echo json_encode(
                array(
                "success" => "1",
                "row_id" => $lid,
                "cat_name" => htmlentities($cat_name),
                "cat_code" => htmlentities($cat_code),
                "letter" => htmlentities($letter),
                //"phone"=>$unique_id,
                )
            );
        }else{
            echo json_encode(array("success" => "0"));
        }
    }
    else if($action == "delete"){
        //echo "delete from info where id = '".$item_id."'";
        $res = $mysqli->query("delete from category where id = '".$item_id."'");
        if($res){
            echo json_encode(array( "success" => "1","item_id" => $item_id));
        }else{
            echo json_encode(array("success" => "0"));
        }
    }
}else{
    echo json_encode(array("success" => "0"));
}
?>

【问题讨论】:

  • 你能给我们更多关于错误的信息吗?
  • @argonius 我的脚本下面的错误。 else{ alert("发生意外错误,请检查您的数据库连接"); } }, error: function(res){ alert("意外错误!再试一次。"); }
  • 在成功函数中,console.log(JSON.stringify(response)),看看数据是什么样的。
  • @Andrew success = 0 为什么会这样?
  • 试试var_dump($lid),如果是0,可能是表类别没有auto_increment列

标签: php jquery ajax mysqli


【解决方案1】:

成功块中返回的响应为零,因为这里的语句$lid = mysqli_insert_id(); 没有返回任何内容。

$lid = mysqli_insert_id(); 替换为 $mysqli-&gt;insert_id ,因为这是获取最近添加/插入行的 id 的正确语法。

快乐编码:)

【讨论】:

    【解决方案2】:

    我明白了。 mysqli_insert_id() 需要数据库的连接。

    应该是这样的

    mysqli_insert_id($mysqli);
    

    【讨论】:

      猜你喜欢
      • 2013-11-09
      • 2023-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多