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.
Once this code is converted to HTML you’ll get this:
This should look similar to something you’ve done before when you sent a user _id to the show_user.php script.
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.
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.
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.)
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.
In fact, most books deal with the problem a bit like this:
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.
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.