使用ajax和php时未定义索引的问题
我有四个不同的文件 index.php select.php insert.php edit.php delete.php 在我的后端,我创建了一个名为“ecc”的数据库 数据库“ecc”有一个名为“task”的表 表任务具有以下字段 标识、名称、类别、成本 id的数据类型设置为int,索引作为主id字段是自动递增的 我的问题:(!)注意:第38行C:\wamp\www\select.php中的未定义索引:id 第39、40、41行也是如此 每个错误在页面上显示两次 index.php使用ajax和php时未定义索引的问题,php,html,ajax,mysqli,Php,Html,Ajax,Mysqli,我有四个不同的文件 index.php select.php insert.php edit.php delete.php 在我的后端,我创建了一个名为“ecc”的数据库 数据库“ecc”有一个名为“task”的表 表任务具有以下字段 标识、名称、类别、成本 id的数据类型设置为int,索引作为主id字段是自动递增的 我的问题:(!)注意:第38行C:\wamp\www\select.php中的未定义索引:id 第39、40、41行也是如此 每个错误在页面上显示两次 index.php <
<html>
<head>
<title>Live Table Data Edit</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
</head>
<body>
<div class="container">
<br />
<br />
<br />
<div class="table-responsive">
<h3 align="center">Live Table Add Edit Delete using Ajax Jquery in PHP Mysql</h3><br />
<div id="live_data"></div>
</div>
</div>
</body>
</html>
<script>
$(document).ready(function(){
function fetch_data()
{
$.ajax({
url:"select.php",
method:"POST",
success:function(data){
$('#live_data').html(data);
}
});
}
fetch_data();
$(document).on('click', '#btn_add', function(){
var name = $('#name').text();
var category = $('#category').text();
if(name == '')
{
alert("Enter service Name");
return false;
}
if(category == '')
{
alert("Enter category");
return false;
}
$.ajax({
url:"insert.php",
method:"POST",
data:{name:name, category:category},
dataType:"text",
success:function(data)
{
alert(data);
fetch_data();
}
})
});
function edit_data(id, text, column_name)
{
$.ajax({
url:"edit.php",
method:"POST",
data:{id:id, text:text, column_name:column_name},
dataType:"text",
success:function(data){
alert(data);
}
});
}
$(document).on('blur', '.name', function(){
var id = $(this).data("id1");
var name = $(this).text();
edit_data(id, name, "name");
});
$(document).on('blur', '.category', function(){
var id = $(this).data("id2");
var category = $(this).text();
edit_data(id,category, "category");
});
$(document).on('click', '.btn_delete', function(){
var id=$(this).data("id3");
if(confirm("Are you sure you want to delete this?"))
{
$.ajax({
url:"delete.php",
method:"POST",
data:{id:id},
dataType:"text",
success:function(data){
alert(data);
fetch_data();
}
});
}
});
});
</script>
实时表数据编辑
在PHP Mysql中使用Ajax Jquery添加、编辑和删除活动表
$(文档).ready(函数(){
函数fetch_data()
{
$.ajax({
url:“select.php”,
方法:“张贴”,
成功:函数(数据){
$('live#u data').html(数据);
}
});
}
获取_数据();
$(document).on('click','btn_add',function(){
变量名称=$('#名称').text();
变量类别=$(“#类别”).text();
如果(名称=“”)
{
警报(“输入服务名称”);
返回false;
}
如果(类别=“”)
{
警报(“输入类别”);
返回false;
}
$.ajax({
url:“insert.php”,
方法:“张贴”,
数据:{name:name,category:category},
数据类型:“文本”,
成功:功能(数据)
{
警报(数据);
获取_数据();
}
})
});
函数编辑数据(id、文本、列名称)
{
$.ajax({
url:“edit.php”,
方法:“张贴”,
数据:{id:id,text:text,column\u name:column\u name},
数据类型:“文本”,
成功:函数(数据){
警报(数据);
}
});
}
$(document).on('blur','.name',function(){
var id=$(this.data(“id1”);
var name=$(this.text();
编辑_数据(id、名称、“名称”);
});
$(document).on('blur','.category',function(){
var id=$(this.data(“id2”);
var category=$(this.text();
编辑_数据(id、类别、“类别”);
});
$(文档).on('click','.btn_delete',function(){
var id=$(this.data(“id3”);
如果(确认(“是否确实要删除此?”))
{
$.ajax({
url:“delete.php”,
方法:“张贴”,
数据:{id:id},
数据类型:“文本”,
成功:函数(数据){
警报(数据);
获取_数据();
}
});
}
});
});
select.php
您收到此错误未定义索引:id
因为您没有在select查询中放入id
$sql = "SELECT id,name, category FROM addservices ORDER BY id DESC";
检查您的查询。它需要更改为:
$sql = "SELECT * FROM task ORDER BY id DESC";
出现此错误是因为您没有在$sql语句中选择“id”字段。 你写了-
$sql=“按id描述从addservices订单中选择名称、类别”代码>
应该是
$sql=“从addservices ORDER BY id DESC中选择id、name、category”代码>选择*
可以进行测试,但在生产环境中从来都不是一个好主意。@Lister您是对的,但Saloni使用了所有列的名称
$sql = "SELECT * FROM task ORDER BY id DESC";