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!
This is the Delete user function
All my files
This is how the users are displayed
45 Replies
Jochem
Jochem•2y ago
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
zellen99
zellen99OP•2y ago
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
zellen99
zellen99OP•2y ago
No description
zellen99
zellen99OP•2y ago
this file controls the routes
Jochem
Jochem•2y ago
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)
zellen99
zellen99OP•2y ago
php
<?php


class HomeController extends AppController
{
public function __construct()
{
$this->init();
}
public function init()
{
session_start();
if (isset($_SESSION['user'])) {
// private page
$data['title'] = 'PRIVATE HOMEPAGE';
$data['user'] = '<h2>HELLO, <i>' . $_SESSION['user'] . '</i></h2>';
$user = new UsersModel();
$data['mainContent'] = $user->displayData();
echo $this->render(APP_PATH . VIEWS . 'layoutAuth.html', $data);
} else {
// include APP_PATH.VIEWS . 'layout.php';
$data['title'] = 'HOMEPAGE';
// $data['mainContent'] = '<h2>HOME SPECIFIC CONTENT</h2>';
// $data['mainContent'] = $this->render(APP_PATH . VIEWS . 'list.html', array());
$data['mainContent'] = $this->render(APP_PATH . VIEWS . 'list.html', array());
echo $this->render(APP_PATH . VIEWS . 'layout.html', $data);
}
}
}
php
<?php


class HomeController extends AppController
{
public function __construct()
{
$this->init();
}
public function init()
{
session_start();
if (isset($_SESSION['user'])) {
// private page
$data['title'] = 'PRIVATE HOMEPAGE';
$data['user'] = '<h2>HELLO, <i>' . $_SESSION['user'] . '</i></h2>';
$user = new UsersModel();
$data['mainContent'] = $user->displayData();
echo $this->render(APP_PATH . VIEWS . 'layoutAuth.html', $data);
} else {
// include APP_PATH.VIEWS . 'layout.php';
$data['title'] = 'HOMEPAGE';
// $data['mainContent'] = '<h2>HOME SPECIFIC CONTENT</h2>';
// $data['mainContent'] = $this->render(APP_PATH . VIEWS . 'list.html', array());
$data['mainContent'] = $this->render(APP_PATH . VIEWS . 'list.html', array());
echo $this->render(APP_PATH . VIEWS . 'layout.html', $data);
}
}
}
https://github.com/claudiuzav/phpwebsite this is the github repositories
zellen99
zellen99OP•2y ago
i don't have nothin in the DeleteController because what I haved it was not good
php
<?php
$newUser = new UsersModel;
$newUser->delProducts($_GET['id']);

header("Location: layout.php");
php
<?php
$newUser = new UsersModel;
$newUser->delProducts($_GET['id']);

header("Location: layout.php");
it was only this
Jochem
Jochem•2y ago
hm, in UsersModel.php you're referencing a user table in every query but the Delete one
DELETE FROM `products` WHERE `id` = $id
DELETE FROM `products` WHERE `id` = $id
won't delete anything from the user table
zellen99
zellen99OP•2y ago
I change that, when I try to delete 1 user it says this
zellen99
zellen99OP•2y ago
No description
Jochem
Jochem•2y ago
ah, yeah, you need to include the id in the link
zellen99
zellen99OP•2y ago
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
Jochem
Jochem•2y ago
you have this in the working one:
echo '<td>' . "<a href='delete.php?id=" . $user['ID'] . "'>DELETE</a>";
echo '<td>' . "<a href='delete.php?id=" . $user['ID'] . "'>DELETE</a>";
and just this in the non-working one:
No description
Jochem
Jochem•2y ago
you're missing the ID parameter, so that href='?page=delete' needs to be something like href='?page=delete&id=" . $user['ID'] . "'
zellen99
zellen99OP•2y ago
now when I click the delete button it redirect me to another page but doesn't delete the user
Jochem
Jochem•2y ago
copy and paste the link in the rendered page for the DELETE link?
zellen99
zellen99OP•2y ago
localhost/Curs_22(MVC_part2)/home?page=delete&id=18 I changed the header to header("Location: home"); in the DeleteController
Jochem
Jochem•2y ago
you can't use route names in the header call, it should be header("Location: index.php"); wait, do you have RewriteRules set up?
zellen99
zellen99OP•2y ago
class LogoutController extends AppController
{
public function __construct()
{
$this->init();
}
public function init()
{
session_start();
session_destroy();
header("location: home");
exit;
}
}
class LogoutController extends AppController
{
public function __construct()
{
$this->init();
}
public function init()
{
session_start();
session_destroy();
header("location: home");
exit;
}
}
yes, I have the .htaccess
Jochem
Jochem•2y ago
ah, yeah, then you can use what's defined in that file it's not in the repo, that's why I was confused
zellen99
zellen99OP•2y ago
ohh, sorry
Jochem
Jochem•2y ago
no worries
zellen99
zellen99OP•2y ago
Options -Indexes

# IndexIgnore controllers models views

# IndexIgnore *.jpg banner?.png

RewriteEngine On
RewriteRule ^([\w]+)$ ?page=$1

ErrorDocument 403 /Curs_22(MVC_part2)/errors/error403.html
ErrorDocument 404 /Curs_22(MVC_part2)/errors/error404.html
Options -Indexes

# IndexIgnore controllers models views

# IndexIgnore *.jpg banner?.png

RewriteEngine On
RewriteRule ^([\w]+)$ ?page=$1

ErrorDocument 403 /Curs_22(MVC_part2)/errors/error403.html
ErrorDocument 404 /Curs_22(MVC_part2)/errors/error404.html
this is what it is in the file
Jochem
Jochem•2y ago
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 though
zellen99
zellen99OP•2y ago
ok thank a lot
Jochem
Jochem•2y ago
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)
zellen99
zellen99OP•2y ago
I hope so
Jochem
Jochem•2y ago
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
zellen99
zellen99OP•2y ago
ok, I will do that, thanks hey, I still haven't figured it out https://github.com/claudiuzav/phpwebsite
class DeleteController extends AppController
{
public function __construct()
{
$this->init();
}
public function init()
{
$newUser = new UsersModel();
$id = $_GET['ID'];
$deleted = $newUser->delProducts($id);
if ($deleted) {
header("Refresh: 1; url= home");
} else {
echo "ID is not set.";
}
}
}
class DeleteController extends AppController
{
public function __construct()
{
$this->init();
}
public function init()
{
$newUser = new UsersModel();
$id = $_GET['ID'];
$deleted = $newUser->delProducts($id);
if ($deleted) {
header("Refresh: 1; url= home");
} else {
echo "ID is not set.";
}
}
}
I put this in the DeleteController and it says that the Id is not set
Jochem
Jochem•2y ago
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);
zellen99
zellen99OP•2y ago
Array ( [page] => delete )
Array ( [page] => delete )
Jochem
Jochem•2y ago
hm, then you're not passing the ID in the query string what does the link look like?
zellen99
zellen99OP•2y ago
Curs_22(MVC_part2)/delete?&id=15
Curs_22(MVC_part2)/delete?&id=15
it's strange
Jochem
Jochem•2y ago
drop the &, that's only to differentiate between the first and second query param
zellen99
zellen99OP•2y ago
still the same
Jochem
Jochem•2y ago
did you refresh the page and re-click the link?
zellen99
zellen99OP•2y ago
yes
Curs_22(MVC_part2)/delete?id=15
Curs_22(MVC_part2)/delete?id=15
zellen99
zellen99OP•2y ago
Jochem
Jochem•2y ago
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=15
zellen99
zellen99OP•2y ago
it is not working
Jochem
Jochem•2y ago
right, I'll have to look more tomorrow then, sorry
zellen99
zellen99OP•2y ago
yea no problem, thx
Jochem
Jochem•2y ago
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)
zellen99
zellen99OP•2y ago
it doesn't work, I think is because of the htaccess, but nevermind, I give up thanks for helping me out
Want results from more Discord servers?
Add your server