User Tools

Site Tools


en:web_development:forms:delete

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
en:web_development:forms:delete [2022/01/24 10:58]
mag created
en:web_development:forms:delete [2023/08/16 09:33] (current)
Line 71: Line 71:
                     <li>                     <li>
                         <label>Hero</label>                         <label>Hero</label>
-                        <select name="hero">+                        <select name="alias">
 <?php <?php
     // display list of heroes     // display list of heroes
Line 143: Line 143:
             foreach($_POST['movie'] as $movie) {             foreach($_POST['movie'] as $movie) {
                 if ($movie == 'new') {                 if ($movie == 'new') {
-                    array_push($args, $_POST['hero'], $_POST['new_movie']);+                    array_push($args, $_POST['alias'], $_POST['new_movie']);
                 } else {                 } else {
                     error_log($movie);                     error_log($movie);
                     // check if it exists already                     // check if it exists already
                     $mquery = "SELECT * FROM appearances WHERE alias=? AND movie=?";                     $mquery = "SELECT * FROM appearances WHERE alias=? AND movie=?";
-                    $margs  = array($_POST['hero'], $movie);+                    $margs  = array($_POST['alias'], $movie);
                     $rslt   = $pdo->prepare($mquery);                     $rslt   = $pdo->prepare($mquery);
                     $rslt->execute($margs);                     $rslt->execute($margs);
                     if (!$row = $rslt->fetch()) {                     if (!$row = $rslt->fetch()) {
-                        array_push($args, $_POST['hero'], $movie);+                        array_push($args, $_POST['alias'], $movie);
                     }                     }
                 }                 }
Line 201: Line 201:
   * Open the 'appearances' table in the 'webdev' database.   * Open the 'appearances' table in the 'webdev' database.
   * Select an entry and click 'Delete'.   * Select an entry and click 'Delete'.
-  * This time it won't show at the top of the screen. Instead, it shows a pop-up with +  * This time it won't show at the top of the screen. Instead, it shows a pop-up asking for confirmation that you want to run the given query. 
 +  * You can see the structure of the delete query in the popup. It looks something like this. 
 +<code>DELETE FROM `appearances` WHERE `appearances`.`id` = 17 </code> 
 +  * We only need to specify the table and the details of the specific record(s) we want deleted. 
 +  * We don't have the 'id' in the data from the form, but we can specify the exact record using both the alias and the movie. 
 + 
 +==== WHERE IN ==== 
 +  * As was the case when we were adding appearances, we can choose between deleting each movie individually or deleting a whole selection. 
 +  * We could identify all the checked movies like <html>(movie='Captain America' OR movie='Iron Man')</html> or we can do it more efficiently with <html>(movie IN ('Captain America', 'Iron Man')</html>. I prefer the second option. 
 +  * We need as many options for <html>IN</html> as we have movies, but we'll replace them with <html> ? </html> for security. 
 +  * Add the following code in the 'Delete Appearances' case. 
 +<code>            $movies = count($_POST['movie']); 
 +            $values = str_repeat('?,', $movies-1) . '?'; 
 +            $query = "DELETE FROM appearances WHERE alias=? AND movie IN ($values)";</code> 
 +  * The first line gets the number of movies. 
 +  * The second line creates the string of arguments for <html>IN</html>
 +  * The third line creates the query string. 
 +  * Now we need to create the array of arguments. 
 +<code>            $args  = array($_POST['alias']); 
 +            foreach($_POST['movie'] as $movie) { 
 +                array_push($args, $movie); 
 +            }</code> 
 +  * First we create the array, then we loop through the movies and add each one to the array. 
 +  * Now we send the query to the database, with a check on whether it worked or not. 
 +<code>            $rslt = $pdo->prepare($query); 
 +            if ($rslt->execute($args)) { 
 +                $message = "$movies appearances were deleted successfully."; 
 +            } else { 
 +                $message = "There was a problem deleting $movies appearances"; 
 +            }</code> 
 +  * Save and upload the code and check that everything works as expected. 
 + 
 +[[en:web_development:forms:javascript|Next: Javascript for Forms]]
en/web_development/forms/delete.1643050714.txt.gz · Last modified: 2023/08/16 09:33 (external edit)