Mini Course PHP , PHP
- 15/01/2009
PHP Mini Course - News system (Delete user) - Lesson 13
Good staff, our course is walking a bit slow - I admit - but I promise it will not stop. I will make every effort to finalize it. I have received several e-mails praising the content and asking for more posts. I am very happy to see that the content of my blog this is useful.
Today, we'll learn how to delete a record from the database. In the last lesson we saw how to insert data (INSERT) in the bank and how to select (SELECT) to display them.
Below is how the SQL code to perform this task:
- DELETE FROM table_name
But so often we will delete one (s) record (s) specific (s) of the database. This is where we create the ID field in the table, do you remember? It serves to create a whole number is incremented and always, or never have two records with the same ID. This ID is used to specify "who" will be deleted. So how would one see below SQL code completion:
- campo_criterio = 'valor' DELETE FROM table_name WHERE campo_criterio = 'value'
Note that we use the WHERE command to tell which record will be deleted. If you do not specify a WHERE ALL THE RECORDS OF THIS TABLE WILL BE DELETED.
To view the complete documentation of this SQL command: http://dev.mysql.com/doc/refman/5.0/en/delete.html
Well, now we know in theory how this bid to erase things from the database, now let's apply this to our news system. Here's what will be done today:
- Create a link next to the users of the system to delete it;
- Create the file that will be responsible for clearing the record;
So get to work !!!!!!
We need to create a link that when the person clicks, the user will be deleted. But as we know, we need to know which user is deleted from the database. But how do we know this? Using the ID field. By clicking on the link 'Delete', the system directed to the 'usuario_delete.php', this in turn will need to get the user ID to be deleted. We will pass this parameter via GET, or will put the user ID in the URL. Open the file 'usuario_lista.php' and make the following adjustments:
- Add the ID field in the list of fields returned by the SELECT (line 8);
- Put the ID in a variable (line 25);
- Create the link 'Delete' passing the ID via GET to the page 'usuario_delete.php' (line 28);
Below is the code looks like the file 'usuario_lista.php':
- <?
- # Includes ===- file that connects to the database
- 'banco.php' ) ; require ('banco.php');
- ?>
- <h1> list of system users </ h1>
- <?
- # Amount ===- the SQL command that makes the search for users
- ; $ Sql = 'SELECT id, name, email FROM users';
- # Running SQL ===-
- ===- # Retrieve the total records found
- ===- # Print the total records
- # WHILE ===- while the command is true, ie, as long as records to display.
- ===- # Declare variables with the data that was recorded in the database
- [ 'nome' ] ; $ Name = $ row ['name'];
- [ 'email' ] ; $ Email = $ row ['email'];
- [ 'id' ] ; $ Id = $ row ['id'];
- ===- # Print the name and e-mail
- . $id . "'>Apagar</a><BR>" ; echo "$ name <b> </ b> (<a href='mailto:$email'> $ email </ a>) - <a href='usuario_delete.php?id=". $id. "'> Delete </ a> <BR> ";
- }
- ?>
Well, the first part is done, now we need to set the file 'usuario_deleta.php'. We will do the following:
- Receive the user ID;
- We will check if the ID is valid (numeric);
- Then run the SQL code that deletes the user from the database;
See below how the script was, commented:
- <?
- # Includes ===- file that connects to the database
- 'banco.php' ) ; require ('banco.php');
- ===- # Retrieve the User ID that was passed by the GET URL
- [ 'id' ] ; $ Id = $ _GET ['id'];
- # ===- Need to check if they arrived via GET parameter is valid in this case, it needs to be valid only if a number.
- is_numeric ( $id ) ) { // Veja a documentacao da funcao is_numeric(): http://br2.php.net/manual/pt_BR/function.is-numeric.php if ( is_numeric ($ id)) {/ / See the documentation of the function is_numeric (): http://br2.php.net/manual/pt_BR/function.is-numeric.php
- # Amount ===- SQL to do the delete.
- ; $ Sql = "DELETE FROM users WHERE id = $ id";
- # Running SQL ===-
- $sql ) { if ($ sql) {
- ===- # Tells the success of Operation
- { Else {}
- # ===- Calls and displays the error message on screen
- . mysql_error ( ) . " <BR><BR><a href='usuario_lista.php'>Voltar</a>" ; echo "Error deleting user! -". mysql_error (). "<BR> <a href='usuario_lista.php'> Back </ a>";
- }
- { Else {}
- # ===- Calls and the option to return
- }
- ?>
They saw how easy it is? Of course, missing a lot, for example, ask for confirmation before deleting ... like, "Really delete this user?". I'll leave you to do this, who want to implement this feature can make it and post here on the blog to share .. and stay with doubt please email me.
I hope I was useful again!
A big hug!



6 Responses to "Mini Course PHP - System News (Delete user) - Lesson 13"
me old beauty of a tip from a good book on php.
Thank you.
By marcelo (3 comments) on 31/01/2009
Oliveira Good afternoon all good?
So tell me ... you already have some knowledge in PHP?
I bought a very good book to study to get certified. He comes to PHP 5. ... talks about Object Orientation ...
The link in the book is in the Submarine: http://www.submarino.com.br/produto/1/877859/?franq=267589
I hope I helped .. a big hug!
By Marcelo Sabadini (109 comments) on 31/01/2009
With me, the exclusion 'DELETE FROM users WHERE id = $ id' only worked when I modified as follows:
'DELETE FROM users WHERE id ='. $ Id. "
because it always returned the error: Unknown column '$ id' in 'where clause'
By Roger J. Gentil (3 comments) on 28/08/2009
Hello Roger!
It did not work for variables within single quotes need to be concatenated!
you can exchange for doing so or double quotes: "DELETE FROM users WHERE id = '. $ id."
I hope I have helped! hugs!!
By Marcelo Sabadini (109 comments) on 28/08/2009
I'm really enjoying their classes, however I need the previous lessons from the first, how do I find these classes?
By Nilsa (1 comments) on 03/08/2010