Zariweya
Zariweya

Reputation: 295

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

...
<a href="javascript:void(0);" class="button red big" style="font-size:24px;" onclick="update_it(<?=$_REQUEST["yeah"]["id"];?>);">Pay</a>
...

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
    $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). 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.

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.

What am I doing wrong? Any tip or advice? Thank you in advance.

Upvotes: 1

Views: 1075

Answers (3)

Zariweya
Zariweya

Reputation: 295

Ok, finally I found a solution.

The code is pretty fine, it is an apache ownerships and rights problem.

This helped a lot.

Thank you guys for all you comments.

Upvotes: 0

Paul Burilichev
Paul Burilichev

Reputation: 444

I am using apache MySQL and php on Winodws. And I has the same issue. I have in my update-script which was called by AJAX

  1. an UPDATE SQL statement
  2. query it from 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. But it will work if you before any saving (updating) data make some changes to it.

I am using REPLACE statement instead of 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.

Upvotes: 1

ahmad rabbani
ahmad rabbani

Reputation: 319

you should add exit; statement at the end of php code. echo some success statement. Then You should alert the output variable in javascript. So that you can make sure that php code is called.

Upvotes: 0

Related Questions