Delete users from DataBase
I have an website that displays a table with the users that are in the database.
I have the function to delete the users but I can't make it work.
I have a folder named "Controllers" with every single function to login,logout.... .
And I make one named "DeleteController" and in there I want to be the controller of the delete function.
And I can't make it work when I click the delete button from the website.
If you can help me I can send you the whole folder to look at it!
Thanks!
45 Replies
it's best to keep everything in public here, so others can help and learn later 🙂
That said, you're using Laravel?
Usually in MVC, the Controller is something that handles all the actions for a single Model. ProductController would have a Get, GetAll, Create, Update, and Delete method that would get called depending on the route and method you use to access the product endpoint
the connecting tissue is in the route file, that's where you tell the server which URL and Method are necessary to delete something. Can you share your routes file, or at least the relevant section?
as for the action itself, I see you're trying to delete something using a query string in an anchor tag, so you'd be using GET. It's generally a bad idea to perform actions based on GET requests that aren't just fetching data. Some robots might crawl your site or some future browser feature might try to prefetch your DELETE link to improve user performance and suddenly all your products are gone. Actions that cause changes in the database need to be a POST, because those will never be crawled or prefetched
I took a course, and this is what the teacher did, and right now I want to implement the delete button and I don't know how
this file controls the routes
that's fine, just adding some context, we can try to get this to work
what does HomeController look like, and all of DeleteController. If you've got this code on github or something that would be handy, or otherwise please paste them as code blocks (open with ```php on its own line (those are backticks, next to 1 on a QWERTY keyboard), and ending with ``` again on its own like. Kinda like
<?php
and ?>
, but for syntax highlighting in Discord messages)I think you might need to push,
DeleteController.php
is empty
https://github.com/claudiuzav/phpwebsite/blob/main/Curs_22(MVC_part2)/myApp/controllers/DeleteController.phpi don't have nothin in the DeleteController because what I haved it was not good
it was only this
hm, in UsersModel.php you're referencing a
user
table in every query but the Delete one
won't delete anything from the user tableI change that, when I try to delete 1 user it says this
ah, yeah, you need to include the id in the link
so I have this website that works
https://github.com/claudiuzav/phpwebsite2
the delete works
and how it work there I want to implement in this project
you have this in the working one:
and just this in the non-working one:
you're missing the ID parameter, so that
href='?page=delete'
needs to be something like href='?page=delete&id=" . $user['ID'] . "'
now when I click the delete button it redirect me to another page but doesn't delete the user
copy and paste the link in the rendered page for the DELETE link?
localhost/Curs_22(MVC_part2)/home?page=delete&id=18
I changed the header to header("Location: home"); in the DeleteController
you can't use route names in the
header
call, it should be header("Location: index.php");
wait, do you have RewriteRules set up?
yes, I have the .htaccess
ah, yeah, then you can use what's defined in that file
it's not in the repo, that's why I was confused
ohh, sorry
no worries
this is what it is in the file
it's time to start adding some
die('got to here')
statements to your code then
and print out some other logging info, like the SQL query it's trying to run, and values of interesting variables
It's late here, so I need to head off, but if you update the github files I can try to take a look again in the morning. Let me know if you fix it before then thoughok
thank a lot
I hope you figure it out, otherwise I'm sure we'll get to the bottom of it (or someone else will before I'm back online)
I hope so
you're close though. use print and exit to your advantage and try to debug some stuff. Run the code in your head line by line, see what you think it should do, then confirm that it does do that by printing things out
ok, I will do that, thanks
hey, I still haven't figured it out
https://github.com/claudiuzav/phpwebsite
I put this in the DeleteController and it says that the Id is not set
I'll have a look tomorrow, it's almost 1am here.
quick tip though, right above where you do that echo, put
print_r($_GET);
hm, then you're not passing the ID in the query string
what does the link look like?
it's strange
drop the &, that's only to differentiate between the first and second query param
still the same
did you refresh the page and re-click the link?
yes
hm, looking at your RewriteRules in .htaccess, I'm not sure this would work properly with a second parameter:
RewriteRule ^([\w]+)$ ?page=$1
\w
matches [A-Za-z0-9_]
, and not ?id=15. Even if it did, it would turn the url into something like ?page=delete?id=15
try turning that rule off temporarily by putting a # in front of it, and changing the link to ?page=delete&id=15it is not working
right, I'll have to look more tomorrow then, sorry
yea no problem, thx
I must've missed this because it was late last night, but GET parameters are case sensitive
This URL works when I run it on my machine:
http://localhost/?page=delete&ID=15
(works being relative, I don't have a db connection and I'm using PHP's builtin webserver which doesn't support htaccess rewrites)
it doesn't work, I think is because of the htaccess, but nevermind, I give up
thanks for helping me out