0

Using php + jquery/ajax + mysql

Everything works fine when I try to insert one record

My steps:

  • I insert a row in a blank mysql table by ajax. (Ok)
  • I check that the new and the only one row is in there (ok)
  • I try to delete that (new and the only one) row by ajax (ok)
  • console.log shows me: mysqli_affected_rows 1 row affected (ok)
  • console.log and mysql reports me that the row was deleted correctly. No errors or warnings (ok)
  • When I take a look to the table, the new and the only one row is still there! (fail)

Just when I add one more row with the same procedure I'm able to delete the 'old and not the only one' row by ajax and then it happens the same with the new last row.

The query

$sql=mysqli_query($connmysql,"DELETE from locations where iddata='97' and location='01-N-D-34' and  items='48'")or die(mysqli_error($connmysql)); 

echo mysqli_affected_rows($connmysql);
$connmysql->close();

This is the ajax(simplified but tested)

$.ajax({
                          type: "POST",
                          url: "delete.php",                             
                          dataType: "text",       
                            success: function(response) {
                            console.log(response);                              
                            
                            },
                        error: function(xhr,textStatus,err) {
                          console.log("readyState: " + xhr.readyState);
                            console.log("responseText: "+ xhr.responseText);
                            console.log("status: " + xhr.status);
                            console.log("text status: " + textStatus);
                            console.log("error: " + err);
                        }
}); 

EDITED

Sorry, it was my fault.

It was something related with jquery and a class name in the wrong place . I was triggering an 'insert' and a 'delete' every time I was clicking the 'delete' button. Basically, I was deleting a record and inserting it again with the same content. Thanks anyway for your support.

Alex Coloma
  • 651
  • 3
  • 8
  • 1
    Maybe you have not committed your changes? *When I take a look to the table, the new and the only one row is still there!* How do you check this? by executing `SELECT COUNT(*) FROM tablename;` in CLI? – Akina Dec 01 '21 at 12:30
  • 3
    Please share more details. As you haven't shown a single line of JS code: is this really related to AJAX? – Nico Haase Dec 01 '21 at 12:30
  • I check it directly in the database table. I updated my question adding the ajax code – Alex Coloma Dec 01 '21 at 12:47
  • 1
    Don’t mix JavaScript/jQuery/AJAX with SQL, you’ll go nuts debugging things. Instead, make a boring old page that submits a boring old form and allows you to see results and dumps. Once that is working, the conversion to AJAX should be straightforward. Also, avoid echo in these situations, it can hide things, try var_dump – Chris Haas Dec 01 '21 at 12:47
  • I'll try that. Thanks – Alex Coloma Dec 01 '21 at 12:51
  • Everything happens for a reason. (1) Please post the JS / HTML code triggering the ajax (2) I believe you will not insert a record and then hardcode the delete query, right ? Please show the actual insert AND delete queries you are using. (I suppose they will be parametized prepared statements ? ) – Ken Lee Dec 01 '21 at 15:20
  • It is a very bad idea to use `die(mysqli_error($conn));` in your code, because it could potentially leak sensitive information. See this post for more explanation: [mysqli or die, does it have to die?](https://stackoverflow.com/a/15320411/1839439) – Dharman Dec 01 '21 at 16:04
  • Thanks everybody. I edited this. It was just a ridiculous mistake with an easy fix. – Alex Coloma Dec 01 '21 at 19:39

0 Answers0