Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <?php
- session_start();
- if(!isset($_SESSION['username']) || !isset($_SESSION['logged_in'])){
- //User not logged in. Redirect them back to the login.php page.
- header('Location: login.php');
- exit;
- }
- require 'database.php';
- $id = null;
- if ( !empty($_GET['id'])) {
- $id = $_REQUEST['id'];
- }
- if ( null==$id ) {
- header("Location: bookstufftable.php");
- }
- if ( !empty($_POST)) {
- // keep track validation errors
- $ISBN_noError = null;
- $book_titleError = null;
- $author_nameError = null;
- $year_publishedError = null;
- $priceError = null;
- $publisherError = null;
- $genreError = null;
- // keep track post values
- $ISBN_no = $_POST['ISBN_no'];
- $book_title = $_POST['book_title'];
- $author_name = $_POST['author_name'];
- $year_published = $_POST['year_published'];
- $price = $_POST['price'];
- $publisher = $_POST['publisher'];
- $genre = $_POST['genre'];
- // validate input
- $valid = true;
- if (empty($ISBN_no)) {
- $ISBN_noError = 'Please enter an ISBN number';
- $valid = false;
- }
- if (empty($book_title)) {
- $albumError = 'Please enter a book title';
- $valid = false;
- }
- if (empty($author_name)) {
- $weeksError = 'Please enter the authors name';
- $valid = false;
- }
- if (empty($year_published)) {
- $year_publishedError = 'Please enter the year this book was published';
- $valid = false;
- }
- if (empty($price)) {
- $priceError = 'Please enter the price of the book';
- $valid = false;
- }
- if (empty($publisher)) {
- $publisherError = 'Please enter the publishers name';
- $valid = false;
- }
- if (empty($genre)) {
- $genreError = 'Please enter the book genre';
- $valid = false;
- }
- // update data
- if ($valid) {
- $pdo = Database::connect();
- $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- $sql = "UPDATE bookstuff SET ISBN_no = ?, book_title = ?, author_name = ?, year_published = ?, price = ?, publisher = ?, genre = ? WHERE id = ?";
- $q = $pdo->prepare($sql);
- $q->execute(array($ISBN_no,$book_title,$author_name,$year_published,$price,$publisher,$genre,$id));
- Database::disconnect();
- header("Location: bookstufftable.php");
- }
- } else {
- $pdo = Database::connect();
- $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- $sql = "SELECT * FROM bookstuff where id =?";
- $q = $pdo->prepare($sql);
- $q->execute(array($id));
- $data = $q->fetch(PDO::FETCH_ASSOC);
- $ISBN_no = $data['ISBN_no'];
- $book_title = $data['book_title'];
- $author_name = $data['author_name'];
- $year_published = $data['year_published'];
- $price = $data['price'];
- $publisher = $data['publisher'];
- $genre = $data['genre'];
- Database::disconnect();
- }
- ?>
- <head>
- <meta charset="utf-8">
- <link href="css/bootstrap.min.css" rel="stylesheet">
- <script src="js/bootstrap.min.js"></script>
- </head>
- <body>
- <div class="container">
- <div class="span10 offset1">
- <div class="row">
- <h3>Update a book</h3>
- </div>
- <form class="form-horizontal" action="update.php?id=<?php echo $id?>" method="post">
- <div class="control-group <?php echo !empty($ISBN_noError)?'error':'';?>">
- <label class="control-label">ISBN_no</label>
- <div class="controls">
- <input name="ISBN_no" type="text" placeholder="ISBN" value="<?php echo !empty($ISBN_no)?$ISBN_no:'';?>">
- <?php if (!empty($ISBN_noError)): ?>
- <span class="help-inline"><?php echo $ISBN_noError;?></span>
- <?php endif;?>
- </div>
- </div>
- <div class="control-group <?php echo !empty($book_titleError)?'error':'';?>">
- <label class="control-label">Book Title</label>
- <div class="controls">
- <input name="book_title" type="text" placeholder="Book Title" value="<?php echo !empty($book_title)?$book_title:'';?>">
- <?php if (!empty($book_titleError)): ?>
- <span class="help-inline"><?php echo $book_titleError;?></span>
- <?php endif;?>
- </div>
- </div>
- <div class="control-group <?php echo !empty($author_nameError)?'error':'';?>">
- <label class="control-label">Author Name</label>
- <div class="controls">
- <input name="author_name" type="text" placeholder="Author Name" value="<?php echo !empty($author_name)?$author_name:'';?>">
- <?php if (!empty($author_nameError)): ?>
- <span class="help-inline"><?php echo $author_nameError;?></span>
- <?php endif;?>
- </div>
- </div>
- <div class="control-group <?php echo !empty($year_publishedError)?'error':'';?>">
- <label class="control-label">Year Published</label>
- <div class="controls">
- <input name="year_published" type="text" placeholder="Year Published" value="<?php echo !empty($year_published)?$year_published:'';?>">
- <?php if (!empty($year_publishedError)): ?>
- <span class="help-inline"><?php echo $year_publishedError;?></span>
- <?php endif;?>
- </div>
- </div>
- <div class="control-group <?php echo !empty($priceError)?'error':'';?>">
- <label class="control-label">Price</label>
- <div class="controls">
- <input name="price" type="text" placeholder="Price" value="<?php echo !empty($price)?$price:'';?>">
- <?php if (!empty($priceError)): ?>
- <span class="help-inline"><?php echo $priceError;?></span>
- <?php endif;?>
- </div>
- </div>
- <div class="control-group <?php echo !empty($publisherError)?'error':'';?>">
- <label class="control-label">Publisher</label>
- <div class="controls">
- <input name="publisher" type="text" placeholder="Publisher" value="<?php echo !empty($publisher)?$publisher:'';?>">
- <?php if (!empty($publisherError)): ?>
- <span class="help-inline"><?php echo $publisherError;?></span>
- <?php endif;?>
- </div>
- </div>
- <div class="control-group <?php echo !empty($genreError)?'error':'';?>">
- <label class="control-label">Genre</label>
- <div class="controls">
- <input name="genre" type="text" placeholder="Genre" value="<?php echo !empty($genre)?$genre:'';?>">
- <?php if (!empty($genreError)): ?>
- <span class="help-inline"><?php echo $genreError;?></span>
- <?php endif;?>
- </div>
- </div>
- <div class="form-actions">
- <button type="submit" class="btn btn-success">Update</button>
- <a class="btn" href="bookstufftable.php">Back</a>
- </div>
- </form>
- </div>
- </div> <!-- /container -->
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement