mysql -uroot -p
Enter password: 在此輸入密碼
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 44
Server version: 5.5.44-0+deb8u1 (Raspbian)
Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
看到提示符號「mysql>」後,輸入底下指令,建立名為wordpress的資料庫:
mysql> create database mydb;
Query OK, 1 row affected (0.00 sec)
然後新增資料表....
mysql> create table member_table( 按下Enter
-> no int(6), 按下Enter
-> username char(20), 按下Enter
-> password char(20), 按下Enter
-> telephone char(12), 按下Enter
-> address text, 按下Enter
-> other text); 按下Enter
如此即可,然後按Ctrl + d跳出ㄡ
接下來參考範例網址...
首先先來張架構圖.....
(1) 首頁 - 登入頁面 (index.php)<!-- 設定網頁編碼為UTF-8 -->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<form name="form" method="post" action="connect.php">
帳號:<input type="text" name="id" /> <br>
密碼:<input type="password" name="pw" /> <br>
<input type="submit" name="button" value="登入" />
<a href="register.php">申請帳號</a>
</form>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<form name="form" method="post" action="connect.php">
帳號:<input type="text" name="id" /> <br>
密碼:<input type="password" name="pw" /> <br>
<input type="submit" name="button" value="登入" />
<a href="register.php">申請帳號</a>
</form>
由於一開始都沒有帳號.... 所以只好按下 申請帳號的連結 -> register.php
(2) 加入(註冊)會員 - 「填寫」會員資料 (register.php)
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<form name="form" method="post" action="register_finish.php">
帳號:<input type="text" name="id" /> <br>
密碼:<input type="password" name="pw" /> <br>
再一次輸入密碼:<input type="password" name="pw2" /> <br>
電話:<input type="text" name="telephone" /> <br>
地址:<input type="text" name="address" /> <br>
備註:<textarea name="other" cols="45" rows="5"></textarea> <br>
<input type="submit" name="button" value="確定" />
</form>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<form name="form" method="post" action="register_finish.php">
帳號:<input type="text" name="id" /> <br>
密碼:<input type="password" name="pw" /> <br>
再一次輸入密碼:<input type="password" name="pw2" /> <br>
電話:<input type="text" name="telephone" /> <br>
地址:<input type="text" name="address" /> <br>
備註:<textarea name="other" cols="45" rows="5"></textarea> <br>
<input type="submit" name="button" value="確定" />
</form>
按下確定的按鈕 -> register_finish.php
(3) 加入(註冊)會員 - 「新增」會員資料進MySQL資料庫 (register_finish.php)
<?php session_start(); ?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
include("mysql_connect.inc.php");
$id = $_POST['id'];
$pw = $_POST['pw'];
$pw2 = $_POST['pw2'];
$telephone = $_POST['telephone'];
$address = $_POST['address'];
$other = $_POST['other'];
<?php session_start(); ?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
include("mysql_connect.inc.php");
$id = $_POST['id'];
$pw = $_POST['pw'];
$pw2 = $_POST['pw2'];
$telephone = $_POST['telephone'];
$address = $_POST['address'];
$other = $_POST['other'];
//判斷帳號密碼是否為空值
//確認密碼輸入的正確性
//確認密碼輸入的正確性
if($id != null && $pw != null && $pw2 != null && $pw == $pw2)
{
//新增資料進資料庫語法
$sql = "insert into member_table (username, password, telephone, address, other) values ('$id', '$pw', '$telephone', '$address', '$other')"; if(mysql_query($sql))
{
echo '新增成功!';
echo '<meta http-equiv=REFRESH CONTENT=2;url=index.php>';
}
else
{
echo '新增失敗!';
echo '<meta http-equiv=REFRESH CONTENT=2;url=index.php>';
}
}
else
{
echo '您無權限觀看此頁面!';
echo '<meta http-equiv=REFRESH CONTENT=2;url=index.php>';
}
?>
php連結MySQL資料庫語法(mysql_connect.inc.php)
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php//資料庫設定
//資料庫位置
$db_server = "localhost";
//資料庫名稱
$db_name = "mydb";
//資料庫管理者帳號
$db_user = "root";
//資料庫管理者密碼$db_passwd = "1234";
<?php//資料庫設定
//資料庫位置
$db_server = "localhost";
//資料庫名稱
$db_name = "mydb";
//資料庫管理者帳號
$db_user = "root";
//資料庫管理者密碼$db_passwd = "1234";
//對資料庫連線if(!@mysql_connect($db_server, $db_user, $db_passwd))
die("無法對資料庫連線");
//資料庫連線採UTF8
mysql_query("SET NAMES utf8");
//選擇資料庫
if(!@mysql_select_db($db_name))
die("無法使用資料庫");
?>
die("無法對資料庫連線");
//資料庫連線採UTF8
mysql_query("SET NAMES utf8");
//選擇資料庫
if(!@mysql_select_db($db_name))
die("無法使用資料庫");
?>
(4) 會員ID、PW與MySQL資料庫作認證(connect.php)
<?php session_start(); ?>
<!--上方語法為啟用session,此語法要放在網頁最前方--><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
//連接資料庫
//只要此頁面上有用到連接MySQL就要include它
include("mysql_connect.inc.php");$id = $_POST['id'];
$pw = $_POST['pw'];
<?php session_start(); ?>
<!--上方語法為啟用session,此語法要放在網頁最前方--><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
//連接資料庫
//只要此頁面上有用到連接MySQL就要include它
include("mysql_connect.inc.php");$id = $_POST['id'];
$pw = $_POST['pw'];
//搜尋資料庫資料
$sql = "SELECT * FROM member_table where username = '$id'";
$result = mysql_query($sql);
$row = @mysql_fetch_row($result);
$sql = "SELECT * FROM member_table where username = '$id'";
$result = mysql_query($sql);
$row = @mysql_fetch_row($result);
//判斷帳號與密碼是否為空白//以及MySQL資料庫裡是否有這個會員
if($id != null && $pw != null && $row[1] == $id && $row[2] == $pw)
{
//將帳號寫入session,方便驗證使用者身份 $_SESSION['username'] = $id; echo '登入成功!';
echo '<meta http-equiv=REFRESH CONTENT=1;url=member.php>';
}
else
{
echo '登入失敗!';
echo '<meta http-equiv=REFRESH CONTENT=1;url=index.php>';
}
?>
登入成功後會跳到 member.php....反之登入失敗會跳回到 index.php
畫面如下:
if($id != null && $pw != null && $row[1] == $id && $row[2] == $pw)
{
//將帳號寫入session,方便驗證使用者身份 $_SESSION['username'] = $id; echo '登入成功!';
echo '<meta http-equiv=REFRESH CONTENT=1;url=member.php>';
}
else
{
echo '登入失敗!';
echo '<meta http-equiv=REFRESH CONTENT=1;url=index.php>';
}
?>
登入成功後會跳到 member.php....反之登入失敗會跳回到 index.php
(5) 會員登入成功後 頁面 - 此頁面有「新增」、「修改」、「刪除」與「登出」的連結
並且會顯示出所有會員資料(member.php)
<?php session_start(); ?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?phpinclude("mysql_connect.inc.php");
echo '<a href="logout.php">登出</a> <br><br>';
並且會顯示出所有會員資料(member.php)
<?php session_start(); ?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?phpinclude("mysql_connect.inc.php");
echo '<a href="logout.php">登出</a> <br><br>';
//此判斷為判定觀看此頁有沒有權限
//說不定是路人或不相關的使用者
//因此要給予排除
if($_SESSION['username'] != null)
{
echo '<a href="register.php">新增</a> ';
echo '<a href="update.php">修改</a> ';
echo '<a href="delete.php">刪除</a> <br><br>';
//將資料庫裡的所有會員資料顯示在畫面上
$sql = "SELECT * FROM member_table";
$result = mysql_query($sql);
while($row = mysql_fetch_row($result))
{
echo "$row[0] - 名字(帳號):$row[1], " .
"電話:$row[3], 地址:$row[4], 備註:$row[5]<br>";
}}
else
{
echo '您無權限觀看此頁面!';
echo '<meta http-equiv=REFRESH CONTENT=2;url=index.php>';
}
?>
//說不定是路人或不相關的使用者
//因此要給予排除
if($_SESSION['username'] != null)
{
echo '<a href="register.php">新增</a> ';
echo '<a href="update.php">修改</a> ';
echo '<a href="delete.php">刪除</a> <br><br>';
//將資料庫裡的所有會員資料顯示在畫面上
$sql = "SELECT * FROM member_table";
$result = mysql_query($sql);
while($row = mysql_fetch_row($result))
{
echo "$row[0] - 名字(帳號):$row[1], " .
"電話:$row[3], 地址:$row[4], 備註:$row[5]<br>";
}}
else
{
echo '您無權限觀看此頁面!';
echo '<meta http-equiv=REFRESH CONTENT=2;url=index.php>';
}
?>
畫面如下:
新增 -> register.php
修改 -> update.php
刪除 -> delete.php
登出 -> logout.php
加入(註冊)會員 - 「填寫」會員資料 (register.php)
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<form name="form" method="post" action="register_finish.php">
帳號:<input type="text" name="id" /> <br>
密碼:<input type="password" name="pw" /> <br>
再一次輸入密碼:<input type="password" name="pw2" /> <br>
電話:<input type="text" name="telephone" /> <br>
地址:<input type="text" name="address" /> <br>
備註:<textarea name="other" cols="45" rows="5"></textarea> <br>
<input type="submit" name="button" value="確定" />
</form>
修改會員資料 - 「填寫」要修改之會員資料(update.php)
<?php session_start(); ?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
include("mysql_connect.inc.php");
if($_SESSION['username'] != null)
{
//將$_SESSION['username']丟給$id
//這樣在下SQL語法時才可以給搜尋的值
$id = $_SESSION['username'];
//若以下$id直接用$_SESSION['username']將無法使用
$sql = "SELECT * FROM member_table where username='$id'";
$result = mysql_query($sql);
$row = mysql_fetch_row($result);
echo "<form name=\"form\" method=\"post\" action=\"update_finish.php\">";
echo "帳號:<input type=\"text\" name=\"id\" value=\"$row[1]\" />(此項目無法修改) <br>";
echo "密碼:<input type=\"password\" name=\"pw\" value=\"$row[2]\" /> <br>";
echo "再一次輸入密碼:<input type=\"password\" name=\"pw2\" value=\"$row[2]\" /> <br>";
echo "電話:<input type=\"text\" name=\"telephone\" value=\"$row[3]\" /> <br>";
echo "地址:<input type=\"text\" name=\"address\" value=\"$row[4]\" /> <br>";
echo "備註:<textarea name=\"other\" cols=\"45\" rows=\"5\">$row[5]</textarea> <br>";
echo "<input type=\"submit\" name=\"button\" value=\"確定\" />";
echo "</form>";
}
else
{
echo '您無權限觀看此頁面!';
echo '<meta http-equiv=REFRESH CONTENT=2;url=index.php>';
}
?>
修改會員資料 - 「更新」要修改之會員資料進MySQL資料庫(update_finish.php)
<?php session_start(); ?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
include("mysql_connect.inc.php");
$id = $_POST['id'];
$pw = $_POST['pw'];
$pw2 = $_POST['pw2'];
$telephone = $_POST['telephone'];
$address = $_POST['address'];
$other = $_POST['other'];
<?php session_start(); ?>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
include("mysql_connect.inc.php");
$id = $_POST['id'];
$pw = $_POST['pw'];
$pw2 = $_POST['pw2'];
$telephone = $_POST['telephone'];
$address = $_POST['address'];
$other = $_POST['other'];
//紅色字體為判斷密碼是否填寫正確
if($_SESSION['username'] != null && $pw != null && $pw2 != null && $pw == $pw2)
{
$id = $_SESSION['username'];
//更新資料庫資料語法
$sql = "update member_table set password=$pw, telephone=$telephone, address=$address, other=$other where username='$id'"; if(mysql_query($sql))
{
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>';
}
?>
刪除會員資料 - 「填寫」要刪除之會員帳號(delete.php)if($_SESSION['username'] != null && $pw != null && $pw2 != null && $pw == $pw2)
{
$id = $_SESSION['username'];
//更新資料庫資料語法
$sql = "update member_table set password=$pw, telephone=$telephone, address=$address, other=$other where username='$id'"; if(mysql_query($sql))
{
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>';
}
?>
<?php session_start(); ?><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
if($_SESSION['username'] != null)
{
echo "<form name=\"form\" method=\"post\" action=\"delete_finish.php\">";
echo "要刪除的帳號:<input type=\"text\" name=\"id\" /> <br>";
echo "<input type=\"submit\" name=\"button\" value=\"刪除\" />";
echo "</form>";
}
else
{
echo '您無權限觀看此頁面!';
echo '<meta http-equiv=REFRESH CONTENT=2;url=index.php>';
}
?>
刪除會員資料 - 對MySQL資料庫進行「刪除」會員資料(delete_finish.php)
<?php session_start(); ?><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
include("mysql_connect.inc.php");
$id = $_POST['id'];
<?php session_start(); ?><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
include("mysql_connect.inc.php");
$id = $_POST['id'];
if($_SESSION['username'] != null)
{
//刪除資料庫資料語法
$sql = "delete from member_table where username='$id'"; if(mysql_query($sql))
{
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>';
}
?>
{
//刪除資料庫資料語法
$sql = "delete from member_table where username='$id'"; if(mysql_query($sql))
{
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>';
}
?>
登出 - 洗掉登入使用者之session(logout.php)
<?php session_start(); ?><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php//將session清空
unset($_SESSION['username']);
echo '登出中......';
echo '<meta http-equiv=REFRESH CONTENT=1;url=index.php>';
?>
<?php session_start(); ?><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php//將session清空
unset($_SESSION['username']);
echo '登出中......';
echo '<meta http-equiv=REFRESH CONTENT=1;url=index.php>';
?>
Reference : example 1
沒有留言:
張貼留言