我已经在表中显示了记录并具有编辑链接,当我单击它时,该行的值显示在文本框中,但是当我单击按钮时,它会插入新记录而不是更新表中的记录。我应该进行哪些更改来更新记录?
评论.php
<script type="text/javascript">
$(document).ready(function(){
function loadList(){
$.ajax({
url: "load_list.php",
cache: false,
success : function(html){
$(".name_list").html(html);
}
});
}
loadList();
$("#Submit").click(function() {
if($(":text").val().length==0)
{
// $(this).next().html("Field needs filling");
$(":text").after('<span class="errorkeyup">Field cannot be empty</span>');
//return false;
success = false;
}
else
{
var name=$("#name").val();
var message=$("#message").val();
$.ajax({
type:"post",
url:"save_list.php",
data:"name="+name+"&message="+message,
success:function(data){
loadList();
}
});
return false;
}
});
$("a.delete_button").on("click", function(e){
//this deletes the row clicked on with an alert and then reloads the list
e.preventDefault();
//var id = $(this).attr("id");
var obj = $(this);
var id = obj.attr("id");
$.ajax({
type: "POST",
url: "delete.php",
data: "id="+ id,
success: function(response){
if(response == '1') {
obj.parent.remove();
}
}
});
return false;
});
$("a.edit_button").click(function(){
//$("form#update").live("submit",(function() {
// we want to send via ajax and empty the html from the update_form element
var name = $('#name').attr('value');
var message = $('#message').attr('value');
var id = $('#id').attr('value');
$("#wrapper:has(button)").hide();
$("#Submit").click(function(){
$.ajax({
url: "updateajax.php",
type: "POST",
data: "name="+ name +"& message="+ message +"& id="+ id,
error: function(){
alert('Error loading document');
},
success: function(){
alert (" i am in success below load list ");
$(".update_form").empty();
//loadList();
}
});
});
return false;
});
});
</script>
</head>
<body>
<form method="post" name="form" action="">
<div id="wrapper" align="center">
<?php
include('connection.php');
if(isset($_GET["id"]))
{
$cmd=mysql_query("select * from login where id=".$_GET["id"]);
$rs=mysql_fetch_array($cmd);
}
?>
<table width="200" border="1">
<tr>
<td colspan="2">Form:<input type="hidden" name="idupdate" id="idupdate" value="<?php echo $rs[0];?>"/></td>
</tr>
<tr>
<td>Name:</td>
<td><input type="text" name="name" id="name" value="<?php echo $rs[1];?>" /></td>
</tr>
<tr>
<td>Message:</td>
<td><input type="text" name="message" id="message" value="<?php echo $rs[2]; ?>" /></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="button" value="Submit" id="Submit"><input type="button" value="edit" id="edit" style="display:none"></td>
</tr>
</table>
</div>
<div class="name_list" align="center"></div>
<div class="update_form" style="display:none">
</div>
</div>
</form>
</body>
loadlist.php 这是表中用于编辑的链接
echo"<td>
<a id=".$row['id']." href=comment.php?id=".$row['id']."&type=edit class=edit_button>Edit</a></td>";
更新ajax.php
<?php
include("connection.php");
if(isset($_REQUEST['id']))
{
$id=int($_REQUEST['id']);
$name=mysql_escape_String($_REQUEST['name']);
$message=mysql_escape_String($_REQUEST['message']);
$sql = "update login set username='$name',message='$message' where id='$id'";
mysql_query($sql);
}
?>