This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
en:web_development:forms:insert [2022/01/19 16:06] 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 105: | Line 109: | ||
include(' | include(' | ||
include(' | include(' | ||
- | |||
- | print_r($_GET); | ||
?> | ?> | ||
< | < | ||
Line 122: | 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|Thor|1962|Magic| | + | |Thor|Thor |
- | |Johan Schmidt|Red Skull|1941|Biological| | + | |Red Skull|Johan Schmidt|1941|Biological| |
- | |Bucky Barnes|Winter Soldier|1941|Tech| | + | |Winter SolBucky |
|Clint Barton|Hawkeye|1969|Skill| | |Clint Barton|Hawkeye|1969|Skill| | ||
|Odin|Odin|1962|Magic| | |Odin|Odin|1962|Magic| | ||
Line 134: | Line 136: | ||
|Pepper Potts|Rescue|1963|Tech| | |Pepper Potts|Rescue|1963|Tech| | ||
+ | * Enter the information for your hero into the appropriate fields in the ' | ||
+ | * See the query used to add the data just below the yellow row at the top of the page. | ||
+ | < | ||
+ | * This is the format we need to use for our insert query in ' | ||
+ | * An INSERT query always begins with ' | ||
+ | * This is followed by ' | ||
+ | |||
+ | ==== Switch Statement ==== | ||
+ | * Remember that we actually have two forms sent to this page by two submit buttons. | ||
+ | * We can identify which form to process by the value of ' | ||
+ | * We could do this with an ' | ||
+ | < | ||
+ | if ($_POST[' | ||
+ | |||
+ | } 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 ' | ||
+ | # code... | ||
+ | break; | ||
+ | | ||
+ | default: | ||
+ | # code... | ||
+ | break; | ||
+ | }</ | ||
+ | * This statement checks the value of ' | ||
+ | * If it doesn' | ||
+ | * This means that if don't include < | ||
+ | < | ||
+ | case 1: | ||
+ | case 17: | ||
+ | case 43: | ||
+ | # code... | ||
+ | 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' and 'Add Appearance' | ||
+ | < | ||
+ | case 'Add Hero': | ||
+ | break; | ||
+ | case 'Add Appearance': | ||
+ | break; | ||
+ | default: | ||
+ | break; | ||
+ | }</ | ||
+ | * Insert our code that we use for running database queries after < | ||
+ | < | ||
+ | $args = array(); | ||
+ | $rslt = $pdo-> | ||
+ | $rslt-> | ||
+ | * We can now copy our query from PHPMyAdmin to the query string. | ||
+ | * Because hackers can perform 'SQL Injection' | ||
+ | * Replace each value with < | ||
+ | < | ||
+ | $args = array($_POST[' | ||
+ | * This code will work unless there is a problem accessing the database or invalid user input. | ||
+ | * Since we want to know if there' | ||
+ | * Change < | ||
+ | < | ||
+ | |||
+ | } else { | ||
+ | | ||
+ | }</ | ||
+ | * Add a message in this code block. | ||
+ | < | ||
+ | $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."; | ||
+ | }</ | ||
+ | * Rather than referring to < | ||
+ | * This has been a big activity, but there' | ||
+ | [[en: |