-1

I'm quite new to PHP and I'm trying to make a basic CRUD with PHP. I'm able to add the record and display it in a table with two actions button on the same row. However, I cannot delete the record from the database and make the table updated (with the data removed). When I hover my mouse on the delete button, it seems like the variable is being parsed but when I clicked on the delete button it says URL not found. I've included some of my code below. Thanks in advance.

The table that shows all the records from the data base:

    <div class="form-group">
      <table class='table'>
          <thead>
            <tr>
              <th>First name</th>
              <th>Last name</th>
              <th>Gender</th>
              <th>Location</th>
              <th colspan="2">Action</th>
            </tr>
          </thead>
          <?php
            $result = $conn->query('SELECT * FROM tb_user ORDER BY id DESC') or die($conn->error);
            while($row = $result->fetch_object()):?>
            <tr>
              <td><?php echo $row->first_name; ?> </td>
              <td><?php echo $row->last_name; ?></td>
              <td><?php echo $row->gender; ?></td>
              <td><?php echo $row->place; ?></td>
              <td colspan="2">
                <a href="index.php?edit<?php echo $row->id; ?>" class="btn btn-info">Edit</a>
                <a href="process.php?delete=<?php echo $row->id; ?>" class="btn btn-danger">Delete</a>
              </td>
            </tr>
                <?php endwhile; ?>
        </table>
      </div>

Here's the "Delete" code:

   if(isset($_GET['delete'])){
      $uId = $_GET['delete'];
      $sql = "DELETE FROM tb_user WHERE id = $uId";
      $conn->query($sql);

      $_SESSION['message'] = "Record has been deleted!";
      $_SESSION['msg_type'] = "danger";
      header("location: index.php");
      
   }

I'm able to get to a blank page with a correct URL instead of getting an "URL not found" error.

The problem was my process.php file wasn't in the same directory as my index.php file.

borak
  • 39
  • 8
  • Is that correct that edit button points to `index.php` and delete button to `process.php`? – biesior Jul 14 '20 at 19:01
  • yes, that is how I did it and it probably not how you go at it. Is this the right way to go at this problem? I want to delete a record from the database. – borak Jul 14 '20 at 19:04
  • I just wondering if this is not a bug. But if both scripts exist, that should work. Did you check file permissions? Do both files have the same? – biesior Jul 14 '20 at 19:12
  • I also tried to use to create a button and try to do it by using if(isset['btnDelete']. However, it does not do anything, I tried to echo some string when I press, still nothing happens. – borak Jul 14 '20 at 19:19
  • add `var_dump($_GET)` to your process.php and check if it can see the data – biesior Jul 14 '20 at 19:23
  • Your logic is correct. Could you post your code execution sequences (delete, listing, page navigating, etc)? And could you post URLs for CRUD? Also, attach the URL when you got a 404 error – Liki Crus Jul 14 '20 at 19:28
  • @LikiCrus So, I don't quite get what u mean by code execution sequences. But, ill try. So, when I load up the page it will pull all the data from the database and display it in a table (index.php), each record(row) will have 2 action buttons - edit and delete though I haven't code the edit button. So, when I click the delete, I'm able to get to a new page and by using var_dump($_GET), it appears to show the right index but it doesn't delete the record from the database. I hope this is what you're looking for. :( – borak Jul 14 '20 at 19:37
  • In case of that listing logic is rendered and deletion is executed, you can get this situation. So you must put deletion logic ahead of listing. – Liki Crus Jul 14 '20 at 19:40
  • @LikiCrus I tried but it didn't work, unfortunately. I appreciate your help. – borak Jul 14 '20 at 19:59
  • @LikiCrus If you want to have a look at the whole thing here's the link to my repo [link]https://github.com/borakhoeun/CRUD.git – borak Jul 14 '20 at 20:07
  • @borak please check my GitHub and let me know if deletion does work. [https://github.com/swdreams/CRUD](https://github.com/swdreams/CRUD) – Liki Crus Jul 14 '20 at 20:35
  • @borak, your mistake is to use ``` isset($_GET['btnDelete']) ``` in process.php. The delete button is not a real button, it is a html A tag. So it is not passed as GET parameter when clicking on it. – Liki Crus Jul 14 '20 at 20:39
  • Yes, it should be $_GET['delete'] as that is the parameter passed in the URL. Not sure why the Edit goes to index.php and the delete goes to process.php. Seems strange to have two files handling the forms action. – TimBrownlaw Jul 14 '20 at 20:50
  • 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 Jul 14 '20 at 21:24
  • @LikiCrus Hello, the delete button is now working. I appreciate your help. Thank you! :) One question tho, normal do people use a HTML A tag and style it as a button or use the actual input tag? – borak Jul 15 '20 at 06:42
  • @Dharman Hi, I should really look into that. I did not know that. Appreciate your warning, thank you! – borak Jul 15 '20 at 06:44
  • @borak you can usual HTML A tag and button. No problem. One thing in your code, you need to add a confirmation dialog to make sure you want to delete an entry on click event of deletion. – Liki Crus Jul 15 '20 at 10:56

2 Answers2

0

When you refresh the page did the deleted row disappear, is the delete code in index.php or in process.php , if after page refresh disappear that mean the code run in index.php and the code not make the page refresh , if the code in process.php make sure the page is in the same directory ,when you say ( when I clicked on the delete button it says URL not found) that mean the code is working because it deleted the row but not refresh the table

  • You're right. Turns out the "process.php" wasn't in the same directory as index.php. Now when I pressed Delete, I'm able to get to a blank page with the right "id". However, the "supposed to be deleted" record still there. Basically, it failed to delete the selected record. – borak Jul 14 '20 at 19:25
  • I glad that my answer helped you , for the delete code when you hit the delete button and transferred to the pros.php that mean (header("location: index.php");) didn't executed and there is some wrong with the sql statement check tb name .. if u see my answer help you please make it as the correct answer ^_^ – Ahmed Alduri Jul 14 '20 at 19:32
0
  1. process.php should be included at the top of index.php. If not (you didn't include it at the top), header("Location: index.php"); will not work. In this case, you can't redirect using the PHP header function, and you should use javascript redirect function because the header was already sent to buffer. You can confirm it after enabling all logs using ini_set function.

Enabling all errors & warnings.

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
  1. process.php is located under includes/ folder. So you may get a 404 error in the redirection. That's after deleting an entry, your URL must be includes/index.php instead of index.php The easiest way is to put process.php at the same level directory of index.php

  2. Updated source: https://github.com/swdreams/CRUD

Liki Crus
  • 1,901
  • 5
  • 16
  • 27