AJAX POST 无法更新 MySQL 数据库的问题
我有一个通过 AJAX 调用的表单(用于在 CMS 上使用),然后该表单用于更新内容数据库,但是它不起作用,我似乎无法弄清楚在哪里。
一切都在提交之前完成,因为字段中都已正确填写了数据库行、列等,并且数据已预先填充了当前的内容。问题出在 AJAX commit() 函数和 eupdate.php MySQL 查询之间。
eform.php(通过另一个页面 eindex.php 拉出并显示)
<?php
require("../mcfrdb.php");
// Included database once using the require method
$item = $_POST['item'];
$page = $_POST['page'];
$row = mysql_query("SELECT * FROM mcfr WHERE pageid = '$page'");
$data = mysql_fetch_array($row);
?>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"> </script>
<script type="text/javascript">var $j = jQuery.noConflict();</script>
<script type="text/javascript">
function submit(){
$j.ajax({
type:"POST",
url:"eupdate.php",
data: "item=" + $j('#item') + "&itemcont=" + $j('#itemcont') + "&page=" + $j('#page'),
success:function(response){
$j("#msg").html(response);
}
});
}
</script>
<div id="msg"></div>
<form id = "edititem" name = "edititem" onsubmit="return false;" method="post" >
<textarea cols="20" rows="5" name="itemcont" id="itemcont"><? echo $data[$item]; ?></textarea> <br/>
<input type="text" name="item" id="item" value="<? echo $item; ?>"><br/>
<input type="text" name="page" id="page" value="<? echo $page; ?>"><br/>
<input type="button" value="make changes" onclick="submit()" >
</form>
eupdate.php
<?php
require("../mcfrdb.php");
// Included database once using the require method
$item=$_POST['item'];
$page=$_POST['page'];
$newcont=$_POST['itemcont'];
$row = mysql_query("UPDATE mcfr SET '$item' = '$newcont' WHERE pageid = '$page'");
?>
当我单击按钮提交时,在检查我的数据库后,没有任何更改或更新。
提前感谢所有回复,希望我们能解决这个问题:) 干杯
I have a form which I call via AJAX (for use on a CMS) and then this form is used to update the database for the content, however its not working and I cant seem to figure out where.
It all works up to the submit, as in the fields are all filled out correctly with the database rows, columns etc and the data is pre-filled with what is there currently. The issue lies somewhere between the AJAX submit() function and the eupdate.php MySQL query.
eform.php (pulled via another page, eindex.php, to be displayed)
<?php
require("../mcfrdb.php");
// Included database once using the require method
$item = $_POST['item'];
$page = $_POST['page'];
$row = mysql_query("SELECT * FROM mcfr WHERE pageid = '$page'");
$data = mysql_fetch_array($row);
?>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"> </script>
<script type="text/javascript">var $j = jQuery.noConflict();</script>
<script type="text/javascript">
function submit(){
$j.ajax({
type:"POST",
url:"eupdate.php",
data: "item=" + $j('#item') + "&itemcont=" + $j('#itemcont') + "&page=" + $j('#page'),
success:function(response){
$j("#msg").html(response);
}
});
}
</script>
<div id="msg"></div>
<form id = "edititem" name = "edititem" onsubmit="return false;" method="post" >
<textarea cols="20" rows="5" name="itemcont" id="itemcont"><? echo $data[$item]; ?></textarea> <br/>
<input type="text" name="item" id="item" value="<? echo $item; ?>"><br/>
<input type="text" name="page" id="page" value="<? echo $page; ?>"><br/>
<input type="button" value="make changes" onclick="submit()" >
</form>
eupdate.php
<?php
require("../mcfrdb.php");
// Included database once using the require method
$item=$_POST['item'];
$page=$_POST['page'];
$newcont=$_POST['itemcont'];
$row = mysql_query("UPDATE mcfr SET '$item' = '$newcont' WHERE pageid = '$page'");
?>
When i click my button to submit, upon checking my DB after this, nothing has changed or updated.
Thanks in advance for all replies, here's hoping we can get this fixed :)
Cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您使用 AJAX 提交数据,因此不需要使用
form
元素(特别是它要求您执行其他操作以防止数据正常提交)。无论如何,基本问题可能是您如何(不)检索字段的值。
$j('#item')
返回绑定到item
输入的 jQuery 对象,而不是其值。要获取该值,请使用.val()
方法,例如:$j('#item').val()
。附带说明一下,在任何 SQL 查询中使用 PHP 脚本中发布的数据之前,您还需要对其进行转义,否则您很容易受到一些可怕的 SQL 注入攻击。请参阅 mysql_real_escape_string 的文档以获取解释:http://php.net/manual/en/function.mysql-real-escape-string.php
Since you're submitting your data with AJAX, you don't need to use a
form
element (especially that it requires you to do additional things to prevent it from being submitted normally).Anyway, the basic problem is probably how you (don't) retrieve the values of the fields.
$j('#item')
returns the jQuery object bound to theitem
input, not its value. To get the value, use the.val()
method, e.g.:$j('#item').val()
.As a side note, you also need to escape the posted data in your PHP script before using it in any SQL queries, otherwise you're vulnerable to some terrible SQL injection attacks. See the documentation of
mysql_real_escape_string
for an explanation: http://php.net/manual/en/function.mysql-real-escape-string.php