我正在尝试直接从表中更新表数据但是当我对表数据模糊时,我得到上述错误。请帮忙......
这是我的代码
function edit_data(id, text, column_name)
{
$.ajax({
url:"edit.php",
type:"POST",
data:{'id':machine_id, 'text':text,'column_name':column_name},
dataType:"json",
success:function(data){
alert(data);
}
});
}
$(document).on('focus','.serial_no',function(){
var id = $(this).data("id1");
var serial_no = $(this).text();
edit_data(id, serial_no,"serial_no");
});
$(document).on('blur','.model',function(){
var id = $(this).data("id2");
var model = $(this).text();
edit_data(id, model,"model");
});
$(document).on('blur','.price',function(){
var id = $(this).data("id3");
var price = $(this).text();
edit_data(id, price,"price");
});
$(document).on('blur','.spare_parts',function(){
var id = $(this).data("id4");
edit_data(id, spare_parts,"spare_parts");
});
$(document).on('blur','.location',function(){
var id = $(this).data("id5");
var location = $(this).text();
edit_data(id, location,"location");
});
我的html是动态的我从php创建我的表
<?php
$conn = mysqli_connect('localhost','root','','swastik_service');
$output = '';
$sql = "SELECT * FROM `machine` ORDER BY `machine_id` DESC";
$result = mysqli_query($conn,$sql);
$output .= '<h4><i class="fa fa-angle-right"></i> Machine Table</h4><hr><table class="table table-striped table-advance table-hover table-bordered">
<thead>
<tr>
<th><i class=\"fa fa-bullhorn\"></i> Machine ID</th>
<th class=\"hidden-phone\"><i class=\"fa fa-question-circle\"></i> Serial No</th>
<th><i class=\"fa fa-bookmark\"></i> Model</th>
<th><i class=\" fa fa-edit\"></i> Price</th>
<th><i class=\" fa fa-edit\"></i> Spare Parts</th>
<th><i class=\" fa fa-edit\"></i> Location</th>
<th></th>
</tr>
</thead>
<tbody>';
$output .= '<tr>
<td class="machine_id" id="machine_id" name="machine_id" contenteditable></td>
<td class="serial_no" id="serial_no" name="serial_no" contenteditable></td>
<td class="model" id="model" name="model" contenteditable></td>
<td class="price" id="price" name="price" contenteditable></td>
<td class="spare_parts" name="spare_parts" id="spare_parts" contenteditable></td>
<td class="location" id="location" name="location" contenteditable></td>
<td><button name="btn_add" id="btn_add" class="btn btn-xs btn-success">+</button></td>
</tr>';
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
$output .= '<tr><td>'.$row["machine_id"].'</td>
<td class="serial_no" data-id1="'.$row["machine_id"].'" contenteditable>'.$row["serial_no"].'</td>
<td class="model" data-id2="'.$row["machine_id"].'" contenteditable>'.$row["model"].'</td>
<td class="price" data-id3="'.$row["machine_id"].'" contenteditable>'.$row["price"].'</td>
<td class="spare_part" data-id4="'.$row["machine_id"].'" contenteditable>'.$row["spare_parts"].'</td>
<td class="location" data-id5="'.$row["machine_id"].'" contenteditable>'.$row["location"].'</td>
<td><button name="btn_delete" id="btn_delete" data-id6="'.$row["machine_id"].'" class="btn btn-xs btn-danger">x</button></td></tr>';
}
}
else{
$output .= "<tr>
<td>Data not found</td>
</tr>";
}
$output .= '</tbody></table>
</div>';`enter code here`
echo $output;
?>
我的edit.php文件
<?php
$conn = mysqli_connect('localhost','root','','swastik_service');
$id = $_POST['id'];
$text = $_POST['text'];
$column_name= $_POST['column_name'];
$sql ="UPDATE `machine` SET '.$column_name.'='.$text.' WHERE `machine_id = '.$id.'";
if(mysqli_query($conn,$sql)){
echo"Data updated";
}
?>
答案 0 :(得分:0)
我认为你的ajax功能代码应该是这样的......
function edit_data(id, text, column_name)
{
var post_data={};
post_data['id']=id;
post_data['column_name']=column_name;
post_data['text']=text;
$.ajax({
url:"edit.php",
type:"POST",
data:post_data,
dataType:"json",
success:function(data){
alert(data);
}
});
}
我不知道你的错误,我认为它来自另一个代码..