<?php function showform($post) { echo '<h1>' . ($post['id'] === '' ? 'New post' : 'Edit "' . $post['title'] . '"') . '</h1>' . PHP_EOL; echo '<hr />' . PHP_EOL; echo '<form class="form-group" method="post" action="" >'; if ($post['id'] !== '') { echo '<input name="id" type="hidden" value="' . $post['id'] . '" />' . PHP_EOL; } $confirmed = ($post['confirmed'] ? ' checked="checked"' : ''); echo <<<EOT <div class="form-group"> <label class="col-sm-3 label-control" for="idcategory">IdCategory:</label> <input class="col-sm-5 form-control" id="idcategory" type="text" name="idcategory" value=" {$post['category_id'] }"/> </div> <div class="form-group"> <label class="col-sm-3 label-control" for="title">Title:</label> <input class="col-sm-5 form-control" id="title" type="text" name="title" value="{$post['title']}"/> </div> <div class="form-group"> <label class="col-sm-3 label-control" for="abstract">Abstract:</label> <input class="col-sm-5 form-control" id="abstract" type="text" name="abstract" value="{$post['abstract']}"/> </div> <div class="form-group"> <label class="col-sm-3 label-control" for="body">Body:</label> <input class=" col-sm-5 form-control" id="body" type="text" name="body" value="{$post['body']}"/> </div> <div class="form-group"> <label class="col-sm-3 label-control" for="confirmed">Confirmed</label> <div class="col-sm-1"> <input{$confirmed} class="form-control" id="confirmed" name="confirmed" type="checkbox" /> </div> </div> <div class="form-group"> <div class="col-sm-offset-3 col-sm-5"> <input class="btn btn-success btn-block" value="save" type="submit" /> </div> </div> EOT; echo '</form>'; }
case 'edit': if (isset($_POST['title'], $_POST['id'], $_POST['id_category'], $_POST['abstract'], $_POST['body'], $_POST['confirmed'])) { $id = dbEscape($_POST['id']); $idcategory = dbEscape($_POST['id_category']); $title = dbEscape($_POST['title']); $abstract = dbEscape($_POST['abstract']); $body = dbEscape($_POST['body']); $confirmed = ($_POST['confirmed'] ? '1' : '0'); dbQuery("UPDATE `posts` SET `id_category`={$idcategory},`title`={$title},`abstract`={$abstract},`body`={$body},`confirmed`={$confirmed} WHERE (`id`={$id});"); redirect(basename(__FILE__)); } else { $id = encode($_GET['id']); $posts = dbArrayQuery("SELECT * FROM `posts` WHERE `id`={$id}"); $post = $posts[0]; showform($post); } break;