1

The code should work the following way: Press a button -> row gets deleted from database.

I tried to follow and copy answers from other questions but with no working solution.

The jquery code:

$(document).on('click', ".menuRemove", function(event) {
  var del_h3name2 = $(this).parent().parent().prev().text();

  $.ajax({
    type:'POST',
    url:'deleteaccordion2.php',
    data:{'del_h3name2':del_h3name2},
    success: function(data){
      if (data=="YES") {
        alert("YES")
      } else {
        alert("can't delete the row")
      }
    }
  });
}

and php code (deleteaccordion2.php):

<?php
  require 'database.php';

  if ( isset($_SESSION['user_id']) ) {

    $id = $_SESSION['user_id'];
    $accordion = $_POST['del_h3name2'];

    echo '$accordion';

    $delete = "DELETE FROM useraccordion WHERE id='$id', h3= '$accordion' ";
    $result = mysqli_query($delete);

    if ($result) {
      echo "YES";
    } else {
      echo "NO";
    }
  }
?>
WEBjuju
  • 5,797
  • 4
  • 27
  • 36
  • Adding to what @Dekel said, do `alert(del_h3name2);` to see if you're getting correct `h3` column value to delete or not. – Rajdeep Paul Dec 03 '16 at 16:11
  • Are you calling `session_start()` in database.php or somewhere in the page but just didn't include it here in your queation? –  Dec 03 '16 at 16:29
  • Terminus, it was missing but it didn't fix the issue. Thanks still. – Still. Learning Dec 03 '16 at 16:36
  • Are you actually getting `"YES"` or `"NO"` back from deleteaccordian2.php? –  Dec 03 '16 at 16:53
  • I'm getting NO. – Still. Learning Dec 03 '16 at 17:01
  • You're also missing a parameter for [`mysqli_query()`](http://php.net/mysqli_query). You can turn on error_reporting and display_errors while developing your site. Too much to explain in a comment but many resources available in the interwebs. [See this answer](http://stackoverflow.com/a/5438125) Good luck and happy programming! –  Dec 03 '16 at 17:02
  • Thanks for help. I'm even more newb when it comes to php, which shows! – Still. Learning Dec 03 '16 at 17:07

1 Answers1

2

You didn't add the html so I really don't know if the value you are sending is correct, but you do have error in your SQL syntax:

 DELETE FROM useraccordion WHERE id='$id', h3= '$accordion'
                                         ^ This is wrong.

You can DELETE where id = x AND h3 = y:

$delete = "DELETE FROM useraccordion WHERE id='$id' AND h3= '$accordion' ";

Note that your code is vulnerable for SQL injections (read about boby tables).

Dekel
  • 60,707
  • 10
  • 101
  • 129
  • The reason I didn't link any html is because the button and most of the html stuff is created dynamically in the jquery file. The button already deletes the correct elements from the website, but I'm having issues with the database. I also added AND but this didn't fix the issue, thanks for it anyway. Also SQL injections are something I will have to look into in future, this is just a hobby project so no rush. – Still. Learning Dec 03 '16 at 16:16
  • Are you sure `del_h3name2 = $(this).parent().parent().prev().text();` gives you the correct value? Try `alert(del_h3name2)` (or check the network tab to see what value you actually send to the server). Without the HTML structure It's impossible to know what that value would be. – Dekel Dec 03 '16 at 16:21
  • $.ajax({ type:'POST', url:'deleteaccordion2.php', data:{'del_h3name2':del_h3name2}, success: function(data){ if(data=="YES"){ alert(del_h3name2); }else{ alert(del_h3name2); } } }); gives the correct del_h3name2 but still not working – Still. Learning Dec 03 '16 at 16:32
  • Checking the network tab says following: Religion:"" --> is this the data sent to deleteaccordion2? I need it strictly to be Religion, without the : and "" – Still. Learning Dec 03 '16 at 16:46
  • Based on your javascript code there should be `del_h3name2` with the value inside. If it's not there - you don't send it to the server. Did you add the `alert` I suggested? – Dekel Dec 03 '16 at 16:47
  • Yes, now the network tab shows two deleteaccordion2.php messages when I press the delete button once. The first which fires in 83ms has form data of: Religion: and the second, which fires at 91ms has the following form data: del_h3name2:Religion – Still. Learning Dec 03 '16 at 17:04
  • I'm afraid I don't understand if you still have a problem (and what is it...) – Dekel Dec 03 '16 at 17:06