为什么我的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"));
}
?>
答案 0 :(得分:0)
成功块中返回的Reponse为零,因为此处的语句$lid = mysqli_insert_id();
没有返回任何内容。
将$lid = mysqli_insert_id();
替换为$mysqli->insert_id
,因为这是获取最近添加/插入行的ID的正确语法。
快乐编码:)
答案 1 :(得分:-1)
我明白了。 mysqli_insert_id()需要连接数据库。
应该是这样的
mysqli_insert_id($mysqli);