# 登入/登出/CRUD 程式碼
###### tags: `puli_foodmap`
## 登入/登出參考資料
[簡易版CRUD](https://ithelp.ithome.com.tw/articles/10206254)
[PHP登入與登出-1](https://ithelp.ithome.com.tw/m/articles/10206716)
[PHP登入與登出-2(含登入頁面)](https://dreamtails.pixnet.net/blog/post/23583385)
[PHP登入與登出-3(session)](https://tw511.com/a/01/4449.html)
[後端基礎](https://yakimhsu.com/project/project_w9_PHP_SQL.html)
## Postman
[會員註冊及登入系統API-測試](https://ithelp.ithome.com.tw/articles/10226798)
[postman測試](https://ithelp.ithome.com.tw/articles/10201503)
記得是在<font color="#f000">**body內的form-data**</font>輸入你的key & value
## 管理員結構 (puli_manager)

## 餐廳結構 (puli_restaurant)

## 餐廳推薦類型結構 (puli_recommend)

## 餐廳營業時間結構 (puli_rest_time)

## 程式碼
### create.php
``` php=
<!-- 加入(註冊)會員 - 「新增」會員資料進MySQL資料庫 (register_finish.php) -->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
include("login.php");
// 取得使用者想新增的資料餐廳資料
@$RestaurantName = $_POST['RestaurantName'];
@$RestaurantTEL = $_POST['RestaurantTEL'];
@$RestaurantIntro = $_POST['RestaurantIntro'];
@$RestaurantTime = $_POST['RestaurantTime'];
@$RestaurantPhoto = $_POST['RestaurantPhoto'];
@$RestaurantComment = $_POST['RestaurantComment'];
@$RestaurantPrice = $_POST['RestaurantPrice'];
@$RestaurantAddress = $_POST['RestaurantAddress'];
@$RestaurantX = $_POST['RestaurantX'];
@$RestaurantY = $_POST['RestaurantY'];
echo $_SESSION['userLogin'];
// 抓那一欄資料
// 身分驗證
// if ($_SESSION['user_login'] == $row['user_login']) {
if (isset($_SESSION['userLogin'])) {
$sql_create = "INSERT INTO puli_restaurant
( Restaurant_name, Restaurant_TEL, Restaurant_intro, Restaurant_time, Restaurant_photo, Restaurant_comment, Restaurant_price, Restaurant_address, Restaurant_x, Restaurant_y)
VALUES
( '$RestaurantName', '$RestaurantTEL', '$RestaurantIntro', '$RestaurantTime', '$RestaurantPhoto', '$RestaurantComment', '$RestaurantPrice', '$RestaurantAddress', '$RestaurantX', '$RestaurantY')";
// mysqli_query($link, $sql_create);
if(mysqli_query($link, $sql_create)){
echo " " . $RestaurantName . " " ;
echo '新增成功!';
// echo '<meta http-equiv=REFRESH CONTENT=2;url=member.php>';
}
else{
echo '新增失敗!';
// echo '<meta http-equiv=REFRESH CONTENT=2;url=member.php>';
}
}
else {
echo '您無權限觀看此頁面!';
}
?>
```
### delete.php
``` php=
<!-- php session_start(); -->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
include("login.php"); // 連接login.php 以核對權限
// include("read.php"); // 取得餐廳資料 (以便刪除餐廳資料用)
// 取得使用者想刪除的餐廳名字
@$RestaurantID = $_GET['RestaurantID'];
// @$RestaurantID = $_POST['RestaurantID'];
// 搜尋資料庫資料
$sql = "SELECT * FROM puli_restaurant Where Restaurant_ID = '$RestaurantID'";
// 執行查詢 mysqli_query(連接要使用的MySQL, 要查詢的資料)
$result = mysqli_query($link, $sql);
echo $_SESSION['userLogin'];
// 核對權限
if(isset($_SESSION['userLogin']))
{
while($row = mysqli_fetch_assoc($result)){
// 刪除資料庫資料
// 餐廳資料不為null且有資料
if($RestaurantID != null && $row['Restaurant_ID'] == $RestaurantID)
{
// $RestaurantID = $row['Restaurant_ID'];
$sql_delete = "DELETE FROM puli_restaurant WHERE Restaurant_ID = $RestaurantID";
mysqli_query($link,$sql_delete);
echo " " . $row['Restaurant_ID']. " "
. $row['Restaurant_name']. " "
. '刪除成功!';
// echo '<meta http-equiv=REFRESH CONTENT=2;url=member.php>';
}
else
{
echo '刪除失敗!';
// echo '<meta http-equiv=REFRESH CONTENT=2;url=member.php>';
}
}
}
else
{
echo '您無權限觀看此頁面!';
// echo '<meta http-equiv=REFRESH CONTENT=2;url=index.php>';
}
?>
```
### read.php
``` php=
<!-- fetch data 連接 puli_restaurant 取得資料 -->
<?php
// db_host, db_username, db_password, db_name
$link = mysqli_connect("localhost","root","","wordpress");
if ($link == false) {
die("連接失敗: " .mysqli_connect_error());
}
// $sql 加入sql語法 從 user 的資料表中選擇所有欄位
$sql = "SELECT * FROM puli_restaurant";
// 以下程式是將DB中的資料印出來
// $result 從DB中取出結果集
// -> 取得 $link 中的 query($sql)
$result = $link->query($sql);
// $row = mysqli_fetch_assoc($result);
$arr_data = [];
if ($result -> num_rows > 0) {
// 輸出數據
// echo "id: "." "."Name: "." "."username: ". "<br>";
// -> 引用一個class的屬性和方法
while($row = $result->fetch_assoc()) {
$restaurant = array
(
" RestaurantID " => $row["Restaurant_ID"],
" RestaurantName " => $row["Restaurant_name"],
" RestaurantTEL " => $row["Restaurant_TEL"],
" RestaurantIntro " => $row["Restaurant_intro"],
" RestaurantTime " => $row["Restaurant_time"],
" RestaurantPhoto " => $row["Restaurant_photo"],
" RestaurantComment " => $row["Restaurant_comment"],
" RestaurantPrice " => $row["Restaurant_price"],
" RestaurantAddress " => $row["Restaurant_address"],
" RestaurantX " => $row["Restaurant_x"],
" RestaurantY " => $row["Restaurant_y"]
);
array_push($arr_data,$restaurant);
}
} else {
echo "0 結果";
}
print_r($arr_data);
mysqli_query($link, "SET sNAMES 'utf8'"); //設定資料庫編碼 utf8
// $link->close();
?>
```
### login.php
``` php=
<?php session_start(); ?>
<!--上方語法為啟用session,此語法要放在網頁最前方-->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
//連接資料庫
//只要此頁面上有用到連接MySQL就要include connect.php
include("connect.php");
// 取得使用者輸入的帳號、密碼
// @$userLogin = $_POST['userLogin'];
// @$userPass = $_POST['userPass'];
@$userLogin = $_GET['userLogin'];
@$userPass = $_GET['userPass'];
// 搜尋資料庫資料
$sql = "SELECT * FROM puli_manager Where user_login = '$userLogin'";
// 執行查詢 mysqli_query(連接要使用的MySQL, 要查詢的資料)
$result = mysqli_query($link, $sql);
// if($rows){
while($row = mysqli_fetch_assoc($result)){
// 登入成功
// 判斷帳號與密碼是否為空白 以及確認是否為MySQL資料庫裡是否有這個會員
if($userLogin != null && $userPass != null && $row['user_login'] == $userLogin && $row['user_pass'] == $userPass)
{
// 將帳號寫入session,方便驗證使用者身份
// session 內儲存 user-login(帳號)
$_SESSION['userLogin'] = @$userLogin;
// 將user_status改成1
$sql_online = "UPDATE puli_manager SET user_status = 1 Where user_login = '$userLogin'";
mysqli_query($link,$sql_online);
echo $row["user_login"];
// $row["userPass"];
echo '登入成功!';
// echo '<meta http-equiv=REFRESH CONTENT=1;url=member.php>';
}
else // 登入失敗
{
echo $row["user_login"].
$row["user_pass"];
echo '登入失敗!';
// echo '<meta http-equiv=REFRESH CONTENT=1;url=index.php>';
}
}
?>
```
### logout.php
``` php=
<!-- 登出 - 洗掉登入使用者之session(logout.php) -->
<!-- -->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
include('connect.php');
include('login.php');
// 如果網頁關掉 , 1:連線由使用者或是網路終止
echo $_SESSION['userLogin'];
$userLogin = $_SESSION['userLogin'];
// 把狀態改成不再線上
// 將user_status改成0
$sql_offline = "UPDATE puli_manager SET user_status = 0 Where user_login = '$userLogin'";
mysqli_query($link,$sql_offline);
//將session清空
unset($_SESSION['userLogin']);
echo '登出中......';
// echo '<meta http-equiv=REFRESH CONTENT=1;url=index.php>';
?>
```
### connect.php
``` php=
<!-- fetch data 連接資料庫 取得資料 -->
<?php
// db_host, db_username, db_password, db_name
$link = mysqli_connect("localhost","root","","wordpress");
if ($link == false) {
die("連接失敗: " .mysqli_connect_error());
}
// $sql 加入sql語法 從 user 的資料表中選擇所有欄位
$sql = "SELECT * FROM `puli_manager`";
// 以下程式是將DB中的資料印出來
// $result 從DB中取出結果集
$result = $link->query($sql);
$row = mysqli_fetch_assoc($result);
// if ($result->num_rows >= 0) {
// // 輸出數據
// // echo "id: "." "."Name: "." "."username: ". "<br>";
// while($row = $result->fetch_assoc()) {
// // id name username password
// echo " id: " . $row["ID"].
// " 帳號: " . $row["user_login"].
// " 密碼: " . $row["user_pass"].
// " 姓名: " . $row["user_nicename"].
// " 信箱: " . $row["user_email"].
// " userUrl: " . $row["user_url"].
// " userRegisterd: " . $row["user_registered"].
// " userActivationKey: " . $row["user_activation_key"].
// " userStatus: " . $row["user_status"].
// " displayName: " . $row["display_name"]. "<br>";
// }
// } else {
// echo "0 結果";
// }
mysqli_query($link, "SET NAMES 'utf8'"); //設定資料庫編碼 utf8
// $link->close();
?>
```
### update.php
``` php=
<!-- php session_start(); -->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
include("login.php");
@$RestaurantID = $_GET['RestaurantID'];
// @$RestaurantID = $_POST['RestaurantID'];
@$key = $_GET['key'];
@$val = $_GET['val'];
echo $_SESSION['userLogin'];
//紅色字體為判斷密碼是否填寫正確
if(isset($_SESSION['userLogin']))
{
//更新資料庫資料語法
$sql_update = "UPDATE puli_restaurant SET
$key = '$val'
WHERE Restaurant_ID = '$RestaurantID'";
if(mysqli_query($link, $sql_update))
{
echo '修改成功!';
// echo '<meta http-equiv=REFRESH CONTENT=2;url=member.php>';
}
else
{
echo '修改失敗!';
// echo '<meta http-equiv=REFRESH CONTENT=2;url=member.php>';
}
}
else
{
echo '您無權限觀看此頁面!';
// echo '<meta http-equiv=REFRESH CONTENT=2;url=index.php>';
}
?>
```