This is an old revision of the document!
In this activity you'll learn how to obtain data from a form and insert it into a database table.
form.php
<?php include('database.php'); include('header.php'); include('menu.php'); // get list of heroes $query = "SELECT * FROM heroes"; $args = array(); $hrslt = $pdo->prepare($query); $hrslt->execute($args); // get list of movies $query = "SELECT movie FROM appearances GROUP BY movie"; $args = array(); $mrslt = $pdo->prepare($query); $mrslt->execute($args); ?> <main> <form name="add_hero" action="process_form.php" method="post"> <fieldset> <legend>Add Hero</legend> <ul> <li> <label>Hero Name:</label> <input type="text" name="name" size="30" maxlength="50"> </li> <li> <label>Side:</label> <ul> <li><input type="radio" name="side" value="Hero" checked> Hero</li> <li><input type="radio" name="side" value="Villain"> Villain</li> </ul> </li> <li> <label>First Appearance:</label> <input type="number" name="year"> </li> <li> <label>Source of Power:</label> <select name="power"> <option value="skill">Skill</option> <option value="bio">Biological</option> <option value="magic">Magic</option> <option value="tech">Technology</option> <option value="mutant">Mutant</option> </select> </li> </ul> <input type="submit" name="submit" value="Add Hero"> </fieldset> </form> <form name="add_appearances" action="process_form.php" method="post"> <fieldset> <legend>Add Appearances</legend> <ul> <li> <label>Hero</label> <select name="hero"> <?php // display list of heroes while($row = $hrslt->fetch()) { ?> <option value="<?php echo $row['character_name']; ?>"><?php echo $row['character_name']; ?></option> <?php } ?> </select> </li> <li> <label>Movie</label> <ul> <?php // display list of movies while($row = $mrslt->fetch()) { ?> <li><input type="checkbox" name="movie[]" value="<?php echo $row['movie']; ?>" id=""> <?php echo $row['movie']; ?></li> <?php } ?> <li><input type="checkbox" name="movie[]" value="new" id=""> <input type="text" name="new_movie"></li> </ul> </li> </ul> <input type="submit" name="submit" value="Add Appearance"> </fieldset> </form> </main> <?php include('footer.php'); ?>
process_form.php
<?php include('header.php'); include('menu.php'); ?> <main> <p><?php print_r($_POST); ?></p> </main> <?php include('footer.php'); ?>
character_name | alias | hero_villain | first_appeared | power |
---|---|---|---|---|
Thor | Thor | 1962 | Magic | |
Johan Schmidt | Red Skull | 1941 | Biological | |
Bucky Barnes | Winter Soldier | 1941 | Tech | |
Clint Barton | Hawkeye | 1969 | Skill | |
Odin | Odin | 1962 | Magic | |
Nick Fury | Nick Fury | 1963 | Skill | |
Phil Coulson | Agent Coulson | 2008 | Skill | |
Peter Quill | Starlord | 1976 | Tech | |
Gamora | Gamora | 1975 | ||
Pepper Potts | Rescue | 1963 | Tech |
INSERT INTO `heroes` (`character_name`, `alias`, `hero_villain`, `first_appeared`, `power`) VALUES ('Bucky Barnes', 'Winter Soldier', 'Villain', '1941', 'Tech');
// check which form if ($_POST['submit'] == 'Add Hero') { } else if ($_POST['submit'] == 'Add Appearance') { } else { }
switch ($variable) { case 'value': # code... break; default: # code... break; }
break
command.default:
.break
at the end of a case
, the code from the next case
will also run. This can be useful if you want the same code to run for multiple cases like this. (Don't add this code)switch ($age) { case 1: case 17: case 43: # code... break; case 22: case 39: # code... break; }
$_POST['submit']
.switch ($_POST['submit']) { case 'Add Hero': break; case 'Add Appearance': break; default: break; }
case 'Add Hero':
.$query = ""; $args = array(); $rslt = $pdo->prepare($query); $rslt->execute($args)
?
then add the respective user input as elements in the '$args' array.$query = "INSERT INTO `heroes` (`character_name`, `alias`, `hero_villain`, `first_appeared`, `power`) VALUES (?,?,?,?,?)"; $args = array($_POST['name'], $_POST['alias'], $_POST['side'], $_POST['year'], $_POST['power']);
$rslt->execute($args)
to the following.if ($rslt->execute($args)) { }
if ($rslt->execute($args)) { $message = "{$_POST['name']} was inserted successfully."; }