This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
en:web_development:forms:insert [2022/01/19 14:42] mag created |
en:web_development:forms:insert [2023/08/16 09:33] (current) |
||
---|---|---|---|
Line 31: | Line 31: | ||
< | < | ||
<ul> | <ul> | ||
+ | <li> | ||
+ | < | ||
+ | <input type=" | ||
+ | </li> | ||
<li> | <li> | ||
< | < | ||
- | <input type=" | + | <input type=" |
</li> | </li> | ||
<li> | <li> | ||
Line 72: | Line 76: | ||
while($row = $hrslt-> | while($row = $hrslt-> | ||
?> | ?> | ||
- | <option value="<? | + | <option value="<? |
<?php | <?php | ||
} | } | ||
Line 120: | Line 124: | ||
* Pick a hero / villain from the following list and add it to the table. If you're working in a class, each pick a different character. | * Pick a hero / villain from the following list and add it to the table. If you're working in a class, each pick a different character. | ||
- | ^character_name^alias^hero_villain^first_appeared^power^ | + | ^alias^identity^hero_villain^first_appeared^power^ |
- | |Thor Odinson|Thor|Hero|1962|Magic| | + | |Thor|Thor |
- | |Johan Schmidt|Red Skull|Villain|1941|Biological| | + | |Red Skull|Johan Schmidt|1941|Biological| |
- | |Bucky Barnes|Winter Soldier|Villain|1941|Tech| | + | |Winter SolBucky |
- | |Clint Barton|Hawkeye|Hero|1969|Skill| | + | |Clint Barton|Hawkeye|1969|Skill| |
- | |Odin|Odin|Hero|1962|Magic| | + | |Odin|Odin|1962|Magic| |
- | |Nick Fury|Nick Fury|Hero|1963|Skill| | + | |Nick Fury|Nick Fury|1963|Skill| |
- | |Phil Coulson|Agent Coulson|Hero|2008|Skill| | + | |Phil Coulson|Agent Coulson|2008|Skill| |
- | |Peter Quill|Starlord|Hero|1976|Tech| | + | |Peter Quill|Starlord|1976|Tech| |
- | |Gamora|Gamora|Hero|1975|Skill | + | |Gamora|Gamora|1975|Skill |
- | |Pepper Potts|Rescue|Hero|1963|Tech| | + | |Pepper Potts|Rescue|1963|Tech| |
- | * Enter the data for the chosen character | + | * Enter the information |
- | * Notice at the top of the screen | + | * See the query used to add the data just below the yellow |
- | * It will look something like this. | + | < |
- | < | + | * This is the format we need to use for our insert query in ' |
- | * This is the query format we need to use in our page to add new information. | + | * An INSERT query always begins |
- | * It starts | + | * This is followed by 'VALUES' |
- | * Next, inside < | + | |
- | * Then < | + | |
- | * Actually, we'll change this a little. Hackers will sometimes use [[https:// | + | |
- | * The query then looks like this. | + | |
- | < | + | |
- | * With an arguments array like this. | + | |
- | < | + | |
==== Switch Statement ==== | ==== Switch Statement ==== | ||
- | * Return | + | * Remember that we actually have two forms sent to this page by two submit buttons. |
- | * Open ' | + | * We can identify |
- | * First we must determine | + | * We could do this with an 'if' |
- | * You know how to check each one using < | + | |
- | * At the end of the first PHP block, type switch and select ' | + | |
- | * You'll see this. | + | |
< | < | ||
- | switch ($variable) { | + | |
+ | |||
+ | } else if ($_POST[' | ||
+ | |||
+ | } else { | ||
+ | |||
+ | }</ | ||
+ | * This would work, but there is a better way. | ||
+ | * A ' | ||
+ | * Open ' | ||
+ | * At the end of the first PHP block, type ' | ||
+ | < | ||
case ' | case ' | ||
# code... | # code... | ||
Line 163: | Line 168: | ||
break; | break; | ||
}</ | }</ | ||
- | * < | + | * This statement checks |
- | * The expected results are listed in separate | + | * If it doesn' |
- | * < | + | * This means that if don't include |
- | * Also note < | + | < |
- | * Replace | + | case 1: |
- | * Replace ' | + | case 17: |
- | * Add another < | + | case 43: |
- | * Delete '# code...' | + | # code... |
- | * The code should look like this so far. | + | break; |
- | < | + | case 22: |
- | | + | case 39: |
+ | # code... | ||
+ | break; | ||
+ | }</ | ||
+ | * We want to run a different query depending on the value of $_POST[' | ||
+ | * Now set up a case each for 'Add Hero' | ||
+ | < | ||
case 'Add Hero': | case 'Add Hero': | ||
break; | break; | ||
- | case ' | + | case ' |
break; | break; | ||
default: | default: | ||
break; | break; | ||
}</ | }</ | ||
- | | + | * Insert our code that we use for running database queries after < |
- | ==== PDO Query ==== | + | < |
+ | $args | ||
+ | $rslt | ||
+ | $rslt-> | ||
+ | * We can now copy our query from PHPMyAdmin to the query string. | ||
+ | * Because hackers can perform 'SQL Injection' | ||
+ | * Replace each value with < | ||
+ | < | ||
+ | $args | ||
+ | * This code will work unless there is a problem accessing the database or invalid user input. | ||
+ | * Since we want to know if there' | ||
+ | * Change < | ||
+ | < | ||
- | * Let's start with the code to add a hero. Add a new line after < | + | } else { |
- | * Add the query and arguments. | + | |
- | < | + | |
- | | + | * Add a message |
- | </ | + | |
- | * Now add the code to execute our query. | + | |
- | < | + | |
- | $rslt-> | + | |
- | * Because we want to know whether it worked, let's update the last line to return | + | |
< | < | ||
$message = " | $message = " | ||
+ | } else { | ||
+ | $message = "There was a problem inserting {$_POST[' | ||
+ | }</ | ||
+ | * The first message will be shown when the query executed successfully. | ||
+ | * The second message will be shown if it wasn' | ||
+ | |||
+ | ==== Multiple Inserts ==== | ||
+ | * The Add Appearances form allows the user to add multiple movies, but each one is its own line in the database table. | ||
+ | * We could create a separate query for each movie, but its also possible to add multiple lines in the same query doing something like this. | ||
+ | < | ||
+ | |||
+ | ==== Array Push ==== | ||
+ | * Let's leave the query for last because we will need to filter out already existing appearances. | ||
+ | * First let's build our array of arguments - a pair for each movie. | ||
+ | * Each pair of arguments is the name of our hero and the name of the movie, which we can do in a < | ||
+ | < | ||
+ | |||
+ | }</ | ||
+ | * And we can add them to an existing array using the < | ||
+ | < | ||
+ | * The first parameter in this function must be an array, and any values after it are added to the end of the array. | ||
+ | * Of course, we need to create the array first, and we don't want it recreated every time we loop so we need to create it before < | ||
+ | < | ||
+ | foreach($_POST[' | ||
+ | array_push($args, | ||
+ | }</ | ||
+ | * But we don't want to add movies that are already listed in the database, so we should check whether they exist first. | ||
+ | < | ||
+ | $mquery = " | ||
+ | $margs | ||
+ | $rslt = $pdo-> | ||
+ | $rslt-> | ||
+ | if (!$row = $rslt-> | ||
+ | array_push($args, | ||
+ | }</ | ||
+ | * Can you read this code for yourself? | ||
+ | * $mquery is the query asking whether the hero / movie pair already exists. | ||
+ | * $margs is the array of parameters for the query. | ||
+ | * We then run the query and if it doesn' | ||
+ | * What if it's a new movie? We don't want to add the word ' | ||
+ | * Let's check using an 'if statement' | ||
+ | < | ||
+ | array_push($args, | ||
+ | } else { | ||
+ | // check if it exists already | ||
+ | $mquery = " | ||
+ | $margs | ||
+ | $rslt = $pdo-> | ||
+ | if (!$rslt-> | ||
+ | array_push($args, | ||
+ | } | ||
+ | }</ | ||
+ | * If it's new, then we use < | ||
+ | |||
+ | ==== Count ==== | ||
+ | * We could have created the query first and added pieces in the loop, but there is a more elegant way. | ||
+ | * If we know how many movies have been added, we can simply add the right number of < | ||
+ | * We can get the number of elements in the < | ||
+ | * < | ||
+ | * Add the following code after the < | ||
+ | < | ||
+ | |||
+ | ==== Repeating String ==== | ||
+ | * We then need to add exactly that many copies of < | ||
+ | < | ||
+ | * This technique uses a function called < | ||
+ | * Now, our query is: | ||
+ | < | ||
+ | * Before we run our query, we should check that code works, that both query and arguments look as they should. | ||
+ | * Add the following before < | ||
+ | < | ||
+ | * Save and upload the code, then open ' | ||
+ | * Choose a hero and select a number of movies including the new one. Enter a movie that isn't already in our list anywhere and click 'Add Appearance' | ||
+ | * Now check the error logs in Putty. | ||
+ | * If the query and arguments array look good, it's time to send them to the database. | ||
+ | < | ||
+ | $rslt = $pdo-> | ||
+ | if ($rslt-> | ||
+ | $message = " | ||
+ | } else { | ||
+ | $message = "There was a problem inserting $movies appearances."; | ||
+ | }</ | ||
+ | * This code also includes the check to see if the query was successful and adds an appropriate message in either case. | ||
+ | * Save and upload your code, then open ' | ||
+ | * The one problem we still have is that if the user only adds movies that are already in the database, we're creating a query and trying to submit it with no values. | ||
+ | * Let's fix that by stopping if there are no new movies to add. | ||
+ | < | ||
+ | $values = str_repeat(' | ||
+ | $query = " | ||
+ | error_log(" | ||
+ | $rslt = $pdo-> | ||
+ | if ($rslt-> | ||
+ | $message = " | ||
+ | } else { | ||
+ | $message = "There was a problem inserting $movies appearances."; | ||
+ | } | ||
+ | } else { | ||
+ | $checked = count($_POST[' | ||
+ | $message = "All $checked appearances are already in the database."; | ||
}</ | }</ | ||
- | * Note that < | + | * Rather than referring to < |
- | * Then we can display | + | * This has been a big activity, but there' |
- | < | + | |
- | < | + | |
- | </ | + | |
- | + | ||
- | ==== New Movie ==== | + | |
+ | [[en: |