我正在使用ajax实现一个简单的插入和编辑,但是在编辑时它没有显示警报,而在插入时它告诉未定义的索引,尽管我的字段名称与我的数据库中一样
edit.php
<?php
$connect=mysqli_connect("localhost","root","") or die('Could not connect: '.mysqli_error()); mysqli_select_db($connect,'elearning') or die ('database not found: '.mysqli_error());
$id = $_POST["pageID"];
$text = $_POST["text"];
$column_name = $_POST["column_name"];
$sql = "UPDATE page SET ".$column_name."='".$text."' WHERE pageID='".$id."'";
if(mysqli_query($connect, $sql)) { echo 'Data Updated'; }
?>
page.php文件
<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 cid = $('#cid').text();
var name = $('#name').text();
var icon = $('#icon').text();
var src = $('#src').text();
var hint = $('#hint').text();
var category = $('#category').text();
$.ajax({
url: "insert.php",
method: "POST",
data: {
cid: cid,
name: name,
icon: icon,
src: src,
hint: hint,
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: {
pageID: id,
text: text,
column_name: column_name
},
dataType: "text",
success: function(data) {
alert(data);
}
});
}
$(document).on('click', '.btn_delete', function() {
var id = $(this).data("id7");
if (confirm("Are you sure you want to delete this?")) {
$.ajax({
url: "delete.php",
method: "POST",
data: {
pageID: id
},
dataType: "text",
success: function(data) {
alert(data);
fetch_data();
}
});
}
});
});
</script>
&#13;