Deleting a User PHP Help

In programming a new problem often presents new challenges new techniques that must be grasped new language features that must be absorbed Those are fun times, but they can also be frustrating Your pace slows to a crawl, and it’s often at least a few hours-and sometimes a few days-before it seems like you make real progress Then there are times that your accumulated pile of tricks knowledge and experience stand higher than the new task which is where you are now This is one of those easy tasks: deleting a user.

Surveying the Individual Components

You already know the query for deleting a user from the users table DELETE FROM users Add to this a WHERE clause to target a particular user DELETE FROM users WHERE user id (some_user_id) Nothing new here But how do you get that user _id? Well you can get it from whatever script calls your script And you already have that in place in show_user.php.

Surveying the Individual Components

Once this code is converted to HTML you’ll get this:

Surveying the Individual Components

This should look similar to something you’ve done before when you sent a user _id to the show_user.php script.

Surveying the Individual Components

Once you’ve grabbed a user _id and deleted the user you can just redirect back to your show_your.php script which will re-SELECT from users but this time the deleted user will simply be gone Perfect.

Putting It All Together

At this point it’s just a matter of retyping various bits from your other scripts and changing a few things here and there The result delete_user.php shown here.

Putting It All Together

Putting It All Together

Putting It All Together

Real Programmers Cut and Paste All the Time

In delete_users.php you’ve written your first script that involves almost a complete reuse of code that you’ve already written But this code doesn’t really belong in so it’s not a case where you need to abstract out bits of code here and there and put them into utility functions as was the case with handling errors or setting up database connections.

At this point if you’ve read many programming becks you’re ready for a tongue-lashing or at least some mild finger wagging don’t cut and pastel Cutting and pasting code is evil cutting and pasting code will lead to annoying difficult to find mistakes cutting and pasting will cause you to gain 10 pounds and hamper your sex life Well maybe not that last one.

Despite all the warnings every programmer who spends more than a few hours a day writing code knows the shortcut keys to copy cut and paste and uses them liberally If they’re making their living coding they probably know the shortcuts not just on a Mac or in Windows but in emacs and vi and any other editor they might use.

So why all the dire warnings It’s true some of the hardest bugs to track down are the ones caused by cutting copying and pasting code and the little inconsistencies introduced as a result For example in one bit of copied code a variable might be called $insert _sql and in another it’s called $insert _query Things go haywire PHP doesn’t always do a great job reporting what the problem is and you’re left to sort out the mess Realistically, though that’s not a copying and pasting problem that’s an inconsistency-in- naming-variables problem So here come the common-sense warnings.

Know that you’re adding risk when you copy cut or paste Be careful and take your time.

When possible cut and copy from as few sources as possible You’re less likely to end up with mismatches between variable names and the like.

• Consider having two windows open  on preceding page) or two tabs open (Figure 11-6) and moving between them, rather than copying, closing a file, opening the new file, and pasting. This arrangement makes it easier to compare code; you can simply move back and forth between open windows.

• test your code after you’ve pasted something That way you catch potential errors quickly and can track them down while you still remember which code you just dropped in.

That’s it Keep those things in mind and don’t be afraid to cut and paste They’re important tools in your arsenal.

Real Programmers Cut and Paste All the Time

Real Programmers Cut and Paste All the Time

So try it out You already have with the correct links open it. and pick an unlucky user to delete Click the “X” icon and you should get back something like Figure -7-which looks just like Figure 11- 3 minus poor David Ramirez (Don’t worry he’ll write another heart-breaking sad song about his deletion if he finds out.)

 

Real Programmers Cut and Paste All the Time

Real Programmers Cut and Paste All the Time

Deleting Users Shouldn’t Be Magical

The functionality that you have in place for deleting users is perfect. There are no hitches, no pauses, nothing but a quick request to detete_user.php a deletion in your database and a return to show_users.php And that perfection-that minimal pause and nothing else-is exactly why you’re not at all done with deleting users.

Deletion is a big deal You’re trashing information never to be heard from again What’s really concerning is that you’re doing it based on one mouse click with no further warning or second thought That’s a problem.

In fact think about your own web usage Have you ever managed to delete anything with one click Most of the time you’re inundated by pop-up windows asking Are you sure and You’ll never get to use this file again and even Be careful Your information will be gone forever All these warnings are a nuisance but they’re there to prevent you from accidentally deleting something that you can never get back

With that in mind you need to add a little more to the deletion process. You must give the user a chance to rethink her decision before you pass things on to delete_user.php SO it’s back to show_ user.php

START WITH A LITTLE JAVA SCRIPT

When it comes to things like confirmation boxes you’re firmly in the world of browsers and clients Although you could build some sort of confirmation in PHP it wouldn’t be pretty You’d essentially need to send a request to the server for deletion the server would run a PHP script that creates a new HTML form and asks for confirmation; the browser would return that to the user and the user would click OK. Then another request would go to the browser at which point you’d finally get to perform deletion.

Even if you used Ajax to avoid lots of page refreshing this is way too much server interaction for a simple confirmation That’s especially true because JavaScript offers you a built in all client means of doing this by using confirm.

Open show_user.php and add some JavaScript:

START WITH A LITTLE JAVA SCRIPT

In a nutshell, you’re simply creating a function that asks for user confirmation before passing control over to delete_user.php There’s a little extra work involved here because the user _id has to be passed to this function, which then shuffles it along to delete_user.php by using the JavaScript version of a redirect window .location.

In or Out JavaScript

Almost as common as the scolding you’ll get for copying and pasting is the admonition to never ever use JavaScript.

In or Out JavaScript

In fact, most books deal with the problem a bit like this:
1. Learn how to write a little JavaScript.
2. Learn how to write some cool JavaScript.
3. Now that you’re advanced set up that JavaScript as external files.

4. Teach all your beginner JavaScript friends to do the same Sounds reasonable, but take a look at the source for pages like Amazon.com Google or Apple Everyone of these Web giants has <script> tags that have code in the head of the page Surely the high-paid folks at powerhouses such as these know what they’re doing The truth is that there are plenty of times when you want some well-placed JavaScript in the head of your page Most notably this is true for JavaScript that is specific.

If you have utility functions, such as creating generic dialog in j Query (stay tuned for more on that) or handling validation for certain data types, put those things in a script .file and reference it in all your pages, That’s the same sort of thing that you’ve done with a site-wide (SS file as well as on the server.

But, delete_user the JavaScript function you just wrote is only useful for this one page. It doesn’t belong in a site-wide utility script. and only adds to the clutter if that’s where you put it. You could create external scripts for every page on your site but that would be way too much of a mess.

That’s not to say you should have lots of JavaScript littering your page stuck between <p> elements and in the crevices between adjacent <td> tags. Just don’t be scared to write some JavaScript in your page Just like copy-and-paste it’s a tool for you to use wisely and judiciously.

FINISH WITH A CHANGE IN LINKING

You have your JavaScript in place, and now it’s time for the big finish: just change the link that previously went directly to deiete_user.pho in your page to call your new Java Script function

In or Out JavaScript

FINISH WITH A CHANGE IN LINKING

Try it out and you’ll finally get a handy warning before you push Master Jason Waddle down the deletion black hole.

FINISH WITH A CHANGE IN LINKING

FINISH WITH A CHANGE IN LINKING

 

Posted on January 13, 2016 in Listing Iterating and Administrating

Share the Story

Back to Top