<?php require_once $_SERVER['DOCUMENT_ROOT'] . '/bootstrap.php'; if ($_SERVER['REQUEST_METHOD'] != "POST") { redirect_user('/people/index.php', "Bad method. Bad user!"); } $person_id = $_POST['id']; $person = \MyClasses\Models\Person::getOne($person_id); $destroyed = \MyClasses\Models\Person::destroy($person_id); redirect_user('/people/index.php', "You killed {$person['first_name']}!");
<?php require_once $_SERVER['DOCUMENT_ROOT'] . '/bootstrap.php'; if (!isset($_GET['id'])) { redirect_user('/people/index.php', 'No person found for ID ... or you didn\'t supply one!'); } $person_id = $_GET['id']; $person = \MyClasses\Models\Person::getOne($person_id); $page['title'] = 'Edit Person'; echo get_partial('header.php', ['page' => $page]); ?> <h1>Edit <?php echo $person['first_name'] . " " . $person['last_name']; ?> </h1> <form action="/people/update.php" method="POST" class="form-horizontal"> <div class="form-group"> <div class="col-sm-3"> <label>First Name</label> <input type="text" name="first_name" value="<?php echo $person['first_name']; ?> " placeholder="Your First Name" class="form-control input-lg"> </div> <div class="col-sm-3"> <label>Last Name</label> <input type="text" name="last_name" value="<?php echo $person['last_name']; ?> " placeholder="Your Last Name" class="form-control input-lg">
<?php require_once $_SERVER['DOCUMENT_ROOT'] . '/bootstrap.php'; // Get form data $validate_fields = ['first_name' => "/\\w+/", 'last_name' => "/\\w+/", 'age' => "/\\d+/"]; foreach ($validate_fields as $key => $pattern) { if (!preg_match($pattern, $_POST[$key])) { redirect_user("/people/new.php", "Whoops. Looks like you forgot to fill in \"{$key}\"!"); } } $first_name = $_POST['first_name']; $last_name = $_POST['last_name']; $age = $_POST['age']; $people_id = \MyClasses\Models\Person::create(compact('first_name', 'last_name', 'age')); // Redirect user redirect_user('/people/show.php?id=' . $people_id, "New user. Hooray.");
<?php require_once $_SERVER['DOCUMENT_ROOT'] . '/bootstrap.php'; \MyClasses\Auth\AuthMaster::redirectIfNotLoggedIn(); $page['title'] = 'People'; echo get_partial('header.php', ['page' => $page]); $wheres = []; $order_bys = []; if (!empty($_GET['older_than'])) { $wheres[] = ['age', '>=', $_GET['older_than']]; } if (!empty($_GET['order_by'])) { $order_bys[] = $_GET['order_by']; } $people = \MyClasses\Models\Person::getAll($wheres, $order_bys); $existing_query_params = $_GET; ?> <div class="row"> <div class="col-sm-4"> <h1>All People</h1> </div> <div class="col-sm-8"> <form action=""> <div class="row" style="padding-top: 40px;"> <div class="col-sm-3 text-right"> <label>Minimum Age: </label> </div> <div class="col-sm-3"> <input type="number" min="18" name="older_than" value="<?php echo !empty($_GET['older_than']) ? $_GET['older_than'] : '';
<?php require_once $_SERVER['DOCUMENT_ROOT'] . '/bootstrap.php'; if (!isset($_POST['id'])) { redirect_user('/people/index.php', 'No person found for ID ... or you didn\'t supply one!'); } $person_id = $_POST['id']; $person = \MyClasses\Models\Person::getOne($person_id); // Get form data $validate_fields = ['first_name' => "/\\w+/", 'last_name' => "/\\w+/", 'age' => "/\\d+/"]; foreach ($validate_fields as $key => $pattern) { if (!preg_match($pattern, $_POST[$key])) { redirect_user("/people/edit.php?id=" . $person_id, "Whoops. Looks like you forgot to fill in \"{$key}\"!"); } } $first_name = $_POST['first_name']; $last_name = $_POST['last_name']; $age = $_POST['age']; // Update User $success = \MyClasses\Models\Person::update($person_id, compact('first_name', 'last_name', 'age')); // Redirect user $success = $success ? "YES" : json_encode($pdo_connection->errorInfo()); redirect_user("/people/edit.php?id=" . $person_id, "Updated... whatever. Success: " . $success);