---
# System prepended metadata

title: '**1212 蕭幫PHP教學**'
tags: [PHP]

---

###### tags: `PHP`

# **1212 蕭幫PHP教學**


基本表單結構 form.php 【POST】
===

```html=
#POST是傳出資料的方式，另有較為常用的GET
#action則是將資料送到該檔案，由該檔案進行後續處理
<form method="POST" action="main.php">
<div class="form-group">
#name代表的是該資料的欄位的名稱
<input name="text" type="text" class="form-control form-control-lg" placeholder="請輸入文字">
</div>

<div>
#type必須為submit，才能送出表單內的資料
<button type="submit" class="btn btn-primary btn-lg btn-block">送出</button>
</div>
#所有需要送出的資料都必須包含在form內
</form>
```


在前端輸入完資料後，以POST的方式傳至後端的接取方式如下
    
```php=
<?php
    $text = $_POST['text'];    #POST傳來的資料會暫存在$_POST['name']
    echo $text;                #echo 是PHP中最直接的輸出方式
?>
```


基本表單結購 form.php 【GET】
===

```html=
<div class="main position-relative container col-lg-3 border ">
<!-- 表單 -->
<form method="GET" action="main.php">
    <div class="form-group">
        <input name="text" type="text" class="form-control form-control-lg" placeholder="請輸入文字">
    </div>

    <div>
        <button type="submit" class="btn btn-primary btn-lg btn-block">送出</button>
    </div>
</form>

</div>
```
GET是帶著資料到後端，明顯的區別便是GET所攜帶的資料會顯示在網址
```php=
<?php
    $text = $_GET['text'];    #GET傳來的資料會暫存在$_GET['name']
    echo $text;   
    #攜帶的資料會出現在?之後
    //http://127.0.0.1/PHPclass/practice/main.php?text=1123
?>
```

資料庫
===

- 資料庫名稱 phpclass
    - 表單 user
        - ID    	int(5)  		AUTO_INCREMENT
        - account	varchar(25)	    utf8_general_ci	
        - name	    varchar(5)	
        - nickname	varchar(10)
        - sex	    char(1)	
        - birthday	date	
        - mail	    varchar(25)
        - address	varchar(50)
          
    - 表單 detail
        - account	     
        - password		varchar(18)
        - uni		    varchar(255)
        - active		varchar(2)
        - time		    date


- 資料庫名稱　test
    - 表單 table
        - S1       varchar(255)
        - S2       varchar(255)
        - S3       varchar(255)
        - S4       varchar(255)
        - S5       varchar(255)

# 連接資料庫 db_connect.php
將連接資料庫的語法寫成一個function，方便調用
```php=
<?

session_start();
function filterTable($query)
	{
		$dbType   = 'MySQL';
		$host     = 'localhost';
		$dbName   = 'phpclass';
		$userName = 'root';
		$pwd      = '0000';
		
		$dbh = mysqli_connect($host, $userName, $pwd, $dbName) or die("Error " . mysqli_error($dbh));
		//$dbh = mysqli_connect($host, $userName, $pwd, $dbName);
	    mysqli_query($dbh, "SET NAMES 'utf8'");
	    $filter_Result = mysqli_query($dbh, $query);
	    return $filter_Result;
	}

	function _fetch_array($sql) {
	// mysqli_connect('localhost','root','','darkcalamity');
	return mysqli_fetch_array(filterTable($sql));
	}


	function _alert($str,$path){
		echo "<script> alert(\"$str\");parent.location.href='$path';</script>";
	}


?>
}
```

資料庫語法
===
 
- CRUD 資料庫四大功能
    - C → Creat 新增
        - INSERT INTO `表單名`(`欄位名`) VALUES('".$data."') WHERE 條件式
    - R → Read 讀取
        - SELECT `欄位名` FROM `表單名` WHERE 條件式
    - U → Update 更新
        - UPDATE `表單名` SET `欄位名` = `$data` WHERE 條件式
    - D → Delete 刪除
        - DELETE FROM `表單名` WHERE 條件式

Query練習 practice.php
===
    
- HTML
```html= 
 <form method="POST" action="main.php">
          <div class="form-group">
              <input name="text1" type="text" class="form-control form-control-lg" placeholder="請輸入text1">
          </div>
          <div class="form-group">
              <input name="text2" type="text" class="form-control form-control-lg" placeholder="請輸入text2">
          </div>
          <div class="form-group">
              <input name="text3" type="text" class="form-control form-control-lg" placeholder="請輸入text3">
          </div>
          <div class="form-group">
              <input name="text4" type="text" class="form-control form-control-lg" placeholder="請輸入text4">
          </div>
          <div class="form-group">
              <input name="text5" type="text" class="form-control form-control-lg" placeholder="請輸入text5">
          </div>

          <div>
            <button type="submit" class="btn btn-primary btn-lg btn-block">送出</button>
          </div>
        </form>

```
- INSERT INTO `表單名`(`欄位名`) VALUES('".$data."') WHERE 條件式
```php=
      <?php

include "db_connect.php";


if(isset($_POST['new'])){
	$text1 = $_POST['text1'];
	$text2 = $_POST['text2'];
	$text3 = $_POST['text3'];
	$text4 = $_POST['text4'];
	$text5 = $_POST['text5'];

	$sql = "INSERT INTO `wow`(`s1`,`s2`,`s3`,`s4`,`s5`) VALUES('".$text1."','".$text2."','".$text3."','".$text4."','".$text5."')";
	filterTable($sql);
	}

?>
```

-  SELECT `欄位名` FROM `表單名` WHERE 條件式
```php=

$sql1 = "SELECT * FROM `wow` WHERE `s1` = '1' ";
$result =filterTable($sql1);
$row = mysqli_fetch_array($result); //將結果變為陣列
print_r($row);
	
```

- UPDATE `表單名` SET `欄位名` = `$data` WHERE 條件式
```php=

if(isset($_POST['fix'])){

$text1 = $_POST['text1'];
$text2 = $_POST['text2'];
$text3 = $_POST['text3'];
$text4 = $_POST['text4'];
$text5 = $_POST['text5'];
$fix = $_POST['fix_value'];

$sql2 = "UPDATE `wow` SET
        `s1`= '$text1',
        `s2`= '$text2',
        `s3`= '$text3',
        `s4`= '$text4',
        `s5`= '$text5'
        WHERE `s1` = '$fix'";
filterTable($sql2);

}

```

- DELETE FROM `表單名` WHERE 條件式

```php=

if(isset($_POST['del'])){

	$text1 = $_POST['text1'];
	$text2 = $_POST['text2'];
	$text3 = $_POST['text3'];
	$text4 = $_POST['text4'];
	$text5 = $_POST['text5'];
	$del = $_POST['del_value'];

	$sql3 = "DELETE FROM `wow` WHERE `s1` = '$del'";
	filterTable($sql3);
}

```


# 實作 註冊→登入→會員中心

# Register.php

- 一、將要傳值的表單完成
    - uniqid() 以為秒計的當前時間，生成一個唯一的ID
```php=
<?php
$uni = sha1(uniqid(rand(),true)); //唯一標識符
?>
<input type="hidden" name="uni" value="<?php echo $uni;?>" />
```

- 二、到main.php準備接資料

```php=
if(isset($_POST['register'])){	/*註冊*/


	/*********接收資料***********/

	$name = $_POST['name'];
	$nickname = $_POST['nickname'];
	$account = $_POST['account'];
	$password = $_POST['password'];
	$checkPassword = $_POST['checkPassword'];
	$sex = $_POST['sex'];
	$birthday=$_POST['year'].'/'.$_POST['month'].'/'.$_POST['day'];	/*將日期合併*/
	$mail = $_POST['mail'];
	$address = $_POST['address'];
	$uni = $_POST['uni'];
	$time = date("Y-m-d",time());

	/******************************/



	/********* 將資料寫進資料庫 *********/

	$sql1="INSERT INTO user(`account`,`name`,`nickname`,`sex`,`birthday`,`mail`,`address`)
			VALUES('".$account."','".$name."','".$nickname."','".$sex."','".$birthday."','".$mail."','".$address."')";
	$sql2="INSERT INTO detail(`account`,`password`,`uni`,`time`)
			VALUES ('".$account."','".$password."','".$uni."','".$time."')";
	filterTable($sql1);
	filterTable($sql2);
	_alert('註冊成功!','login.php');

	/*************************************/


	}
```

# Login.php

- 一、將要傳出的表單完成
    - checkCode.php
        - 產生一組驗證碼圖片
    - SESSION 全域變數
        - 基本用法
```php=
session_start();
$_SESSION['test'] = 'abc';
```
- 二、到main.php準備接資料
```php=
if(isset($_POST['login'])){		/*登入*/
	/*接收資料*/
	$account = $_POST['account'];
	$pwd = $_POST['password'];
	$check = $_POST['check'];
	/**********/

	/******* 登入判斷 ******/
	$SQL1="SELECT `account` FROM `detail` WHERE `account` = '".$account."' AND `password` ='".$pwd."'"; /*與資料庫核對有無此帳號與密碼*/

	if($check==$_SESSION['checkNum']){
		$result=_fetch_array($SQL1);
		if($result!=""){
			$_SESSION['user'] = $account;
			_alert('登入成功!','member.php');
		}else{
			_alert('登入失敗!','login.php');
		}
	}else{
		_alert('驗證碼錯誤!','login.php');
	}
	/*************************/
	
	_alert('登入成功!','member.php');
	// print_r($result);

}
	
```


    


        
    



    
    





# 修改XAMPP的mysql密碼
C:\xampp\phpMyAdmin\config.inc.php

```php=
/* Authentication type and info */

$cfg['Servers'][$i]['auth_type'] = 'http'; <-改成http

$cfg['Servers'][$i]['user'] = 'root';

$cfg['Servers'][$i]['password'] = '●●●●●●';   <-填入剛剛輸入的密碼

$cfg['Servers'][$i]['extension'] = 'mysqli';

$cfg['Servers'][$i]['AllowNoPassword'] = true;

$cfg['Lang'] = '';
```

# Dark Theme CSS

{%hackmd theme-dark %}