Learnit Training
process.php

We checked the entries and displayed them again in the browser. We did not save the data yet. We will do that now.

Task 7.1

  • Create a folder 7/ in phpcourse/.
  • Chapter 'Extended Example' of the free course PHP and MySQL. Learnit, also free online webinars with trainer and live online courses with trainer.

  • Save form.html in phpcourse/7/ (right click, save target as).
  • e.g. excel, time management, sql

  • Save this program code as process.php in phpcourse/7/.
  • Sitemap

View the source of phpcourse/7/process.php by opening it in Notepad

Look at what is happening, some things have changed:

There are pieces of text that

+31 20 636 9179

comment

  1. are added, they always begin with
  2. //
  3. . This code is not executed and only serves to make the code more readable for a programmer.
  4. The variable

$correct

 has been added. This variable keeps track of errors. As soon as we encounter an error, we set the bolean variable 

at false (it is not correct).The bottom piece of code is completely new: //Was alles correct ingevuld? if ($correct) { // Opslaan! // Vul hier de code in om de reactie op te slaan! echo "

Bovenstaande informatie is opgeslagen!
\n"; } else { // Er is ergens een foute waarde ingevoerd, geef de bezoeker de // mogelijkheid om terug te gaan echo "

Er is een foute waarde ingevoerd, ga terug.
\n"; } Contact At the site of

//Fill in the code here to save the reaction!

 We are about to insert the code that will store the data.
        

Check what the form does by going to http://localhost/phpcursus/7/form.html in your browser.

Change the line

 in:

$db = new PDO('mysql:host=localhost;dbname=reacties', 'root', ''); //kijk of een persoon al bestaat $query = "SELECT id FROM personen WHERE naam = ? AND email = ?"; $stmt = $db->prepare($query); $stmt->execute(array( $naam, $email)); if ($stmt->rowCount() > 0){ $row = $stmt->fetch(PDO::FETCH_ASSOC); $persoon_id = $row['id']; } else { // Voeg de naam en e-mail toe in de tabel personen $query = "INSERT INTO personen(naam, email) VALUES (?, ?)"; $stmt = $db->prepare($query); $stmt->execute(array( $naam, $email)); //vraag de id van de nieuwe persoon op $persoon_id = $db->lastInsertId(); } // voeg de reactie toe in de tabel reacties. Gebruik de id van de zojuist toegevoegde persoon $query = "INSERT INTO reacties(persoon_id, reactie, datum) VALUES (?, ?, ?)"; $stmt = $db->prepare($query); $stmt->execute(array( $persoon_id, $reactie, date('Y-m-d H:i:s'))); The above contains a lot of new things that we will discuss here. First, a connection is made with the MySQL server. For this, we make use of the PHP Data Object extension. This PHP extension makes it possible to communicate with the database from our PHP script. Our MySQL server runs on the

localhost

and we log in with the root account (actually this is not very nice, but for this demonstration it is the easiest), here no password is needed. The code to log in therefore looks like volt:

$db = new PDO('mysql:host=localhost;dbname=reactions', 'root', '');

  1. The result is a new database object. It is stored in the variable $db. We can use this object to communicate with the database.
  2. Next, we create SQL queries, which you will probably recognise from the previous chapter. However, there is one major difference. The placeholder for the name and e-mail address is a ?. By using this placeholder we protect ourselves against SQL injections. This is a common attack, where hackers try to add their own SQL code to the query. Unfortunately, we cannot go into detail about this. Fortunately, there is a lot of information available online about SQL injections. false We ask the database to prepare the query with root Our MySQL server runs on the
  3. $stmt = $db->prepare($query);
  4. . Then we run the queries with
  5. $stmt->execute(array( $name, $email));
  6. . Only now, the variables $name and $email are provided during execution. The SQL server places these in the query at the position of the question marks. falseOn working days from 9:00 to 17:30SELECT whether a person already exists with the name and e-mail address entered. The function $db->lastInsertId();
     ensures that the 
  7. id of the last query performed is used, thus associating a response with the correct person. Test the new script by going to the form again and adding some responses. Make sure that there are people who have multiple messages in the database. You can view the added record using PHPMyAdmin http://localhost/phpmyadmin. SELECTIf necessary, download the new version of the program code for verWork.php here (overwrite the old verWork.php).
  8. If all went well, you have filled the database with new data, we will now create a simple script to display this data.
  9. 7.2. Displaying data

info@learnit.nl

We remember from the previous chapter how to combine data from different tables. We modify the query a little by adding a ContactORDER BYSELECT command:root SELECT persons.name, persons.email, reactions.response, reactions.date FROM persons, reactions WHERE reactions.person_id = person.id ORDER BY reactions.date If we do not, the rows are returned in an undefined order. The following php script displays all responses: root Reacties

query($query); while ($row = $result->fetch(PDO::FETCH_ASSOC)) { echo "\n"; echo "\n"; echo "\n"; //pas deze regel aan! } ?>
van " . $row["naam"] . " op " . $row["datum"] . "
" . $row["reactie"] . "
 

Opdracht 5.3.

command. We then loop through the results of the query using the while loop. We print the result in an HTML table. root Our MySQL server runs on the

Assignment 7.2.

Save reactions.php in phpcourse/7/ (right click, save target as).

phpcourse/7/reactions.php corresponds to the above script.Go to http://localhost/phpcursus/7/reacties.php to view all the comments you have previously made. View the HTML code generated by the reactions.php script via the browser.7.3. Delete dataWe are going to phpcourse/7/reactions.phpSELECT modify it so that it contains a button for deleting reactions. The file already indicates where this button should be, namely on the line where it says: Contact

// Adapt this line!

. We are going to put a link on that line to

  1. remove.php
  2. place and send the
  3. of the reaction. The deletion itself is handled by the delete.php script:

Contact

Verwijderd prepare($query); $stmt->execute(array($reactie_id)); echo 'De reactie is verwijderd!
'; echo 'Ga terug naar de reacties.
'; } else { echo "Ongeldige aanvraag"; } ?>

Save delete.php in phpcourse/7/. Note that the file must be saved with the extension .php

Open phpcourse/7/reactions.php in Notepad

Change the line with 

echo " remove\n";

  1. Go to the comments section http://localhost/phpcursus/7/reacties.php Please make sure that comments are deleted after clicking on the link.
  2. Trainer
  3. Take this course with a trainer.
  4. Tracking with trainer
  5. PDF download
  6. This free course is available as a PDF download
  7. Download PDF
  8. Training and education

Online

BlogWorkshopsWebinars
Blended learning pathways

Last minuteTraining adviceCustomised/incompany

Last minuteCourse guideLearnitAbout usMissionClients command:Delivery termsPrivacy statementAbout usForms

Vacancies

Frequently asked questions

Transit guarantee

Cancellation policy

NRTO Code of Conduct

  1. Popular trainings
    Communication
  2. Project management
  3. Webdesign
Photoshop
Software development

Office, Excel and Word

  1. Information overview
  2. Ask a question
  3. Non-binding offer
process.php