ΔΗΜΙΟΥΡΓΙΑ WEB ΕΦΑΡΜΟΓΗΣ ΜΕ PHP & MYSQL: phpLibrary

ΟΔΗΓΙΕΣ

  1. Δημιουργήστε τα αρχεία php που περιγράφονται παρακάτω
  2. Δημιουργήστε μια βάση με το όνομα “Library”
  3. Εισάγετε το αρχείο “library.sql” μέσα στην βάση “Library”
  4. Τρέξτε τα αρχεία από το folder του WAMP ή XAMPP

1. Δημιουργούμε την σελίδα db.php:

<?php  
$conn = mysqli_connect('localhost', 'root', '', 'library');

if(!$conn)
{
  die('Could not connect: ' . mysqli_error());
}
?>

2.Δημιουργούμε την σελίδα delete.php:

<?php
include("db.php");  

$id = $_REQUEST['BookID'];

// sending query
mysqli_query($conn, "DELETE FROM books WHERE BookID = '$id'")
             or die(mysqli_error());  	

header("Location: index.php");
?>

3.Εισάγουμε το παρακάτω script SQL δημιουργώντας την βάση δεδομένων example.sql:

-- phpMyAdmin SQL Dump
-- version 4.1.12
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1
-- Generation Time: Aug 25, 2014 at 01:38 PM
-- Server version: 5.6.16
-- PHP Version: 5.5.11

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- Database: `Library`
--

-- --------------------------------------------------------

--
-- Table structure for table `books`
--

CREATE TABLE IF NOT EXISTS `books` (
  `BookID` int(11) NOT NULL AUTO_INCREMENT,
  `Title` varchar(150) NOT NULL,
  `Author` varchar(150) NOT NULL,
  `PublisherName` varchar(150) NOT NULL,
  `CopyrightYear` year(4) NOT NULL,
  PRIMARY KEY (`BookID`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=38 ;

--
-- Dumping data for table `example`
--

INSERT INTO `books` (`BookID`, `Title`, `Author`, `PublisherName`, `CopyrightYear`) VALUES
(34, 'Java Developer', 'Aegis', 'Manoj', 2014),
(36, 'Java 1+', 'eee', 'ertret', 2010),
(37, 'Ab', 'hh', 'hhhn', 2014);

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

4. Δημιουργούμε την σελίδα index.php:

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>phpLibrary</title>
</head>

<body>

<h1>phpLibrary v1.0</h1>

<form method="post">
<table>
<tr>
    <td>Title:</td>
    <td><input type="text" name="title" class="form-control"/></td>
</tr>
<tr>
    <td>Author</td>
    <td><input type="text" name="author" class="form-control"/></td>
</tr>
<tr>
    <td>Publisher Name</td>
    <td><input type="text" name="name" class="form-control"/></td>
</tr>
<tr>
    <td>Copyright Year</td>
    <td><input type="text" name="copy" class="form-control"/></td>
</tr>
<tr>
    <td>&nbsp;</td>
    <td><input type="submit" name="submit" value="add" class="btn btn-success btn-lg"/></td>
</tr>
</table>

<br /><br />

<?php
if (isset($_POST['submit']))
{	   
    include 'db.php';

    $title  = $_POST['title'];
    $author = $_POST['author'];					
    $name   = $_POST['name'];
    $copy   = $_POST['copy'];

    mysqli_query($conn,"INSERT INTO `books`(Title, Author, PublisherName, CopyrightYear) 
                        VALUES ('$title', '$author', '$name', '$copy')"); 
}
?>
</form>

<table border="1">
    <?php
    include("db.php");
 
    $result = mysqli_query($conn, "SELECT * FROM books");

    while($row = mysqli_fetch_array($result))
    {
        $id = $row['BookID'];	

        echo "<tr>";	
        echo"<td>" . $row['BookID'] . "</td>";
        echo"<td>" . $row['Title'] . "</td>";
        echo"<td>" . $row['Author'] . "</td>";
        echo"<td>" . $row['PublisherName'] . "</td>";
        echo"<td>" . $row['CopyrightYear'] . "</td>";	

        echo"<td> <a href ='view.php?BookID=$id'>Edit</a>";
        echo"<td> <a href ='delete.php?BookID=$id'><center>Delete</center></a>";
        echo "</tr>";
    }

    mysqli_close($conn);
    ?>
</table>

<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>

</body>
</html>

5.Δημιουργούμε την σελίδα view.php:

<?php
require("db.php");
$id = $_REQUEST['BookID'];

$result = mysqli_query($conn,"SELECT * FROM books WHERE BookID  = '$id'");
$row    = mysqli_fetch_array($result);

if (!$result) 
{
    die("Error: Data not found..");
}

$title = $row['Title'];
$author = $row['Author'];					
$publisherName = $row['PublisherName'];
$copyrightYear = $row['CopyrightYear'];

if(isset($_POST['save']))
{	
    $title_save = $_POST['title'];
    $author_save = $_POST['author'];
    $name_save = $_POST['name'];
    $copy_save = $_POST['copy'];

    mysqli_query($conn,"UPDATE books SET Title ='$title_save', 
                                           Author ='$author_save',
                                           PublisherName ='$name_save',
                                           CopyrightYear ='$copy_save' 
                        WHERE BookID = '$id'")
    or die(mysql_error()); 

    echo "Saved!";

    header("Location: index.php");			
}

mysqli_close($conn);
?>

<!DOCTYPE html>
<html>
<head>
    <title>phpLibrary</title>
</head>

<body>
<form method="post">
<table>
<tr>
    <td>Title:</td>
    <td><input type="text" name="title" value="<?php echo $title ?>"/></td>
</tr>
<tr>
    <td>Author</td>
    <td><input type="text" name="author" value="<?php echo $author ?>"/></td>
</tr>
<tr>
    <td>Publisher Name</td>
    <td><input type="text" name="name" value="<?php echo $publisherName ?>"/></td>
</tr>
<tr>
    <td>Copyright Year</td>
    <td><input type="text" name="copy" value="<?php echo $copyrightYear ?>"/></td>
</tr>
<tr>
    <td>&nbsp;</td>
    <td><input type="submit" name="save" value="save" /></td>
</tr>
</table>

</body>
</html>