简体   繁体   English

无法获取AJAX表单来提交MySQL数据

[英]Can't get AJAX form to commit MySQL data

I have these two files: 我有这两个文件:

<?php
include('db.php');


?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Generic Title</title>
<script type="text/javascript" src="http://ajax.googleapis.com/
ajax/libs/jquery/1.5/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
$(".edit_tr").click(function()
{
var ID=$(this).attr('id');
$("#time_"+ID).hide();
$("#character_"+ID).hide();
$("#message_"+ID).hide();
$("#status_"+ID).hide();
$("#time_input_"+ID).show();
$("#character_input_"+ID).show();
$("#message_input_"+ID).show();
$("#status_input_"+ID).show();
}).change(function()
{
var ID=$(this).attr('id');
var time=$("#time_input_"+ID).val();
var character=$("#character_input_"+ID).val();
var message=$("#message_input_"+ID).val();
var status=$("#status_input_"+ID).val();
var dataString = 'id='+ ID +'&time='+ time + '&character='+ character + '&message='+ message+ '&status='+ status;
$("#character_"+ID).html('<img src="load.gif" />');


if(time.length>0 && message.length>0 && character.length>0 && status.length>0)
{
$.ajax({
type: "POST",
url: "table_edit_ajax.php",
data: dataString,
cache: false,
success: function(html)
{

$("#time_"+ID).html(time);
$("#character_"+ID).html(character);
$("#message_"+ID).html(message);
$("#status_"+ID).html(status);
}
});
}
else
{
alert('Enter something.');
}

});

$(".editbox").mouseup(function() 
{
return false
});

$(document).mouseup(function()
{
$(".editbox").hide();
$(".text").show();
});

});
</script>
<style>
body
{
font-family:Arial, Helvetica, sans-serif;
font-size:14px;
}
.editbox
{
display:none
}
td
{
padding:7px;
}
.editbox
{
font-size:14px;
width:135px;
background-color:#ffffcc;

border:solid 1px #000;
padding:4px;
}
.edit_tr:hover
{
background:url(edit.png) right no-repeat #80C8E5;
cursor:pointer;
}


th
{
font-weight:bold;
text-align:left;
padding:4px;
}
.head
{
background-color:#333;
color:#FFFFFF

}

</style>

</head>

<body bgcolor="#dedede">
<div style="margin:0 auto; width:750px; padding:10px; background-color:#fff; height:800px;">
<table width="100%">
<tr class="head">
<th>Time</th><th>Character</th><th>Message</th><th>Status</th>
</tr>
<?php
$sql=mysql_query("select * from script");
$i=1;
while($row=mysql_fetch_array($sql))
{
$id=$row['id'];
$character=$row['character'];
$time=$row['time'];
$message=$row['message'];
$status=$row['status'];

if($i%2)
{
?>
<tr id="<?php echo $id; ?>" class="edit_tr">
<?php } else { ?>
<tr id="<?php echo $id; ?>" bgcolor="#f2f2f2" class="edit_tr">
<?php } ?>
<td width="10%" class="edit_td">
<span id="time_<?php echo $id; ?>" class="text"><?php echo $time; ?></span>
<input type="text" value="<?php echo $time; ?>" class="editbox" id="time_input_<?php echo $id; ?>" />
</td>
<td width="25%" class="edit_td">
<span id="character_<?php echo $id; ?>" class="text"><?php echo $character; ?></span> 
<input type="text" value="<?php echo $character; ?>"  class="editbox" id="character_input_<?php echo $id; ?>"/>
</td>
<td width="55%" class="edit_td">
<span id="message_<?php echo $id; ?>" class="text"><?php echo $message; ?></span> 
<input type="text" value="<?php echo $message; ?>"  class="editbox" id="message_input_<?php echo $id; ?>"/>
</td>
<td width="10%" class="edit_td">
<span id="status_<?php echo $id; ?>" class="text"><?php echo $status; ?></span> 
<input type="text" value="<?php echo $status; ?>"  class="editbox" id="status_input_<?php echo $id; ?>"/>
</td>
</tr>

<?php
$i++;
}
?>

</table>
</div>
</body>
</html>

and

<?php
include("db.php");
if($_POST['id'])
{
$id=mysql_escape_String($_POST['id']);
$time=mysql_escape_String($_POST['time']);
$character=mysql_escape_String($_POST['character']);
$message=mysql_escape_String($_POST['message']);
$status=mysql_escape_String($_POST['status']);
$sql = "update script set time='$time',character='$character',message='$message',status='$status' where id='$id' ";
mysql_query($sql)
    or die("display_db_query:" . mysql_error());
}
?>

They don't seem to be throwing errors, but edits also aren't committing to the database. 它们似乎没有引发错误,但是编辑也没有提交给数据库。 Where am I going wrong? 我要去哪里错了?

db.php contains the connect script with all the right "stuff" (all the right fields are showing, the UPDATE just doesn't seem to want to play right) db.php包含带有所有正确的“东西”的连接脚本(显示了所有正确的字段,UPDATE似乎并不想正确播放)

A working demo to show what it's supposed to do (most of the code is borrowed) is at http://demos.9lessons.info/table_edit/TableEdit.htm 一个可以演示应做的工作示例(大部分代码是借来的)位于http://demos.9lessons.info/table_edit/TableEdit.htm

Thanks in advance for any light you can help shed on this, I'm a stumped noob. 在此先感谢您提供的任何帮助,我是一个陷入困境的菜鸟。

character is a reserved word in MySQL. 字符是MySQL中的保留字 If you want to use it, you'll need to escape it with backticks 如果要使用它,则需要使用反引号将其转义

UPDATE script SET time='$time', `character` = '$character' ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM