Remove record from database, it says request URL not found
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.
- 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 usingini_set
function.
Enabling all errors & warnings.
ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);
-
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
-
Updated source: https://github.com/swdreams/CRUD
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