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/20 09:03] mag |
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|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 138: | ||
| * Enter the information for your hero into the appropriate fields in the ' | * 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. | * 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 ' | * This is the format we need to use for our insert query in ' | ||
| * An INSERT query always begins with ' | * An INSERT query always begins with ' | ||
| Line 196: | Line 200: | ||
| * Because hackers can perform 'SQL Injection' | * Because hackers can perform 'SQL Injection' | ||
| * Replace each value with < | * Replace each value with < | ||
| - | < | + | < |
| - | $args = array($_POST[' | + | $args = array($_POST[' |
| * This code will work unless there is a problem accessing the database or invalid user input. | * This code will work unless there is a problem accessing the database or invalid user input. | ||
| * Since we want to know if there' | * Since we want to know if there' | ||
| Line 203: | Line 207: | ||
| < | < | ||
| + | } else { | ||
| + | | ||
| }</ | }</ | ||
| * Add a message in this code block. | * Add a message in this code block. | ||
| < | < | ||
| $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 ==== | ==== Multiple Inserts ==== | ||
| * The Add Appearances form allows the user to add multiple movies, but each one is its own line in the database table. | * 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. | + | * 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. |
| - | * First we need to check how many movies have been added. | + | < |
| + | |||
| + | ==== 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, | ||
| + | < | ||
| + | foreach($_POST[' | ||
| + | array_push($args, | ||
| + | }</ | ||
| + | * But we don't want to add movies that are already listed in the database, so we should | ||
| + | < | ||
| + | $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: | ||