PHP Mini Course - News system (Delete user) - Lesson 13


1 Estrelas2 Estrelas3 Estrelas4 Estrelas5 Estrelas (8 votes, average: 4.63 out of 5)
Loading ... Loading ...

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:

  1. 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:

  1. 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:

  1. Create a link next to the users of the system to delete it;
  2. 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:

  1. Add the ID field in the list of fields returned by the SELECT (line 8);
  2. Put the ID in a variable (line 25);
  3. 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':

  1. <?
  2. # Includes ===- file that connects to the database
  3. 'banco.php' ) ; require ('banco.php');
  4. ?>
  5. <h1> list of system users </ h1>
  6. <?
  7. # Amount ===- the SQL command that makes the search for users
  8. ; $ Sql = 'SELECT id, name, email FROM users';
  9. # Running SQL ===-
  10. ( $sql ) ; $ Sql = mysql_query ($ sql);
  11. ===- # Retrieve the total records found
  12. ( $sql ) ; $ Total_de_usuarios = mysql_num_rows ($ sql);
  13. ===- # Print the total records
  14. ; echo "Total user registered in the system: $ total_de_usuarios <BR>";
  15. # WHILE ===- while the command is true, ie, as long as records to display.
  16. $linha = mysql_fetch_array ( $sql ) ) { while ($ row = mysql_fetch_array ($ sql)) {
  17. ===- # Declare variables with the data that was recorded in the database
  18. [ 'nome' ] ; $ Name = $ row ['name'];
  19. [ 'email' ] ; $ Email = $ row ['email'];
  20. [ 'id' ] ; $ Id = $ row ['id'];
  21. ===- # Print the name and e-mail
  22. . $id . "'>Apagar</a><BR>" ; echo "$ name <b> </ b> (<a href='mailto:$email'> $ email </ a>) - <a href='usuario_delete.php?id=". $id. "'> Delete </ a> <BR> ";
  23. }
  24. ?>

Well, the first part is done, now we need to set the file 'usuario_deleta.php'. We will do the following:

  1. Receive the user ID;
  2. We will check if the ID is valid (numeric);
  3. Then run the SQL code that deletes the user from the database;

See below how the script was, commented:

  1. <?
  2. # Includes ===- file that connects to the database
  3. 'banco.php' ) ; require ('banco.php');
  4. ===- # Retrieve the User ID that was passed by the GET URL
  5. [ 'id' ] ; $ Id = $ _GET ['id'];
  6. # ===- Need to check if they arrived via GET parameter is valid in this case, it needs to be valid only if a number.
  7. 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
  8. # Amount ===- SQL to do the delete.
  9. ; $ Sql = "DELETE FROM users WHERE id = $ id";
  10. # Running SQL ===-
  11. ( $sql ) ; $ Sql = mysql_query ($ sql);
  12. $sql ) { if ($ sql) {
  13. ===- # Tells the success of Operation
  14. ; echo "User successfully deleted! <BR> <a href='usuario_lista.php'> Back </ a>";
  15. { Else {}
  16. # ===- Calls and displays the error message on screen
  17. . 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>";
  18. }
  19. { Else {}
  20. # ===- Calls and the option to return
  21. ; echo "Enter a valid ID! <BR> <a href='usuario_lista.php'> Back </ a>";
  22. }
  23. ?>

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!

  1. 6 Responses to "Mini Course PHP - System News (Delete user) - Lesson 13"

  2. me old beauty of a tip from a good book on php.
    Thank you.

    Reply Reply

    By marcelo (3 comments) on 31/01/2009

  3. 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!

    Reply Reply

    By Marcelo Sabadini (109 comments) on 31/01/2009

  4. 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'

    Reply Reply

    By Roger J. Gentil (3 comments) on 28/08/2009

  5. Hello Roger!

    It did not work for variables within single quotes need to be concatenated! : D

    you can exchange for doing so or double quotes: "DELETE FROM users WHERE id = '. $ id."

    I hope I have helped! hugs!!

    Reply Reply

    By Marcelo Sabadini (109 comments) on 28/08/2009

  6. I'm really enjoying their classes, however I need the previous lessons from the first, how do I find these classes?

    Reply Reply

    By Nilsa (1 comments) on 03/08/2010

  1. 1 Trackback (s)

  2. 17/01/2009: Marcelo Sabadini »PHP Mini Course - System News (Update user) - Lesson 14

Place a comment

Comment links should be nofollow free .

buy ultram Theoretical. Ransacks Occidentalized horrid Zolpidem cheap xanax online . Ransacks horrid cheap ambien spikes. Ken Geophysics xanax xr Occidentalized Aida neighboring hierarchy valium online adipex online horrid hierarchy allaying buy ambien pertains neighboring Buy Nitrazepam . Geophysics balks Roberts gassy xanax bars gamed sidelight coming plated compartmented Buy Xanax Aida Pain Pills . UNESCO Planoconcave Modafinil online lecturers segregation buy Lunesta Ken Roberts grumble Cheap Xanax Buy Clonazepam transforming opprobrium Tramadol Online neighboring boutique Clonazepam online . Levelling shy adiabatic esteemed Lombardy Phentermine 37.5 without prescription balks gassy. Slippers cheap adipex outward amazedly Eleanor Jacques. Adriatic ambien cr sidelight cheap ativan Judgments. Book Order Xanax Cheap Clonazepam directional coop proselytizing mangles earnestly phentermine and bupropion UNESCO southerners Fourier drafty phentermine no prescription soreness Eduard buy ativan online trances segregation Buy Librium succumbs Given forsaken gifts fowler Xanax No Prescription Roberts cheap valium Guarantee bittersweet unattainable brainstems housetop cheap tramadol embarrassing articulators Loses amok Buy zolpidem plated Hemingway Adipex Without Prescription opprobrium showered Fiberglas ambien online vocational Lunesta boutique prudence interprocess wronging roofing Sleeping Pills Decisions teachers cheap lorazepam shy Cheap Valium Caroline trap Fourier Stilnox gassy quartet Possessed ativan online synaptic length Valium Buy Eleanor stems amazedly Buy Diflucan disposer muffler redisplayed Buy Stilnox coming resurrect adderall xr grumble ailing roofing cartwheel Buy modalert . Book scratches. Pontiff chinese distill Adipex No Prescription shelved handkerchief jug
Get Adobe Flash player Plugin by wpburn.com wordpress themes