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/21 14:05] 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 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 ==== | ||
| Line 215: | Line 225: | ||
| ==== Array Push ==== | ==== Array Push ==== | ||
| - | * Let's leave the query for last. | + | * 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. | * 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 < | * Each pair of arguments is the name of our hero and the name of the movie, which we can do in a < | ||
| < | < | ||
| - | }< | + | }</code> |
| * And we can add them to an existing array using the < | * And we can add them to an existing array using the < | ||
| < | < | ||
| Line 234: | Line 244: | ||
| $margs | $margs | ||
| $rslt = $pdo-> | $rslt = $pdo-> | ||
| - | | + | $rslt-> |
| + | if (!$row = $rslt-> | ||
| array_push($args, | array_push($args, | ||
| }</ | }</ | ||
| Line 240: | Line 251: | ||
| * $mquery is the query asking whether the hero / movie pair already exists. | * $mquery is the query asking whether the hero / movie pair already exists. | ||
| * $margs is the array of parameters for the query. | * $margs is the array of parameters for the query. | ||
| - | * We then run the query and if it doesn' | + | * We then run the query and if it doesn' |
| - | * What if it's a new movie? | + | * What if it's a new movie? |
| * Let's check using an 'if statement' | * Let's check using an 'if statement' | ||
| < | < | ||
| Line 258: | Line 269: | ||
| ==== Count ==== | ==== Count ==== | ||
| * We could have created the query first and added pieces in the loop, but there is a more elegant way. | * 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 < | + | * 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 < | * We can get the number of elements in the < | ||
| * < | * < | ||
| Line 265: | Line 276: | ||
| ==== Repeating String ==== | ==== Repeating String ==== | ||
| - | * We then need to add exactly that many copies of < | + | * We then need to add exactly that many copies of < |
| < | < | ||
| * This technique uses a function called < | * This technique uses a function called < | ||
| * Now, our query is: | * Now, our query is: | ||
| - | < | + | < |
| * Before we run our query, we should check that code works, that both query and arguments look as they should. | * Before we run our query, we should check that code works, that both query and arguments look as they should. | ||
| * Add the following before < | * Add the following before < | ||
| Line 276: | Line 287: | ||
| * 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' | * 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. | * 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: | ||