PHP: Buổi 1. PHP Project1


Khóa học qua video:
Lập trình Python All Lập trình C# All SQL Server All Lập trình C All Java PHP HTML5-CSS3-JavaScript
Đăng ký Hội viên
Tất cả các video dành cho hội viên

File index.php:

<?php
  session_start();
  $connect=new MySQLi('localhost','root','','db_c2005l');
  mysqli_set_charset($connect,'UTF8');
?>
<!DOCTYPE html>
<html>
<head>
  <title>include</title>
  <meta charset="utf-8">
  <style>
    nav>a{display: block;width: 16.666667%;float: left;text-align: center;border: green thin solid;box-sizing: border-box;}
    article{border: green thin solid;min-height: 300px}
  </style>
</head>
<body>
<section>
  <header></header>
  <nav><a href="?option=home">Home</a><a href="?option=news">News</a><a href="?option=feedback">Feedback</a><a href="?option=cart">Cart</a>
    <?php if(!isset($_SESSION['usercustomer'])):?>
      <a href="?option=signup">Sign up</a><a href="?option=signin">Sign in</a>
    <?php else:?>
      <section style="border:green thin solid">Hello: <?=$_SESSION['usercustomer']?> [<a href="?option=signout">Signout</a>]</section>
    <?php endif?>
  </nav>
  <article>
    <?php
//      echo md5('123456');
      if(isset($_GET['option'])){
        switch($_GET['option']){
          case'home':
            include"home.php";
            break;
          case'news':
            include"news.php";
            break;
          case'feedback':
            include"feedback.php";
            break;
          case'cart':
            include"cart.php";
            break;
          case'signup':
            include"signup.php";
            break;
          case'signin':
            include"signin.php";
            break;
          case'signout':
            unset($_SESSION['usercustomer']);
            header('location: .');
            break;
          // default:
          //   include"home.php";
          //   break;
        }
      }else{
        include"home.php";
      }
    ?>
  </article>
</section>
</body>
</html>

File signin.php:

<?php
  if(isset($_POST['username'])){
    $username=$_POST['username'];
    $password=md5($_POST['password']);
    $query="select*from customer where username='$username' and password='$password'";
    $result=$connect->query($query);
    if(mysqli_num_rows($result)==0){
      $alert="Tên đăng nhập hoặc mật khẩu không đúng!";
    }else{
      $_SESSION['usercustomer']=$username;
      echo"<script>location='?option=home';</script>";
    }
  }
?>
<h1>ĐĂNG NHẬP TÀI KHOẢN</h1>
<section>
  <?php if(isset($alert)) echo $alert;?>
</section>
<section>
  <form method="post">
    <section>
      <label>Tên đăng nhập: </label><input type="text" name="username" required>
    </section>
    <section>
      <label>Mật khẩu: </label><input type="password" name="password" required>
    </section>
    <section><input type="submit" value="Đăng nhập"></section>
  </form>
</section>

File home.php:

<?php
  $query="select*from customer";
  $result=$connect->query("select*from customer limit 0,10");
  if(mysqli_num_rows($result)==1){
    $result=mysqli_fetch_array($result);
    echo"<br>".$result['name'];
  }else{
    //Hiển thị tên của các khách hàng:
   foreach($result as $rs){
     echo"<br>".$rs['name'];
   }
  }
?>

File db_c2005l.sql:

-- phpMyAdmin SQL Dump
-- version 4.9.0.1
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Dec 10, 2020 at 02:38 PM
-- Server version: 10.3.16-MariaDB
-- PHP Version: 7.3.6

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: `db_c2005l`
--

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

--
-- Table structure for table `customer`
--

CREATE TABLE `customer` (
  `id` int(11) NOT NULL,
  `name` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
  `username` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
  `password` varchar(32) COLLATE utf8_unicode_ci NOT NULL,
  `mobile` varchar(11) COLLATE utf8_unicode_ci NOT NULL,
  `address` varchar(99) COLLATE utf8_unicode_ci NOT NULL,
  `email` varchar(40) COLLATE utf8_unicode_ci NOT NULL,
  `status` tinyint(1) NOT NULL DEFAULT 1
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

--
-- Dumping data for table `customer`
--

INSERT INTO `customer` (`id`, `name`, `username`, `password`, `mobile`, `address`, `email`, `status`) VALUES
(1, 'ABCD', 'abcd', 'e10adc3949ba59abbe56e057f20f883e', '0987654321', 'Hà Nội', 'abcd@gmail.com', 1),
(2, 'ÈGH', '', '243434', '1234567890', '', '', 1);

--
-- Indexes for dumped tables
--

--
-- Indexes for table `customer`
--
ALTER TABLE `customer`
  ADD PRIMARY KEY (`id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `customer`
--
ALTER TABLE `customer`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3;
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 */;
« Trước: $_GET và $_POST
Khóa học qua video:
Lập trình Python All Lập trình C# All SQL Server All Lập trình C All Java PHP HTML5-CSS3-JavaScript
Đăng ký Hội viên
Tất cả các video dành cho hội viên
Copied !!!