0

I'm running around the issue which I don't really know how to solve - I simply want to remove uploaded file from the database, but I'm failing to do so. My personal guess now is that my delete_shop_item.php doesn't work or that upload.php loop messes up the deletion process from the database (but that's only my guess).

Ajax:

$(document).on('click', '#rmvBtn', function() { /*press the button to remove selected item*/
    del_title = $("#"+ $("#selectOpt").val()); /* select dynamically generated option to remove*/
       $.ajax({
            type: 'POST',
            url: 'delete_shop_item.php',
            cache: false,
            processData: false,
            data: {title:del_title.val()},
            success: function() {
                $("#" + $("#selectOpt").val()).remove();
                $("#selectOpt option:selected").remove();
            }
        });
   

delete_shop_item.php

 $title = $_POST['title'];
    $pdo = new PDO('mysql:host=localhost;dbname=project', 'root', '');
    $query = 'DELETE FROM photos WHERE title = :title';
    $stmt = $pdo->prepare($query);
    $stmt->bindPARAM(':title', $title);
    $stmt->execute();

upload.php

<?php   $count = 1; 
        while($data = mysqli_fetch_array($result)) {
            if($count === 1) {
                echo "<div class='img_container'>";
            }
                echo "<div  class='img_div' id='".$data['title']."'>"; 
                    echo "<img src='uploads/" . $data['filename']."'>";
                    echo "<p  class='img_title' >" .$data['title']. "</p>";
                    echo "<p class='img_desc'>" .$data['photo_description']. "</p>";
                    echo "<p>" .$data['price']. "</p>";
                echo "</div>";
            if($count % 5 === 0) {
                echo "</div>";
                $count = 1;
                continue;
            }
            $count++;
        }
?>
DarkBee
  • 16,592
  • 6
  • 46
  • 58
Auri-stack
  • 33
  • 6
  • use " del_title = $("#"+ $("#selectOpt").val()).val(); " and pass del_title , because direct getting the value at the time of assigning the data values sometimes causes errors and make sure del_title getting the correct value – Bhanu Pratap Aug 13 '21 at 11:50
  • yes it's ```delete_shop_item``` (my mistake) – Auri-stack Aug 13 '21 at 11:55
  • No problem, updated your question to avoid further misunderstandings about this – DarkBee Aug 13 '21 at 11:57
  • Tried the ```del_title = $("#"+ $("#selectOpt").val()).val();``` but the issue is still the same: item is not removed from db – Auri-stack Aug 13 '21 at 12:00
  • To debug this, skip AJAX and jQuery completely, and make the route work using GET. Then manually visit the URL in your browser with a querystring for `title` parameter. If the database entry is deleted, the problem is in AJAX/jQuery. If it isn't deleted, the problem is in PHP/MySQL. Once you get it working simply, add the complexities back in. – Chris Haas Aug 13 '21 at 13:26
  • I understand what you mean by this method: instead of POST, use GET, type the required querystring in the url? Or do I need to change anything else (never tried anything like this so I'm not sure). If that's what you meant: item is not deleted. – Auri-stack Aug 13 '21 at 13:45
  • First, deleting something by title is probably not a good idea, what if two things have the same title? Usually an ID is used instead. But I'll leave that to you. Second, we're debugging so we're doing things non-standard, but the goal is to get things to work first. So yes, just have a page with a querystring that accepts your title such as `blah.php?title=Your Title Here`. Then in PHP, `var_dump($_GET['title']);` to see what is in there. Finally, find a MySQL tool and manually run `DELETE FROM photos WHERE title = "Your Title Here"` to see what it says. – Chris Haas Aug 13 '21 at 13:56
  • Also, turn on [PHP](https://stackoverflow.com/a/21429652/231316) and [MySQL](https://stackoverflow.com/a/22662582/231316) error reporting and watch for anything. AJAX calls make it much harder to debug these kinds of errors which is why we want the native browser. – Chris Haas Aug 13 '21 at 13:57
  • Okay I just used GET and queryString and it worked to manually delete the item WHEN I included `delete_item_shop.php` after the delete button (in the same form), but it doesn't work for the POST `Undefined array key "title`. As for the titles: yes, I'll make a change to my upload form (so they dont repeat) – Auri-stack Aug 13 '21 at 14:24

1 Answers1

-1

try data: {title:del_title} in ajax request