简体   繁体   English

Mysql UPDATE使用PHP和AJAX,无法更新数据库

[英]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 调用

  1. an UPDATE SQL statement一个 UPDATE SQL 语句
  2. query it from DB MySQL从 DB MySQL 查询
  3. try to update the record without changes, (I mean simple updating a row with the same data)尝试在不更改的情况下更新记录,(我的意思是简单地更新具有相同数据的行)

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 代码。

Ok, finally I found a solution.好的,我终于找到了解决方案。

The code is pretty fine, it is an apache ownerships and rights problem.代码非常好,这是一个 apache 所有权和权限问题。

This helped a lot. 有很大帮助。

Thank you guys for all you comments.谢谢大家的评论。

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

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