Menu

11/28/21

Quan li loai (crud_database)

 


Cấu trúc thư mục:

Css\style.css

action_add.php

action_delete.php

action_update.php

index.php

add.php

delete.php

update.php

pdo.php

File loai.sql

-- phpMyAdmin SQL Dump
-- version 4.8.3
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1:3306
-- Generation Time: Nov 28, 2021 at 04:41 AM
-- Server version: 5.7.23
-- PHP Version: 7.2.10

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
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 utf8mb4 */;

--
-- Database: `bookstorevn`
--

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

--
-- Table structure for table `loai`
--

DROP TABLE IF EXISTS `loai`;
CREATE TABLE IF NOT EXISTS `loai` (
  `maloai` varchar(5) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `tenloai` varchar(50) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`maloai`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `loai`
--

INSERT INTO `loai` (`maloai`, `tenloai`) VALUES
('gk', 'GK'),
('kt', 'Kinh Tế'),
('nn', 'Ngoại Ngữ'),
('pl', 'Pháp Luật'),
('SK', 'SUC KHOE'),
('td', 'Từ Điển'),
('test', 'Loai Moi'),
('th', 'tin hocc'),
('to', 'toan hocs'),
('tt', 'The Thao Du Lich'),
('vh', 'Văn Học'),
('vhxh', 'Van Hoa xa Hoi');
COMMIT;

/*!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 */;

File style.css
		body {
			font:30px arial;
			margin: 30px;
			padding: 10px;
			background: #959393;
		}
		#wrapper{ 
			border: 3px solid black;
			background-color:#9e9e9e;
			height: 500px;
		
		}
		#header{
			background-color:#66917a;
			text-align:center;
			padding: 8px;
			font-size:30px;
		}
		#header h1{
			text-transform: uppercase;
			font-size:30px;
			font-style:italic;
		}
		#menu{
			background-color: blue;
		}
		#menu ul{
			padding: 20px;
			margin: 0px;
			font-weight: bold;
			text-transform: uppercase;

		}
		#menu ul li{
		display: inline-block;
		
		}
		#menu ul li a{
			color: white;
			font-size: 20px;
			padding:8px 10px;
			list-style:none;
			text-decoration: none;
			display: block; 
			transition: all 2s ease;
		}
		#menu ul li a:hover{
			background-color: #4c625678;
		}
		#main-content{

			border: 1px solid yellow;
			min-height: 100px;
			padding: 30px;
			background-color: #efefef;
		}
		#main-content h2{
			text-transform: capitalize;
			padding: 10px;
			font-size: 100px;
		}
		#main-content table{
	
			width: 80%;
    		border: 1px solid black;
    		height: auto;
			
		}
		#main-content th{
			border: 1px solid black;
			background-color: #788f8f;
			text-transform: uppercase;
		}
		#main-content td{
			border: 1px solid black;    
			text-align: center;
			background-color: #e7e7e2;
		}

		#main-content td a{
			font-size: 20px;
			color: white;
			padding: 5px;  
			text-align: center;
			background-color: #e17e21;
			text-transform: uppercase;
			margin: 5px;

		}
		#main-content td a:nth-child(2){
			background-color: red;
		}
		#main-content td a:nth-child(3){
			background-color: #607d8b;
		}

		#main-content .post-form {
			width: 40%;	   
			margin: 0 auto; 
			padding: 30px;
			background-color: #abafafd4;
			border-radius: 10px;
		}
		#main-content .post-form .form-group{
			margin:  0 0 15px;
		}
		#main-content .post-form .form-group label{
				font-size: 30px;
			    display:block;
    			margin: 0 0 12px;
		}
		#main-content .post-form .form-group input{
			font-size:  30px;
		}
		#main-content .post-form .form-group .select{
			font-size: 30px;
		}
		#main-content .post-form .submit{

				    background-color: black;
				    color: white;
				    font-size: 25px;
				    margin-left: 30%;
				    border-radius: 9px;
				    padding: 10px;
		}
File action_add.php

<?php
 
    $o = new PDO('mysql:host=localhost;dbname=bookstorevn','root','');
    $o->query('set names utf8');
    $sql='insert into loai(maloai, tenloai) value(?,?)';
    $stm=$o->prepare($sql);
    $arr=[$_POST['maloai'], $_POST['tenloai']];
    $stm->execute($arr);
    header('location:index.php');
?>

File action_delete.php

<?php
    $o = new PDO('mysql:host=localhost;dbname=bookstorevn','root','');
    $o->query('set names utf8');
    $sql='delete from loai where maloai=?';
    $stm=$o->prepare($sql);
    $arr=[$_POST['maloai']];
    $stm->execute($arr);
    header('location:index.php');
?>

File action_update.php

<?php
    $o = new PDO('mysql:host=localhost;dbname=bookstorevn','root','');
    $o->query('set names utf8');
    $sql='update loai set tenloai=? where maloai=?';
    $stm=$o->prepare($sql);
    $arr=[$_POST['tenloai'], $_POST['maloai']];
    $stm->execute($arr);
    header('location:index.php');
?>

File add.php

<!DOCTYPE html>
<html>
<head>
<title>Add</title>
	<link rel="stylesheet" type="text/css" href="css/style.css">
	<?php include "pdo.php" ; ?>
</head>
<body>
		<div id="header">
			<h1>CRUD</h1>

		</div>
        <div id="menu">
            <ul>
                <li>
                    <a href="index.php">Home</a>
                </li>
                <li>
                    <a href="add.php">Add</a>
                </li>
                <li>
                    <a href="update.php">Update</a>
                </li>
                <li>
                    <a href="delete.php">Delete</a>
                </li>
            </ul>
        </div>
		<div id="main-content">
				<h2>add new record</h2>
		<form class="post-form" method="post" action="action_add.php">
				<div class="form-group">
					<label>Ma Loai</label>
					<input type="text" name="maloai">
				</div>
				<div class="form-group">
					<label>Ten Loai </label>
					<input type="text" name="tenloai" >
				</div>
				
				<div class="form-group">
					<label>Ten Loai Trong DB</label>
					<select class="select">
						<option selected disabled>xem loai</option>
						<option value="1">A</option>
						            <?php 

			            foreach ($data as $key=>$value){
			                 ?>                 
			                                
			                 <option value=""> <?php echo $value["tenloai"]; ?></option>
			                       
			                    
			                 <?php

			            }
			            ?>
					</select>
				</div>
				<input class="submit" type="submit" value="Them" />

		</form>

	</div>

</body>
</html>

File delete.php

<!DOCTYPE html>
<html>
<head>
<title>delete</title>
	<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
		<div id="header">
			<h1>CRUD</h1>

		</div>
        <div id="menu">
            <ul>
                <li>
                    <a href="index.php">Home</a>
                </li>
                <li>
                    <a href="add.php">Add</a>
                </li>
                <li>
                    <a href="update.php">Update</a>
                </li>
                <li>
                    <a href="delete.php">Delete</a>
                </li>
            </ul>
        </div>
		<div id="main-content">
				<h2>delete record</h2>
		<form class="post-form" method="post" action="action_delete.php">
				<div class="form-group">
					<label>Id</label>
					<input type="text" name="maloai">
				</div>
				<input class="submit" type="submit" value="delete">
		</form>

	</div>

</body>
</html>

File index.php

<!DOCTYPE html>
<html>
<head>
	<title>Page Title</title>
	<link rel="stylesheet"  href="css/style.css">
    <?php include "pdo.php"; ?>
</head>
<body>
		<div id="header">
			<h1>CRUD</h1>

		</div>
        <div id="menu">
            <ul>
                <li>
                    <a href="index.php">Home</a>
                </li>
                <li>
                    <a href="add.php">Add</a>
                </li>
                <li>
                    <a href="update.php">Update</a>
                </li>
                <li>
                    <a href="delete.php">Delete</a>
                </li>
            </ul>
        </div>
		<div id="main-content">
				<h2>all record</h2>
				 <table cellpadding="7px">
        <thead>
        <th>stt</th>
        <th>ma loai</th>
        <th>ten loai</th>
        <th>Action</th>
        </thead>
        <tbody>
     
            <?php 

            foreach ($data as $key=>$value){
                 ?>                 
                    <tr>
                        <td><?php echo $key+1 ?></td>
                        <td><?php echo $value["maloai"]; ?></td>
                        <td><?php echo $value["tenloai"]; ?></td>
                        <td> 
                            <a href="add.php?maloai=<?php echo $value['maloai']; ?>">
                            Add
                            </a>
                            <a href="delete.php?maloai=<?php echo $value['maloai']; ?>">
                            Delete
                            </a>
                             <a href="update.php?maloai=<?php echo $value['maloai']; ?>">
                            Edit
                            </a>
                        </td>
                    </tr>
                 <?php

            }
            ?>
            
        </tbody>
    </table>
		</div>



</body>
</html>

File pdo.php

    <?php
    $o = new PDO('mysql:host=localhost;dbname=bookstorevn','root','');
    $o->query('set names utf8');
    $sql='select * from loai';
    $stm=$o->query($sql);
    $data=$stm->fetchAll(PDO::FETCH_ASSOC);
    ?>
File update
<!DOCTYPE html>
<html>
<head>
<title>update</title>
	<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
		<div id="header">
			<h1>CRUD</h1>

		</div>
        <div id="menu">
            <ul>
                <li>
                    <a href="index.php">Home</a>
                </li>
                <li>
                    <a href="add.php">Add</a>
                </li>
                <li>
                    <a href="update.php">Update</a>
                </li>
                <li>
                    <a href="delete.php">Delete</a>
                </li>
            </ul>
        </div>
		<div id="main-content">
				<h2>edit record</h2>
		<form class="post-form" method="post" action="action_update.php">
				<div class="form-group">
					<label>Ma Loai</label>
					<input type="text" name="maloai">
				</div>
				
				<div class="form-group">
					<label>Ten Loai </label>
					<input type="text" name="tenloai" >
				</div>
				
				<input class="submit" type="submit" value="Save" />

		</form>

	</div>

</body>
</html>

No comments:

Post a Comment