• 沒有找到結果。

PHP 連結 MySQL 資料庫 功能表

N/A
N/A
Protected

Academic year: 2021

Share "PHP 連結 MySQL 資料庫 功能表"

Copied!
27
0
0

加載中.... (立即查看全文)

全文

(1)

PHP 連結 MySQL 資料庫  功能表

登入 處理登入 登出 顯示紀錄 新增紀錄 儲存新增紀錄 查看鍵值 - 設定 取得鍵值 顯示鍵值紀錄 修改紀錄 刪除紀錄 選資料庫 取得資料庫 選資料表 取得資料表 PHP 範例檔

(2)

功能表 menu.php

<html>

<head>

<meta http-equiv="Content-Language" content="zh-tw">

<meta http-equiv="Content-Type" content="text/html; charset=big5">

<base target="main">

</head>

<body bgcolor="#CCFFFF">

<a href= "dblogon.html"><font size=3>登入</font></a><br>

<a href="logout.php"><font size=3>登出</font></a><br>

<a href="showrecord.php"><font size=3>顯示紀錄</font></a><br>

<a href="addrecord.php"><font size=3>新增紀錄</font></a><br>

<a href="setkeyvalue.php"><font size=3>查看鍵值</font></a><br>

<a href="selectdb.php"><font size=3>選資料庫</font></a><br>

<a href="selecttbl.php"><font size=3>選資料表</font></a><br>

<a href="showtblall.php"><font size=3>顯示紀錄</font></a><br>

<a href="showtblrec.php"><font size=3>逐筆顯示</font></a><br>

</body>

</html>

(3)

登入

dblogon.html

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=big5">

<title>PHP連結MySQL登錄表單</title>

</head>

<body>

<form method="POST" action="dblogon.php">

<div align="center"><center><table border="0" width="80%"

cellspacing="1">

<tr>

<td width="11%" bgcolor="#C0C0C0"><strong>帳號</strong></td>

<td width="89%"><input type="text" name="username"

size="10"></td>

</tr>

<tr>

<td width="11%" bgcolor="#C0C0C0"><strong>密碼</strong></td>

<td width="89%"><input type="password" name="password"

size="10"></td>

</tr>

<tr>

<td width="11%" bgcolor="#C0C0C0"><strong>主機</strong></td>

<td width="89%"><input type="text" name="host" value="localhost"

size="60"></td>

</tr>

<tr>

<td width="11%" bgcolor="#C0C0C0"><strong>資料庫

</strong></td>

<td width="89%"><input type="text" name="dbname"

size="20"></td>

</tr>

<tr>

<td width="11%"></td>

<td width="89%"><input type="submit" value="送出"

name="send">

<input type="reset" value="重填" name="cancel" ></td>

(4)

</tr>

</table>

</center></div>

</form>

</body>

</html>

(5)

處理登入

<?php

session_start();

$username = $_POST['username'];

$password = $_POST['password'];

$host = $_POST['host'];

$dbname = $_POST['dbname'];

$_SESSION['username'] = $username;

$_SESSION['password'] = $password;

$_SESSION['host'] = $host;

$_SESSION['dbname'] = $dbname;

$link = mysql_pconnect($host,$username,$password) or die(mysql_error());

mysql_select_db($dbname) or die(mysql_error());

mysql_query("SET CHARSET big5");

if (mysql_errno()) {

header("HTTP/1.1 500 Internal Server Error");

echo $query.'\n';

echo mysql_error();

} else {

echo "Connect successfully...\n";

$url='http://localhost/menu.php';

echo '<META HTTP-EQUIV=REFRESH CONTENT="1; '.$url.'">';

}

?>

(6)

登出

logout.php

<?php

session_start();

session_destroy();

echo "Disconnected successfully...\n";

die();

?>

(7)

顯示紀錄

showrecord.php

<?php

session_start();

?>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=big5">

<title>PHP連結MySQL 首頁</title>

</head>

<body>

<div align="center"><center>

<table border="1" width="95%" cellspacing="1">

客戶資料表<br>

<tr>

<td width="1%">編號</td>

<td width="8%">公司名稱 </td>

<td width="10%">電話 </td>

<td width="10%">手機 </td>

<td width="20%"> Email</td>

<td width="5%">聯絡人</td>

<td width="25%">地址 </td>

<td width="8%">統編 </td>

<td width="1%">報表 </td>

</tr>

<?php

//session_start();

$username = $_SESSION['username'];

$password = $_SESSION['password'];

$host = $_SESSION['host'];

$dbname =$_SESSION['dbname'];

$link = mysql_pconnect($host,$username,$password) or

(8)

die(mysql_error());

mysql_select_db($dbname) or die(mysql_error());

mysql_query("SET CHARSET big5");

//mysql_query("use mysal",$link);

$result = mysql_query("SELECT * FROM cuinfo order by cu_no;");

if (!$result ) {

die("Could not open cuinfo: " . mysql_error());

}

$row = mysql_fetch_row($result);

while ($row != NULL) {

list($cu_no, $cu_na, $cu_tel, $cu_mtel, $cu_email,$cu_staf,$cu_adrs,

$cu_txno,$ml_type) = $row;

echo <<<EOD <tr>

<td width="1%">$cu_no</td>

<td width="8%">$cu_na</td>

<td width="10%">$cu_tel</td>

<td width="10%">$cu_mtel</td>

<td width="20%"> $cu_emaill</td>

<td width="8%">$cu_staf</td>

<td width="25%">$cu_adrs</td>

<td width="8%">$cu_txno</td>

<td width="1%">$ml_type</td>

</tr>

EOD;

$row = mysql_fetch_row($result);

}

mysql_free_result($result);

?>

</table>

<a href= "menu.php"><font size=3>回首頁</font></a><br>

</center></div>

</body>

</html>

(9)

新增紀錄 addrecord.php

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=big5">

<title>PHP連結MySQL 新增紀錄</title>

</head>

<body>

<div align="center"><center>

客戶資料表<br>

<form method="POST" action="insertrec.php">

<div align="center"><center><table border="0" width="80%"

cellspacing="1">

<table border="1" width="80%" cellspacing="1">

<tr>

<td width="15%" bgcolor="#C0C0C0"><strong>編號</td>

<td width="85%"><input type="text" name="cu_no"

size="10"></td>

</tr><tr>

<td width="15%">公司名稱 </td>

<td width="85%"><input type="text" name="cu_na"

size="20"></td>

</tr><tr>

<td width="15%">電話 </td>

<td width="85%"><input type="text" name="cu_tel"

size="12"></td>

</tr><tr>

<td width="15%">手機 </td>

<td width="85%"><input type="text" name="cu_mtel"

size="12"></td>

</tr><tr>

<td width="15%"> Email</td>

(10)

<td width="85%"><input type="text" name="cu_email"

size="40"></td>

</tr><tr>

<td width="15%">聯絡人</td>

<td width="85%"><input type="text" name="cu_staf"

size="12"></td>

</tr><tr>

<td width="15%">地址 </td>

<td width="85%"><input type="text" name="cu_adrs"

size="40"></td>

</tr><tr>

<td width="15%">統編</td>

<td width="85%"><input type="text" name="cu_txno"

size="10"></td>

</tr><tr>

<td width="15%">報表 </td>

<td width="85%"><input type="text" name="ml_type"

size="4"></td>

</tr>

<tr>

<td width="15%"></td>

<td width="85%">

<input type="submit" value="新增" name="send">

<input type="reset" value="重填" name="cancel" >

</td>

</tr>

</table>

</center></div>

</form>

<a href= "menu.php"><font size=3>回首頁</font></a><br>

</body>

</html>

(11)

儲存新增紀錄 insertrec.php

<?php

session_start();

$username = $_SESSION['username'];

$password = $_SESSION['password'];

$host = $_SESSION['host'];

$dbname =$_SESSION['dbname'];

$keyvalue=$_SESSION['keyvalue'] ;

$link = mysql_pconnect($host,$username,$password) or die(mysql_error());

mysql_select_db($dbname) or die(mysql_error());

mysql_query("SET CHARSET big5");

$no = $_POST['cu_no'];

$na = $_POST['cu_na'];

$tel = $_POST['cu_tel'];

$mtel = $_POST['cu_mtel'];

$ml = $_POST['cu_email'];

$st = $_POST['cu_staf'];

$ad = $_POST['cu_adrs'];

$tx = $_POST['cu_txno'];

$tp = $_POST['ml_type'];

$sql="Insert Into cuinfo Values ('" . $no . "','" . $na . "','" . $tel . "','" .

$mtel . "','" . $ml . "','" . $st . "','" . $ad . "','" . $tx . "','" . $tp . "')";

mysql_query($sql);

//echo $sql;

if (mysql_errno()) {

echo "Failed...";

} else

(12)

{

echo "Insert Successfully...";

}

mysql_free_result($result);

?>

<form>

<input class="MyButton" type="button" value="回首頁"

onclick="window.location.href='http://localhost/menu.php'" />

&nbsp;&nbsp; &nbsp;&nbsp;

<input class="MyButton" type="button" value="新增紀錄"

onclick="window.location.href='http://localhost/addrecord.php'" />

</form>

(13)

查看鍵值 - 設定 setkeyvalue.php

<?php

session_start();

$username = $_SESSION['username'];

$password = $_SESSION['password'];

$host = $_SESSION['host'];

$dbname =$_SESSION['dbname'];

$link = mysql_pconnect($host,$username,$password) or die(mysql_error());

mysql_select_db($dbname) or die(mysql_error());

mysql_query("SET CHARSET big5");

$result = mysql_query("SELECT Distinct cu_no FROM cuinfo order by cu_no;");

if (!$result ) {

die("Could not open cuinfo: " . mysql_error());

}

?>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=big5">

<title>PHP連結MySQL 選取鍵值</title>

</head>

<body>

<div align="center"><center>

<form method="POST" action="getkeyvalue.php">

<div align="center"><center>

<table border="0" width="80%" cellspacing="1">

<tr>

<td width="15%" bgcolor="#C0C0C0"><strong>編號</strong></td>

(14)

<td width="85%"> <Select name="selValue" >

<?php

$row = mysql_fetch_row($result);

while ($row != NULL) {

list($cu_no) = $row;

// echo <<<EOD

echo "<Option Value='" . $cu_no . "'>" . $cu_no . "</Option>";

// EOD;

$row = mysql_fetch_row($result);

}

mysql_free_result($result);

?>

</Select>

</td></tr>

<tr>

<td width="15%"></td>

<td width="85%">

<input type="submit" value="送出" name="send">

<input type="reset" value="重填" name="cancel" >

</td>

</tr>

</table>

</center></div>

</form>

</center></div>

</body>

</html>

(15)

取得鍵值

getkeyvalue.php

<?php

session_start();

$keyvalue = $_POST['selValue'];

$_SESSION['keyvalue'] = $keyvalue;

echo "選取鍵值 =". $keyvalue . "\n";

$url='http://localhost/showkeyrec.php';

echo '<META HTTP-EQUIV=REFRESH CONTENT="1; '.$url.'">';

?>

(16)

顯示鍵值紀錄 showkeyrec.php

<?php

session_start();

//session_start();

$username = $_SESSION['username'];

$password = $_SESSION['password'];

$host = $_SESSION['host'];

$dbname =$_SESSION['dbname'];

$keyvalue=$_SESSION['keyvalue'] ;

$link = mysql_pconnect($host,$username,$password) or die(mysql_error());

mysql_select_db($dbname) or die(mysql_error());

mysql_query("SET CHARSET big5");

$sql ="SELECT * FROM cuinfo where cu_no='" . $keyvalue . "'";

$result = mysql_query($sql);

if (!$result ) {

die("Could not open cuinfo: " . mysql_error());

}

$row = mysql_fetch_row($result);

if ($row != NULL) {

} else {

echo $sql . "\n";

echo "無對應紀錄...";

} ?>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=big5">

(17)

<title>PHP連結MySQL 顯示鍵值紀錄</title>

</head>

<body>

<div align="center"><center>

客戶資料表<br>

<?php

list($cu_no, $cu_na, $cu_tel, $cu_mtel, $cu_email,$cu_staf,$cu_adrs,

$cu_txno,$ml_type) = $row;

?>

<form method="POST" action="updaterec.php">

<div align="center"><center><table border="0" width="80%"

cellspacing="1">

<table border="1" width="60%" cellspacing="1">

<tr>

<td width="15%" bgcolor="#C0C0C0"><strong>編號</td>

<td width="85%"><input type="text" name="cu_no" Value="<?

=$cu_no?>" size="10"></td>

</tr><tr>

<td width="15%">公司名稱 </td>

<td width="85%"><input type="text" name="cu_na" Value="<?

=$cu_na?>" size="20"></td>

</tr><tr>

<td width="15%">電話 </td>

<td width="85%"><input type="text" name="cu_tel" Value="<?

=$cu_tel?>" size="12"></td>

</tr><tr>

<td width="15%">手機 </td>

<td width="85%"><input type="text" name="cu_mtel" Value="<?

=$cu_mtel?>" size="12"></td>

</tr><tr>

<td width="15%"> Email</td>

<td width="85%"><input type="text" name="cu_email" Value="<?

=$cu_email?>" size="40"></td>

</tr><tr>

(18)

<td width="15%">聯絡人</td>

<td width="85%"><input type="text" name="cu_staf" Value="<?

=$cu_staf?>" size="12"></td>

</tr><tr>

<td width="15%">地址 </td>

<td width="85%"><input type="text" name="cu_adrs" Value="<?

=$cu_adrs?>" size="40"></td>

</tr><tr>

<td width="15%">統編</td>

<td width="85%"><input type="text" name="cu_txno" Value="<?

=$cu_txno?>" size="10"></td>

</tr><tr>

<td width="15%">報表 </td>

<td width="85%"><input type="text" name="ml_type" Value="<?

=$ml_type?>" size="4"></td>

</tr>

<tr>

<td width="15%">

<input class="MyButton" type="button" value="回首頁"

onclick="window.location.href='http://localhost/menu.php'" />

</td>

<td width="85%">

<input type="submit" value="修改" name="send">&nbsp;&nbsp;

&nbsp;&nbsp;

<input class="MyButton" type="button" value="刪除"

onclick="window.location.href='http://localhost/deleterec.php'"

/>&nbsp;&nbsp; &nbsp;&nbsp;

<input class="MyButton" type="button" value="重選鍵值"

onclick="window.location.href='http://localhost/setkeyvalue.php'" />

</td>

</tr>

</table>

</center></div>

</form>

(19)

<a href= "menu.php"><font size=3>回首頁</font></a><br>

</body>

</html>

(20)

修改紀錄 updaterec.php

<?php

session_start();

$username = $_SESSION['username'];

$password = $_SESSION['password'];

$host = $_SESSION['host'];

$dbname =$_SESSION['dbname'];

$keyvalue=$_SESSION['keyvalue'] ;

$link = mysql_pconnect($host,$username,$password) or die(mysql_error());

mysql_select_db($dbname) or die(mysql_error());

mysql_query("SET CHARSET big5");

$no = $_POST['cu_no'];

$na = $_POST['cu_na'];

$tel = $_POST['cu_tel'];

$mtel = $_POST['cu_mtel'];

$ml = $_POST['cu_email'];

$st = $_POST['cu_staf'];

$ad = $_POST['cu_adrs'];

$tx = $_POST['cu_txno'];

$tp = $_POST['ml_type'];

$sql="Update cuinfo set cu_no='" . $no . "'";

$sql=$sql . ", cu_name='" . $na . "'";

$sql=$sql . ", cu_tel='" . $tel . "'";

$sql=$sql . ", cu_mtel='" . $mtel . "'";

$sql=$sql . ", cu_email='" . $ml . "'";

$sql=$sql . ", cu_staf='" . $st . "'";

$sql=$sql . ", cu_adrs='" . $ad . "'";

$sql=$sql . ", cu_txno='" . $tx . "'";

$sql=$sql . ", ml_type='" . $tp . "' where cu_no='" .

(21)

$_SESSION['keyvalue'] . "';";

mysql_query($sql);

//echo $sql;

if (mysql_errno()) {

echo "Failed...";

} else {

echo "Update Successfully...";

}

mysql_free_result($result);

?>

<form>

<input class="MyButton" type="button" value="回首頁"

onclick="window.location.href='http://localhost/menu.php'" />

&nbsp;&nbsp; &nbsp;&nbsp;

<input class="MyButton" type="button" value="重選鍵值"

onclick="window.location.href='http://localhost/setkeyvalue.php'" />

</form>

(22)

刪除紀錄 deleterec.php

<?php

session_start();

//session_start();

$username = $_SESSION['username'];

$password = $_SESSION['password'];

$host = $_SESSION['host'];

$dbname =$_SESSION['dbname'];

$keyvalue=$_SESSION['keyvalue'] ;

$link = mysql_pconnect($host,$username,$password) or die(mysql_error());

mysql_select_db($dbname) or die(mysql_error());

$sql="Delete from cuinfo where cu_no='" . $_SESSION['keyvalue'] . "';";

mysql_query($sql);

if (mysql_errno()) {

echo "Delete Record Failed...";

} else {

echo "Delete Record Successfully...";

}

?>

<form>

<input class="MyButton" type="button" value="回首頁"

onclick="window.location.href='http://localhost/menu.php'" />

&nbsp;&nbsp; &nbsp;&nbsp;

<input class="MyButton" type="button" value="重選鍵值"

onclick="window.location.href='http://localhost/setkeyvalue.php'" />

</form>

(23)

選資料庫 selectdb.php

<?php

session_start();

$username = $_SESSION['username'];

$password = $_SESSION['password'];

$host = $_SESSION['host'];

$dbname =$_SESSION['dbname'];

$link = mysql_pconnect($host,$username,$password) or die(mysql_error());

mysql_select_db($dbname) or die(mysql_error());

mysql_query("SET CHARSET big5");

$result = mysql_query("show databases;");

if (!$result ) {

die("Could not open Database: " . mysql_error());

}

?>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=big5">

<title>PHP連結MySQL 選取資料庫</title>

</head>

<body>

<div align="center"><center>

<form method="POST" action="getselectdb.php">

<div align="center"><center>

<table border="0" width="80%" cellspacing="1">

<tr>

<td width="15%" bgcolor="#C0C0C0"><strong>資料庫

</strong></td>

(24)

<td width="85%"> <Select name="selValue" >

<?php

$row = mysql_fetch_row($result);

while ($row != NULL) {

list($db) = $row;

echo "<Option Value='" . $db . "'>" . $db . "</Option>";

$row = mysql_fetch_row($result);

}

mysql_free_result($result);

?>

</Select>

</td></tr>

<tr>

<td width="15%"></td>

<td width="85%">

<input type="submit" value="送出" name="send">

<input type="reset" value="重填" name="cancel" >

</td>

</tr>

</table>

</center></div>

</form>

</center></div>

</body>

</html>

(25)

取得資料庫 getselectdb.php

<?php

session_start();

$keyvalue = $_POST['selValue'];

$_SESSION['dbname'] = $keyvalue;

echo "選取資料庫 = ". $keyvalue . "\n";

?>

<form>

<input class="MyButton" type="button" value="回首頁"

onclick="window.location.href='http://localhost/menu.php'" />

</form>

(26)

選資料表 selecttbl.php

<?php

session_start();

$username = $_SESSION['username'];

$password = $_SESSION['password'];

$host = $_SESSION['host'];

$dbname =$_SESSION['dbname'];

$link = mysql_pconnect($host,$username,$password) or die(mysql_error());

mysql_select_db($dbname) or die(mysql_error());

mysql_query("SET CHARSET big5");

$result = mysql_query("show tables;");

if (!$result ) {

die("Could not open Database: " . mysql_error());

}

?>

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=big5">

<title>PHP連結MySQL 選取資料表</title>

</head>

<body>

<div align="center"><center>

<form method="POST" action="getselecttbl.php">

<div align="center"><center>

<table border="0" width="80%" cellspacing="1">

<tr>

<td width="15%" bgcolor="#C0C0C0"><strong>資料表

</strong></td>

(27)

<td width="85%"> <Select name="selValue" >

<?php

$row = mysql_fetch_row($result);

while ($row != NULL) {

list($tbl) = $row;

echo "<Option Value='" . $tbl . "'>" . $tbl . "</Option>";

$row = mysql_fetch_row($result);

}

mysql_free_result($result);

?>

</Select>

</td></tr>

<tr>

<td width="15%"></td>

<td width="85%">

<input type="submit" value="送出" name="send">

<input type="reset" value="重填" name="cancel" >

</td>

</tr>

</table>

</center></div>

</form>

</center></div>

</body>

</html>

(28)

取得資料表 getselecttbl.php

<?php

session_start();

$keyvalue = $_POST['selValue'];

$_SESSION['tblname'] = $keyvalue;

echo "選取資料表 = ". $keyvalue . "\n";

?>

<form>

<input class="MyButton" type="button" value="回首頁"

onclick="window.location.href='http://localhost/menu.php'" />

</form>

(29)

dblogon.php

<?php

session_start();

$username = $_POST['username'];

$password = $_POST['password'];

$host = $_POST['host'];

$dbname = $_POST['dbname'];

$_SESSION['username'] = $username;

$_SESSION['password'] = $password;

$_SESSION['host'] = $host;

$_SESSION['dbname'] = $dbname;

$link = mysql_pconnect($host,$username,$password) or die(mysql_error());

mysql_select_db($dbname) or die(mysql_error());

mysql_query("SET CHARSET big5");

if (mysql_errno()) {

header("HTTP/1.1 500 Internal Server Error");

echo $query.'\n';

echo mysql_error();

} else {

echo "Connect successfully...\n";

$url='http://localhost/menu.php';

echo '<META HTTP-EQUIV=REFRESH CONTENT="1; '.$url.'">';

}

?>

參考文獻

相關文件

: joint hearing before the Subcommittee on International Economic Policy, Export and Trade Promotion and the Subcommittee on East Asian and Pacific Affairs of the Committee

– Write special code to protect against system crashes – Optimize applications for efficient access and query – May often rewrite applications. • Easier to buy a DBMS to handle

(十二) 裁判長資料袋整理、封條及成績彙收作業(評分結束後收取評分 表、競賽總成績表、優勝前 5 名及佳作成績表及競賽場紀錄表

(二)使用 PHP 語言、MySQL 資料庫與 Apache 伺服軟體開發互

透過 Java Servlet 程式存取資料庫.

Client: Angular 、 Cordova Server: Node.js(Express) 資料庫: MySQL. 套件管理: Node Package

 試著將投影片第 12 頁的範例加上 critical section ,解決 race

神秘的資料結構