我正在使用 JQuery/Ajax 和 php/MySQL
执行 CRUD
操作
我能够插入/选择
和删除
数据,但我必须停留在编辑/更新中。我在单击编辑按钮时将数据拉入文本框,但在编辑后单击保存按钮时无法在 mysql 数据库中更新!!
感谢任何帮助
html代码
<span class="noedit name" idl='<?php echo $row->id;?>'>
<?php echo $row->url;?>
</span>
<input id="url1" name="url1" class="form-control edit name url1" value="<?php echo $row->id;?>"/>
<a ide='<?php echo $row->id;?>' id="edit" class='editOrder' href="#" style="display:block-inline;">EDIT</a>
<a idu='<?php echo $row->id;?>' id="update" class='update saveEdit' href='#' style='display:none;'>SAVE</a>
<a idc='<?php echo $row->id;?>' id="cancel" class='cancelEdit edit' href='#' style='display:none;'>CANCEL</a>
Jquery代码
$('body').delegate('.edit','click',function(){
var IdEdit = $(this).attr('ide');
alert(IdEdit);
$.ajax({
url:"pages/feeds.php",
type:"post",
data:{
editvalue:1,
id:IdEdit
},
success:function(show)
{
$('#id').val(show.id);
$('#url1').val(show.url);
}
});
});
$('.update').click(function(){
var id = $('#id').val()-0;
var urls = $('#url1').val();
$.ajax({
url:"pages/feeds.php",
type:"post",
async:false,
data:{
update:1,
id:id,
upurls:urls
},
success:function(up)
{
$('input[type=text]').val('');
showdata();
},
error:function(){
alert('error in updating');
}
});
});
PHP 代码
if(isset($_POST['editvalue']))
{
$sql = "select * from test where id='{$_POST['id']}'";
$row = mysql_query($sql);
$rows = mysql_fetch_object($row);
header("Content-type:text/x-json");
echo json_encode($rows);
exit();
}
if(isset($_POST['update']))
{
$sql = "
update test
set
url='{$_POST['upurls']}'
where id='{$_POST['id']}'
";
$result = mysql_query($sql);
if($result)
{
//alert('success');
echo 'updated successfully';
}
else
{
//alert('failed');
echo 'failed to update';
}
}
最佳答案
我在您的代码中没有看到#id 输入。是吗?
我觉得问题就出在这里。
如果此输入存在,请使用以下提示:
检查是否所有值(id、url)都发送到您的 PHP 脚本。
您可以在 Javascript 中使用 console.log
或在 PHP 中使用 print_r
、var_dump
函数。
关于javascript - 使用 jquery/php 在 mysql 中进行内联更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32521387/