[英]Mysql UPDATE using PHP and AJAX, can't update database
After following several examples from this website I think I'm about to do it but still can't.在遵循本网站的几个示例之后,我想我即将这样做,但仍然不能。
I have this code:我有这个代码:
HTML HTML
...
<a href="javascript:void(0);" class="button red big" style="font-size:24px;" onclick="update_it(<?=$_REQUEST["yeah"]["id"];?>);">Pay</a>
...
AJAX AJAX
function update_it(n_id){
$.ajax({
type: 'POST',
url: 'update_yes.php',
data: {idd: n_id},
success: function(output)
{
alert('Updated, server says '+n_id);
}, error: function()
{
alert('Wrong!');
}
});
}
PHP PHP
<?php
$link = mysqli_connect("localhost", "root", "****", "****");
$sql = "DELETE FROM stuff WHERE id = " .$_POST["idd"];
mysqli_query($link,$sql) or die(mysql_error());
?>
And everything works but the PHP (I think).一切正常,但 PHP(我认为)。 I say this because I can see how the HTML works properly and how the AJAX function return the success message, but still nothing happens in the database.
我这样说是因为我可以看到 HTML 如何正常工作以及 AJAX 函数如何返回成功消息,但在数据库中仍然没有任何反应。
I tried different structures in the data field of the AJAX function like data: 'idd': n_id,
or data: 'idd=' n_id,
but nothing seems to work.我在 AJAX 函数的数据字段中尝试了不同的结构,例如
data: 'idd': n_id,
或data: 'idd=' n_id,
但似乎没有任何效果。
What am I doing wrong?我究竟做错了什么? Any tip or advice?
任何提示或建议? Thank you in advance.
先感谢您。
I am using apache MySQL and php on Winodws.我在 Winodws 上使用 apache MySQL 和 php。 And I has the same issue.
我有同样的问题。 I have in my update-script which was called by AJAX
我的更新脚本中有 AJAX 调用
then... I got nothing to be updated because it is bug in AJAX query which call UPDATE statement in your server-side script.然后...我没有什么要更新的,因为它是 AJAX 查询中的错误,它在您的服务器端脚本中调用 UPDATE 语句。 But it will work if you before any saving (updating) data make some changes to it.
但是,如果您在任何保存(更新)数据之前对其进行一些更改,它就会起作用。
I am using REPLACE
statement instead of UPDATE
.我正在使用
REPLACE
语句而不是UPDATE
。
I looked for this solution a lot of time, really too much.我找了很多时间这个解决方案,真的太多了。 And I can't understand why it happens, because when I debug entire script with own params right on the server and all works fine, but when Ajax then only help REPLACE.
而且我无法理解为什么会发生这种情况,因为当我在服务器上使用自己的参数调试整个脚本并且一切正常时,但是当 Ajax 仅帮助 REPLACE 时。
you should add exit;你应该添加退出; statement at the end of php code.
php 代码末尾的语句。 echo some success statement.
回应一些成功的声明。 Then You should alert the output variable in javascript.
然后你应该在javascript中提醒输出变量。 So that you can make sure that php code is called.
这样就可以确保调用了 php 代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.