博客來網路書店查詢

書名

博客來網路書店查詢

星期四, 12月 23, 2010

PHP簡報檔

PHP簡報檔我分成多個檔案:
簡報檔案0到第九章的簡報,網址為
http://rapidshare.com/files/395256844/0to9ppt.zip
簡報檔案第十章到第十五章,網址為:
http://www.megaupload.com/?d=NW9OSUXS
以上網址已經不能使用..2011年1月1日修改後可再繼續使用

簡報檔案第十六章到第十九章,網址為:

http://www.megaupload.com/?d=6DC703SL

歡迎下載

星期一, 11月 22, 2010

PHP&MySQL--12 2010年11月課程講義預覽

PHP&MySQL--12
一、 沒有對應的方法與權限會如何呈現呢
1. 基本樣式


〈html〉〈head〉
〈meta http-equiv="content-type" content="text/html; charset=utf-8" /〉
〈title〉沒有對應的方法與權限會如何呈現呢〈/title〉〈/head〉
〈body〉
class cbox {
private $lenght1;
private $breadth1;
private $height1;
function getdata( ) {
return $this-〉breadth1*$this-〉height1*$this-〉lenght1;
}
}
$box1 = new cbox;
$box1-〉titlename="php";
echo $box1-〉titlename;
$box1-〉test2(123);〈/body〉〈/html〉



2. 處理方式【另存新檔後增加或修改內容】

function __get($var) {
echo "沒有對應的屬性可處理,若無此函數該屬性會自動產生:".$var."〈br〉"; }
function __set($a,$b) {
echo "沒有對應的屬性可接收,若無此函數該屬性會自動產生:".$a."〈br〉";
echo "第二個參數:".$b."〈br〉"; }
function __call($a,$b) {
echo "沒有對應的函數可處理,若無此函數會產生錯誤訊息:".$a."〈br〉";
echo "第二個參數:".$b."〈br〉"; }




3. 權限不合的處理方式?【另存新檔後增加或修改內容】

$box1 = new cbox;
$box1-〉lenght1="php";
echo $box1-〉lenght1;
$box1-〉test2(123);




二、 物件複製是否有錯?


〈html〉〈head〉
〈meta http-equiv="content-type" content="text/html; charset=utf-8" /〉
〈title〉物件複製是否有錯〈/title〉〈/head〉〈body〉
class cbox {
private $lenght1;
private $breadth1;
private $height1;
function __construct($a,$b,$c) {
$this-〉lenght1=$a;
$this-〉breadth1=$b;
$this-〉height1=$c;
}
function setlength1($lenght1)
{ $this-〉lenght1=$lenght1; }
function getlength1( )
{ return $this-〉lenght1; }
function getdata( ) {
return $this-〉breadth1*$this-〉height1*$this-〉lenght1; }
}
$box1 = new cbox(20,20,20);
$box2 = $box1;
$box2-〉setlength1(30);
echo "第1個箱子length1=".$box1-〉getlength1( )."〈br〉";
echo "第2個箱子length1=".$box2-〉getlength1( )."〈br〉";〈/body〉〈/html〉



三、 繼承
1. 基本樣式


〈html〉〈head〉
〈meta http-equiv="content-type" content="text/html; charset=utf-8" /〉
〈title〉繼承〈/title〉〈/head〉〈body〉
class cat extends animal {
function __construct( ) {
echo "產生一隻貓科動物"."〈br〉";
} }
class tiger extends cat{
function __construct( ) {
echo "產生一隻老虎"."〈br〉";
} }
class animal {
function __construct( ) {
echo "產生一隻動物"."〈br〉";
} }
$tiger1 = new tiger;
echo "〈hr〉";
$cat1 = new cat;
echo "〈hr〉";
$animal2 = new animal;
echo "〈hr〉";〈/body〉〈/html〉



2. 子類別執行父類別的建構子


class cat extends animal {
function __construct( ) {
parent::__construct( );//new
echo "產生一隻貓科動物"."〈br〉";
} }



3. 父子類別都有相同的方法

class cat extends animal {
function __construct( ) {
parent::__construct( );//new
echo "產生一隻貓科動物"."〈br〉"; }
function run( )
{ echo "貓科動物跑得快"."〈br〉"; } }
class tiger extends cat{
function __construct( ) {
echo "產生一隻老虎"."〈br〉"; }
function run( )
{ echo "老虎跑得快"."〈br〉"; } }




4. 子類別執行父類別的方法


class tiger extends cat{
function __construct( ) {
echo "產生一隻老虎"."〈br〉"; }
function run( )
{ echo "老虎跑得快"."〈br〉";
return parent::run( ); } }



四、 影像處理:劃框線


$img = ImageCreateFromPNG('img.png');
$color_black = ImageColorAllocate($img, 0, 0, 0);
drawBorder($img, $color_black, 30);
header('Content-type: image/png');
Imagepng($img);
function drawBorder(&$img, &$color, $thickness = 1)
{
$x1 = 0;
$y1 = 0;
$x2 = ImageSX($img) - 1;
$y2 = ImageSY($img) - 1;
for($i = 0; $i 〈 $thickness; $i++)
{
ImageRectangle($img, $x1++, $y1++, $x2--, $y2--, $color_black);
}
}




五、 繪製圖表-1

header("Content-type: image/png");
$arrval = array(12,123,21,32,77,85,166,176,163,121);
$height = 260;
$width = 330;
$im = imagecreate($width,$height);
$white = imagecolorallocate($im,255,255,255);
$gray = imagecolorallocate($im,200,200,200);
$black = imagecolorallocate($im,0,0,0);
$red = imagecolorallocate($im,255,0,0);
$x = 21;
$y = 11;
$num = 0;
while($x〈=$width && $y〈=$height){
$prcnt = ((($height-50)-($y-1))/($height-60))*100;
imageline($im, 21, $y, $width-10, $y, $gray);
imageline($im, $x, 11, $x, $height-50, $gray);
imagestring($im,2,1,$y-10,$prcnt.'%',$red);
imagestring($im,2,$x-3,$height-40,$num,$red);
$x += 30;
$y += 20;
$num++;
}
$tx = 20;
$ty = 210;
foreach($arrval as $values){
$cx = $tx + 30;
$cy = 200-$values;
imageline($im,$tx,$ty,$cx,$cy,$red);
imagestring($im,5,$cx-3,$cy-13,'.',$red);
$ty = $cy;
$tx = $cx;
}
imageline($im, 20, 11, 20, $height-50, $black);
imageline($im, 20, $height-49, $width-10, $height-49, $black);
imagestring($im,3,10,$height-20,'Line Graph by: Roseindia Technologies',$red);
imagepng($im);




六、 繪製圖表-2


$image = imagecreatetruecolor(300, 300);
$white = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);
$gray = imagecolorallocate($image, 0xC0, 0xC0, 0xC0);
$darkgray = imagecolorallocate($image, 0x90, 0x90, 0x90);
$navy = imagecolorallocate($image, 0x00, 0x00, 0x80);
$darknavy = imagecolorallocate($image, 0x00, 0x00, 0x50);
$red = imagecolorallocate($image, 0xFF, 0x00, 0x00);
$darkred = imagecolorallocate($image, 0x90, 0x00, 0x00);
for ($i = 160; $i 〉 150; $i--) {
imagefilledarc($image, 150, $i, 200, 100, 0, 45, $darknavy, IMG_ARC_PIE);
imagefilledarc($image, 150, $i, 200, 100, 45, 75 , $darkgray, IMG_ARC_PIE);
imagefilledarc($image, 150, $i, 200, 100, 75, 360 , $darkred, IMG_ARC_PIE);
}
imagefilledarc($image, 150, 150, 200, 100, 0, 45, $navy, IMG_ARC_PIE);
imagefilledarc($image, 150, 150, 200, 100, 45, 75 , $gray, IMG_ARC_PIE);
imagefilledarc($image, 150, 150, 200, 100, 75, 360 , $red, IMG_ARC_PIE);
header('Content-type: image/png');
imagepng($image);
imagedestroy($image);




七、 繪製圖表與資料庫


include "db.php";
$qt=mysql_query("select * from gd_graph");
header ("Content-type: image/jpg");
$x_gap=40;
$x_max=$x_gap*13;
$y_max=250;
$im = @ImageCreate ($x_max, $y_max) or die ("Cannot Initialize new GD image
stream");
$background_color = ImageColorAllocate ($im, 234, 234, 234);
$text_color = ImageColorAllocate ($im, 233, 14, 91);
$graph_color = ImageColorAllocate ($im,25,25,25);
$x1=0;
$y1=0;
$first_one="yes";
while($nt=mysql_fetch_array($qt)){
$x2=$x1+$x_gap;
$y2=$y_max-$nt[sales];
//ImageString($im,2,$x2,$y2,$nt[month].$nt[sales],$graph_color);
ImageString($im,2,$x2,$y2,$nt[month],$graph_color);
if($first_one=="no"){
imageline ($im,$x1, $y1,$x2,$y2,$text_color);
}
$x1=$x2;
$y1=$y2;
$first_one="no";
}
ImageJPEG ($im);




八、 其他

2010年11月:東區認證中心VB第三階段課程

算一下時間..上次帶VB第三階段課程是2006年12月..
四年後再次開課..希望能提供給各位滿滿的資訊。
這次課程將跟大家說明VB視窗程式設計,原則上以圖檔顯示與運動、
listbox運用、picturebox繪圖、功能表與表單切換做介紹。
課程會運用到第二階段所提的物件導向觀念,所以各位日前所學
的項目會有實作的機會。
課程中會使用講義,講義用來控制課程進度與課程重點提示,
課本請各位帶回家,於家中做練習之用。
如對課程內容有疑問..歡迎與我聯繫與討論。

星期五, 11月 19, 2010

2010年11月:東區認證中心C#初階課程

這次課程將跟大家說明C#程式變數宣告、條件判斷式、迴圈與陣列宣告等觀念。
程式設計採視窗程式設計(Windows Form),所以與坊間書籍於終端機環境
設計有很大的不同,但觀念相同。
課程中會使用講義,講義用來控制課程進度與課程重點提示,
課本請各位帶回家,於家中做練習之用。
如對課程內容有疑問..歡迎與我聯繫與討論。
課程中除參考過往經驗、課本安排課程進度外,另會參考
碁峰的「Visual C# 2005 程式設計經典」這本書內容進行課程內容安排。

星期三, 11月 17, 2010

PHP&MySQL--11 2010年11月課程講義預覽

PHP&MySQL--11
一、 簡單資料操作
1. 新增:請參考上次講義
2. 列表:請參考上次講義
3. 編輯


〈? ob_start( ); ?〉〈html〉〈head〉
〈meta http-equiv="content-type" content="text/html; charset=utf-8"〉
〈title〉編輯〈/title〉〈/head〉〈body〉
〈?
include("config.php");
if(!isset($_GET['id'])) die("請先執行返回資料列表網頁");
if($_GET['id']=="") die("請回資料列表網頁選擇資料");
$id = $_GET['id'];
$res = mysql_query("select * from student where id ='$id'");
$row = mysql_fetch_array($res);
?〉
〈table width="75%" border="0" align="center"〉
〈tr〉
〈td〉〈form action="edit2.php" method="post" name="form1"〉
〈p〉姓名
〈input name="frm_name" type="text" value="〈? echo $row["username"]; ?〉"〉
〈/p〉〈p〉帳號
〈input name="frm_account" type="text" value="〈? echo $row["account"]; ?〉"〉
〈/p〉〈p〉性別
〈input type="radio" name="frm_sex" value="1" 〈? if($row["sex"] == 1) echo "checked"; ?〉〉男
〈input type="radio" name="frm_sex" value="0" 〈? if($row["sex"] == 0) echo "checked"; ?〉〉女
〈/p〉〈p〉
〈input type="submit" name="Submit" value="送出"〉
〈input name="id" type="hidden" value="〈? echo $row["id"]; ?〉"〉
〈/p〉〈/form〉〈/td〉〈/tr〉〈/table〉〈/body〉〈/html〉





〈? ob_start( ); ?〉〈html〉〈head〉
〈meta http-equiv="content-type" content="text/html; charset=utf-8"〉
〈title〉編輯〈/title〉
〈/head〉〈body〉
〈?
include("config.php");
if(!isset($_POST['frm_account'])) die("請先執行編輯資料網頁");
if($_POST['frm_account']=="") die("請回編輯資料網頁輸入資料");
$id = $_POST['id'];
//echo $id."〈br〉";
$account = $_POST['frm_account'];
$username = $_POST['frm_name'];
$sex = $_POST['frm_sex'];
$temp_date = date("Y-m-d H:m:s");
$sql="update student set
account = '$account',
username = '$username',
sex = '$sex',
date1 = '$temp_date'
where id ='$id'";
//echo $sql."〈br〉";
//exit;
mysql_query("update student set
account = '$account',
username = '$username',
sex = '$sex',
date1 = '$temp_date'
where id ='$id'") or die("error");
header("location:index.php");
?〉〈/body〉〈/html〉



4. 刪除


〈? ob_start( ); ?〉〈html〉〈head〉
〈meta http-equiv="content-type" content="text/html; charset=utf-8"〉
〈title〉刪除〈/title〉〈/head〉〈body〉
〈?
include("config.php");
if(!isset($_GET['id'])) die("請先執行返回資料列表網頁");
if($_GET['id']=="") die("請回資料列表網頁選擇資料");
$id = $_GET['id'];
mysql_query("delete from student where id ='$id'") or die("SQL Error");
header("location:index.php");
?〉
〈/body〉〈/html〉



二、 CSV檔案匯出與匯入
1. 匯入【接收端網頁】


〈? include("server.php"); ?〉〈html〉
〈head〉〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉匯入 CSV 檔〈/title〉〈/head〉〈body〉
〈?
$uploaddir='./';
if ($_FILES["CSVfile"]["name"] ) {
$temp_SQL = "";
$file2=mb_convert_encoding($_FILES["CSVfile"]["name"],"big5","utf8");
if(!move_uploaded_file($_FILES['CSVfile']['tmp_name'],$uploaddir.$file2)){
echo "上傳失敗〈br〉";
}
$fp = fopen($_FILES["CSVfile"]["name"] , "r");
while ( $ROW = fgetcsv($fp,$_FILES["CSVfile"]["size"] ) ) {
if ( strlen($ROW[0]) ) {
if ( strlen($temp_SQL) ) $temp_SQL .= ", ";
$temp_SQL .= "('" . $ROW[0] . "', '" . $ROW[1] . "', '" . $ROW[2] . "', '" .
$ROW[3] . "') ";
}
}
fclose($fp);
$sql = "INSERT INTO student1(sid, username,address,birthday) VALUES " . $temp_SQL;
mysql_query($sql) or die(mysql_error( ));
}
else {
echo "Error!";
}?〉〈/body〉〈/html〉



2. 匯出


〈?
include("server.php");
$file = 'export';
$result = mysql_query("show columns from student1");
$i = 0;
if (mysql_num_rows($result) 〉 0) {
while ($row = mysql_fetch_assoc($result)) {
$csv_output .= $row['Field'].",";
$i++;
}
}
$csv_output .= "\n";

$values = mysql_query("select * from student1");
while ($rowr = mysql_fetch_row($values)) {
for ($j=0;$j〈$i;$j++) {
$csv_output .= $rowr[$j].", ";
}
$csv_output .= "\n";
};
ini_set("date.timezone","Asia/Taipei");
$filename = $file."_".date("Y-m-d_H-i",time( ));
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");
print $csv_output;
exit;
?〉



三、 上傳與解壓縮


〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉zip檔案上傳〈/title〉〈/head〉〈body〉〈?
include ('pclzip.lib.php');
$tmp_folder = basename($_FILES['myfile']['name']);
if(is_dir($tmp_folder))
{
echo "資料夾已存在"."〈br〉";
}
else
{
//echo "資料夾不存在"."〈br〉";
if(mkdir($tmp_folder, 0700))
echo "成功建立資料夾"."〈br〉";
else
echo "建立資料夾失敗"."〈br〉";
}
if($_FILES['myfile']['size']〉0){
if(is_uploaded_file($_FILES['myfile']['tmp_name'])&&$_FILES['myfile']['type']=='application/zip'){
if(is_writeable($tmp_folder)){
if(move_uploaded_file($_FILES['myfile']['tmp_name'],$tmp_folder.'/'.$_FILES['myfile']['name'])){
echo '檔案上傳成功!〈p〉';
$hzip = new PclZip($tmp_folder.'/'.$_FILES['myfile']['name']);
if($hzip-〉extract($tmp_folder.'/'))
echo '檔案解壓縮成功!';
else
echo '檔案解壓縮失敗!';
} else
echo '檔案上傳失敗!〈p〉';
} else
echo '檔案無法寫入!';
} else
echo '沒有上傳檔案!';
}
?〉〈/body〉〈/html〉




四、 初論
五、 屬性
1. 基本設計【請參考講義補充遺漏的語法】


〈html〉〈head〉
〈meta http-equiv="content-type" content="text/html; charset=utf-8" /〉
〈title〉箱子:類別與屬性〈/title〉〈/head〉
〈body〉
class cbox {
public $lenght1;
public $breadth1;
public $height1;
}
$box1 = new cbox( );
$box1-〉lenght1 = 20;
$box1-〉breadth1 = 20;
$box1-〉height1 = 20;
$box2 = new cbox( );
$box2-〉lenght1 = 30;
$box2-〉breadth1 = 30;
$box2-〉height1 = 30;
echo "第1個箱子體積=".$box1-〉breadth1*$box1-〉height1*$box1-〉lenght1."〈br〉";
echo "第2個箱子體積=".$box2-〉breadth1*$box2-〉height1*$box2-〉lenght1."〈br〉";
〈/body〉〈/html〉



2. 如果加上函數呢【另存新檔後增加或修改內容】


function getdata( ) {
return $this-〉breadth1*$this-〉height1*$this-〉lenght1;
}



3. 不過屬性仍可呼叫,因此請變更屬性權限並改變類別內方法接收資料方式【另存新檔後增加或修改內容】


function getdata($a,$b,$c) {
$this-〉lenght1=$a;
$this-〉breadth1=$b;
$this-〉height1=$c;
return $this-〉breadth1*$this-〉height1*$this-〉lenght1;


}
4. 加入建構子【另存新檔後增加或修改內容】


function __construct($a,$b,$c) {
$this-〉lenght1=$a;
$this-〉breadth1=$b;
$this-〉height1=$c;


}
5. 加入解構子【另存新檔後增加或修改內容】


function __destruct( ) {
print "Destroying "."〈br〉"; }



六、 方法內包含類別


〈html〉〈head〉
〈meta http-equiv="content-type" content="text/html; charset=utf-8" /〉
〈title〉方法內內包含類別〈/title〉〈/head〉
〈body〉
class cbox {
private $lenght1;
private $breadth1;
private $height1;
function __construct($a,$b,$c) {
$this-〉lenght1=$a;
$this-〉breadth1=$b;
$this-〉height1=$c; }
function getdata( ) {
return $this-〉breadth1*$this-〉height1*$this-〉lenght1; } }
class worker {
public function work($box3) {
$str = $box3-〉getdata( );
echo $str; }
}
$box4 = new cbox(20,30,20);
$worker1 = new worker( );
$worker1-〉work( $box4 ); 〈/body〉〈/html〉



七、 沒有對應會如何呈現呢
1. 基本樣式


〈html〉〈head〉
〈meta http-equiv="content-type" content="text/html; charset=utf-8" /〉
〈title〉沒有對應會如何呈現呢〈/title〉〈/head〉
〈body〉
class cbox {
private $lenght1;
private $breadth1;
private $height1;
function getdata( ) {
return $this-〉breadth1*$this-〉height1*$this-〉lenght1;
}
}
$box1 = new cbox;
$box1-〉titlename="php";
echo $box1-〉titlename;
$box1-〉test2(123); 〈/body〉〈/html〉



2. 處理方式【另存新檔後增加或修改內容】


function __get($var) {
echo "沒有對應的屬性可處理,若無此函數該屬性會自動產生:".$var."〈br〉"; }
function __set($a,$b) {
echo "沒有對應的屬性可接收,若無此函數該屬性會自動產生:".$a."〈br〉";
echo "第二個參數:".$b."〈br〉"; }
function __call($a,$b) {
echo "沒有對應的函數可處理,若無此函數會產生錯誤訊息:".$a."〈br〉";
echo "第二個參數:".$b."〈br〉"; }

3. 權限不合的處理方式?【另存新檔後增加或修改內容】



$box1 = new cbox;
$box1-〉lenght1="php";
echo $box1-〉lenght1;
$box1-〉test2(123);





八、 複製是否有錯?


〈html〉〈head〉
〈meta http-equiv="content-type" content="text/html; charset=utf-8" /〉
〈title〉複製是否有錯〈/title〉〈/head〉〈body〉
class cbox {
private $lenght1;
private $breadth1;
private $height1;
function __construct($a,$b,$c) {
$this-〉lenght1=$a;
$this-〉breadth1=$b;
$this-〉height1=$c;
}
function setlength1($lenght1)
{ $this-〉lenght1=$lenght1; }
function getlength1( )
{ return $this-〉lenght1; }
function getdata( ) {
return $this-〉breadth1*$this-〉height1*$this-〉lenght1; }
}
$box1 = new cbox(20,20,20);
$box2 = $box1;
$box2-〉setlength1(30);
echo "第1個箱子length1=".$box1-〉getlength1( )."〈br〉";
echo "第2個箱子length1=".$box2-〉getlength1( )."〈br〉"; 〈/body〉〈/html〉



九、 繼承
1. 基本樣式


〈html〉〈head〉
〈meta http-equiv="content-type" content="text/html; charset=utf-8" /〉
〈title〉繼承〈/title〉〈/head〉〈body〉
class cat extends animal {
function __construct( ) {
echo "產生一隻貓科動物"."〈br〉";
} }
class tiger extends cat{
function __construct( ) {
echo "產生一隻老虎"."〈br〉";
} }
class animal {
function __construct( ) {
echo "產生一隻動物"."〈br〉";
} }
$tiger1 = new tiger;
echo "〈hr〉";
$cat1 = new cat;
echo "〈hr〉";
$animal2 = new animal;
echo "〈hr〉"; 〈/body〉〈/html〉



2. 子類別執行父類別的建構子


class cat extends animal {
function __construct( ) {
parent::__construct( );//new
echo "產生一隻貓科動物"."〈br〉";
} }



3. 父子類別都有相同的方法


class cat extends animal {
function __construct( ) {
parent::__construct( );//new
echo "產生一隻貓科動物"."〈br〉"; }
function run( )
{ echo "貓科動物跑得快"."〈br〉"; } }
class tiger extends cat{
function __construct( ) {
echo "產生一隻老虎"."〈br〉"; }
function run( )
{ echo "老虎跑得快"."〈br〉"; } }



4. 子類別執行父類別的方法


class tiger extends cat{
function __construct( ) {
echo "產生一隻老虎"."〈br〉"; }
function run( )
{ echo "老虎跑得快"."〈br〉";
return parent::run( ); } }



十、 其他

星期五, 11月 12, 2010

PHP&MySQL--10 2010年11月課程講義預覽

PHP&MySQL--10
一、 上傳多個檔案
【提供表單網頁upload4.htm及接收資料網頁upload4.php,再請修改】

〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉處理上傳多個檔案〈/title〉〈/head〉 〈body〉〈?
$uploaddir='./upload/';
$i=count($_FILES["fileupload"]["name"]);
for ($j=0;$j〈$i;$j++){
$tmpfile=$_FILES["fileupload"]["tmp_name"][$j];
$file2=mb_convert_encoding($_FILES["fileupload"]["name"][$j],"big5","utf8");
if(move_uploaded_file($tmpfile,$uploaddir.$file2)){
echo "上傳成功〈br〉";
echo "檔案名稱:".$_FILES["fileupload"]["name"][$j]."〈br〉";
echo "檔案類型:".$_FILES["fileupload"]["type"][$j]."〈br〉";
echo "檔案大小:".$_FILES["fileupload"]["size"][$j]."〈br〉";
}else{
echo $_FILES["fileupload"]["tmp_name"][$j]."上傳失敗!〈br /〉";
switch ($_FILES["fileupload"]["error"][$j]){
case 1:
echo "失敗原因:大小超過php.ini內設定 upload_max_filesize"."〈br〉";
break;
case 2:
echo "失敗原因:大小超過表單設定 MAX_FILE_SIZE"."〈br〉";
break;
case 3:
echo "失敗原因:上傳不完整"."〈br〉";
break;
case 4:
echo "失敗原因:沒有檔案上傳"."〈br〉";
break;
case 6:
echo "失敗原因:暫存資料夾不存在"."〈br〉";
break;
case 7:
echo "失敗原因:上傳檔案無法寫入"."〈br〉";
break;
case 8:
echo "失敗原因:上傳停止"."〈br〉";
break;
} } } ?〉
〈/body〉〈/html〉




二、 繪製圖片—graph.php


〈? $height = 200;
$width = 200;
$im = ImageCreate($width, $height);
$white = ImageColorAllocate ($im, 255, 255, 255);
$black = ImageColorAllocate ($im, 0, 0, 0);
ImageFill($im, 0, 0, $black);
ImageLine($im, 0, 0, $width, $height, $white);
ImageString($im, 4, 50, 150, 'Sales', $white);
Header ('Content-type: image/png');
ImagePng ($im);
ImageDestroy($im);?〉



三、 如何呼叫已經產生圖片的網頁? (請自行設計)


〈img src="graph.php" height=200 width=200 alt="顯示php的內容"〉



四、 輸入資料檢驗驗證碼
1. 表單部分


〈html〉 〈head〉〈title〉〈/title〉
〈meta http-equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈/head〉〈body〉〈form action="input.php" method="post"〉
〈input type="text" name="graph" maxlength="7" size="7"〉
〈input type="submit"〉〈/form〉
〈img src="graph.php" width="200" height="200" alt="show image"/〉
〈/body〉〈/html〉



2. 驗證碼部分(graph.php,請修改網頁)


〈? session_start( );
$_SESSION['test']=generatorPassword( );
$height = 200;
$width = 200;
$im = ImageCreate($width, $height);
$white = ImageColorAllocate ($im, 255, 255, 255);
$black = ImageColorAllocate ($im, 0, 0, 0);
ImageFill($im, 0, 0, $black);
ImageLine($im, 0, 0, $width, $height, $white);
ImageString($im, 4, 50, 150, $_SESSION['test'], $white);
Header ('Content-type: image/png');
ImagePng ($im);
ImageDestroy($im);
function generatorPassword( )
{ $password_len = 7;
$password = '';
$word = '0123456789';
$len = strlen($word);
for ($i = 0; $i 〈 $password_len; $i++) {
$password .= $word[rand( ) % $len]; }
return $password; } ?〉



3. 接收資料

〈?session_start( ); ?〉〈html〉〈head〉〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉〈title〉驗證資料〈/title〉〈/head〉〈body〉
〈? if ($_POST['graph']==$_SESSION['test'])
{ ?〉
〈script language="JavaScript"〉
alert("〈? echo "輸入資料正確,轉往google"; ?〉");
location.href="http://www.google.com";
〈/script〉〈? }
else
{ ?〉
〈script language="JavaScript"〉
alert("輸入資料不正確,請重新輸入");
history.back( );
〈/script〉 〈?
} ?〉 〈/body〉〈/html〉




五、 簡單資料操作
1. 新增


〈? ob_start(); ?〉〈html〉〈head〉
〈meta http-equiv="content-type" content="text/html; charset=utf-8"〉
〈title〉新增〈/title〉
〈/head〉〈body〉
〈?
include("config.php");
$account = $_POST['frm_account'];
$username = $_POST['frm_name'];
$sex = $_POST['frm_sex'];
$temp_date = date("Y-m-d H:m:s");

mysql_query("insert into student (account,username,sex,date1) values('$account','$username','$sex','$temp_date') ") or die("error");

header("location:index.php");
?〉〈/body〉〈/html〉



2. 列表

〈html〉〈head〉
〈meta http-equiv="content-type" content="text/html; charset=utf-8"〉
〈title〉表列資料〈/title〉
〈/head〉〈body〉
〈?
include("config.php");
$res =mysql_query("select * from student") or dir("sql error");
?〉〈a href="add.htm"〉新增〈/a〉〈br〉 〈?
while($row=mysql_fetch_array($res)){
echo "姓名:".$row["username"]."帳號:".$row["account"]."性別:";
if($row["sex"] == 1)
echo "男";
else
echo "女";
echo "日期:".$row["date1"];?〉
〈a href="edit.php?id= 〈? echo $row["id"]; ?〉"〉編輯〈/a〉
〈a href="del.php?id= 〈? echo $row["id"]; ?〉"〉刪除〈/a〉
〈br〉 〈?
}
?〉〈/body〉〈/html〉




3. 編輯

〈? ob_start(); ?〉〈html〉〈head〉
〈meta http-equiv="content-type" content="text/html; charset=utf-8"〉
〈title〉編輯〈/title〉〈/head〉〈body〉
〈?
include("config.php");
$id = $_GET['id'];
$res = mysql_query("select * from student where id ='$id'");
$row = mysql_fetch_array($res);
?〉

〈table width="75%" border="0" align="center"〉
〈tr〉
〈td〉〈form action="edit2.php" method="post" name="form1"〉
〈p〉姓名
〈input name="frm_name" type="text" value="〈? echo $row["username"]; ?〉"〉
〈/p〉〈p〉帳號
〈input name="frm_account" type="text" value="〈? echo $row["account"]; ?〉"〉
〈/p〉〈p〉性別
〈input type="radio" name="frm_sex" value="1" 〈? if($row["sex"] == 1) echo "checked"; ?〉〉男
〈input type="radio" name="frm_sex" value="0" 〈? if($row["sex"] == 0) echo "checked"; ?〉〉女
〈/p〉〈p〉
〈input type="submit" name="Submit" value="送出"〉
〈input name="id" type="hidden" value="〈? echo $row["id"]; ?〉"〉
〈/p〉〈/form〉〈/td〉〈/tr〉〈/table〉〈/body〉〈/html〉




〈? ob_start(); ?〉〈html〉〈head〉
〈meta http-equiv="content-type" content="text/html; charset=utf-8"〉
〈title〉編輯〈/title〉
〈/head〉〈body〉
〈?
include("config.php");
$id = $_POST['id'];
//echo $id."〈br〉";
$account = $_POST['frm_account'];
$username = $_POST['frm_name'];
$sex = $_POST['frm_sex'];
$temp_date = date("Y-m-d H:m:s");
$sql="update student set
account = '$account',
username = '$username',
sex = '$sex',
date1 = '$temp_date'
where id ='$id'";
//echo $sql."〈br〉";
//exit;
mysql_query("update student set
account = '$account',
username = '$username',
sex = '$sex',
date1 = '$temp_date'
where id ='$id'") or die("error");
header("location:index.php");
?〉〈/body〉〈/html〉





4. 刪除


〈? ob_start(); ?〉〈html〉〈head〉
〈meta http-equiv="content-type" content="text/html; charset=utf-8"〉
〈title〉刪除〈/title〉〈/head〉〈body〉
〈?
include("config.php");
$id = $_GET['id'];
mysql_query("delete from student where id ='$id'") or die("SQL Error");
header("location:index.php");
?〉
〈/body〉〈/html〉



六、 CSV檔案匯出與匯入
1. 匯入【接收端網頁】


〈? include("server.php"); ?〉〈html〉
〈head〉〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉匯入 CSV 檔〈/title〉〈/head〉〈body〉
〈?
$uploaddir='./';
if ($_FILES["CSVfile"]["name"] ) {
$temp_SQL = "";
$file2=mb_convert_encoding($_FILES["CSVfile"]["name"],"big5","utf8");
if(!move_uploaded_file($_FILES['CSVfile']['tmp_name'],$uploaddir.$file2)){
echo "上傳失敗〈br〉";
}
$fp = fopen($_FILES["CSVfile"]["name"] , "r");
while ( $ROW = fgetcsv($fp,$_FILES["CSVfile"]["size"] ) ) {
if ( strlen($ROW[0]) ) {
if ( strlen($temp_SQL) ) $temp_SQL .= ", ";
$temp_SQL .= "('" . $ROW[0] . "', '" . $ROW[1] . "', '" . $ROW[2] . "', '" .
$ROW[3] . "') ";
}
}
fclose($fp);
$sql = "INSERT INTO student1(sid, username,address,birthday) VALUES " . $temp_SQL;
mysql_query($sql) or die(mysql_error());
}
else {
echo "Error!";
}?〉〈/body〉〈/html〉



2. 匯出


〈?
include("server.php");
$file = 'export';
$result = mysql_query("show columns from student1");
$i = 0;
if (mysql_num_rows($result) 〉 0) {
while ($row = mysql_fetch_assoc($result)) {
$csv_output .= $row['Field'].",";
$i++;
}
}
$csv_output .= "\n";

$values = mysql_query("select * from student1");
while ($rowr = mysql_fetch_row($values)) {
for ($j=0;$j〈$i;$j++) {
$csv_output .= $rowr[$j].", ";
}
$csv_output .= "\n";
};
ini_set("date.timezone","Asia/Taipei");
$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");
print $csv_output;
exit;
?〉



七、 上傳與解壓縮


〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉zip檔案上傳〈/title〉〈/head〉〈body〉〈?
include ('pclzip.lib.php');
$tmp_folder = basename($_FILES['myfile']['name']);
if(is_dir($tmp_folder))
{
echo "資料夾已存在"."〈br〉";
}
else
{
//echo "資料夾不存在"."〈br〉";
if(mkdir($tmp_folder, 0700))
echo "成功建立資料夾"."〈br〉";
else
echo "建立資料夾失敗"."〈br〉";
}
if($_FILES['myfile']['size']〉0){
if(is_uploaded_file($_FILES['myfile']['tmp_name'])&&$_FILES['myfile']['type']=='application/zip'){
if(is_writeable($tmp_folder)){
if(move_uploaded_file($_FILES['myfile']['tmp_name'],$tmp_folder.'/'.$_FILES['myfile']['name'])){
echo '檔案上傳成功!〈p〉';
$hzip = new PclZip($tmp_folder.'/'.$_FILES['myfile']['name']);
if($hzip-〉extract($tmp_folder.'/'))
echo '檔案解壓縮成功!';
else
echo '檔案解壓縮失敗!';
} else
echo '檔案上傳失敗!〈p〉';
} else
echo '檔案無法寫入!';
} else
echo '沒有上傳檔案!';
}
?〉〈/body〉〈/html〉




八、 其他

星期二, 11月 09, 2010

PHP&MySQL--9 2010年11月課程講義預覽

PHP&MySQL--9
一、 補充:資料表正規化
1. 第一正規化


1. 欄位內容必須是單一內容。
2. 同一欄位的內容必須是相同的類型。
3. 必須要有一個或一組主鍵可以辨識資料。
4. 不可用很多欄位來表達同一筆資料。




2. 第二正規化


1.無法單獨新增一筆學生資料。因為 Subject_id是主鍵之一,不能為空值。當學生尚未選修任何課程之前,將無法寫入資料表。  
2.無法單獨刪除一筆成績資料。如果打算刪除科目代表為P1234這個科目資訊,您會把這名學生資訊也刪除,學號99345這位同學資料就此消失。
3.需要同步異動的資料太多。假設學號為99312 這個學生搬家了,那麼我們得異動其中的 6 筆紀錄。



第一正規化之後資料表將產生以下的問題:

第二正規化將要求資料表裡所有的資料必需與該資料表的主鍵有相依關係,以避免資料重複與不一致性。若某欄位只和主鍵的一部份而非全部有關的話,請將這些欄位獨立成另外一個資料表。

3. 第三正規化


1.無法單獨新增一筆縣市資料。因為 Stu_no 是主鍵,不能是空值;因此,若無任何學生居住的某個縣市,其郵遞區號資料將無法被事先建立。  
2.無法單獨刪除一筆學生資料。如果我們打算刪除 99524 這筆資料的話,新北市郵遞區號資料也將一併消失。
3.更新郵遞區號困難。假設有兩位同學同住在台中市清水區,若郵遞區號變更,則要同時異動多筆紀錄。



第三正規化將要求資料表內刪除不依賴主鍵的欄位,也就是不應該存在遞移相依性欄位,將非主鍵決定的欄位獨立為新資料表。

二、 人數統計分頁分析【提供display1.php,並請修改網頁內容】


〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉人數統計分析〈/title〉〈/head〉
〈body〉〈?
include("server.php");
if( isset($_GET['page']) )
$page = intval( $_GET['page'] );
else{$page = 1;}
$page_size = 10;
$sql = "select count(*) as total from record";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
$total = $row['total'];
if(isset($total))
{
if( $total 〈 $page_size )
$page_count = 1;
if( $total % $page_size )
$page_count = (int)($total / $page_size) + 1;
else
$page_count = $total / $page_size;
}
else
$page_count = 0;
$page_number = '';
if( $page == 1 )
$page_number .= '[第一頁][上一頁] ';
else {
$page_number.='[〈a href='.$_SERVER['PHP_SELF'].'?page=1〉第一頁〈/a〉]';
$page_number.='[〈a href='.$_SERVER['PHP_SELF'].'?page='.($page-1).'〉'."上一頁〈/a〉]";
}
if(($page == $page_count) || ($page_count == 0))
$page_number .= '[下一頁][尾頁]';
else {
$page_number.='[〈a href='.$_SERVER['PHP_SELF'].'?page='.($page+1).'〉'."下一頁〈/a〉]";
$page_number.='[〈a href='.$_SERVER['PHP_SELF'].'?page='.$page_count.'〉'."尾頁〈/a〉]";
}
if(isset($total)) {
$sql = "select * from record limit ". ($page-1)*$page_size .", $page_size";
$result = mysql_query($sql);
echo "〈table border=1〉";
echo "〈tr〉〈td〉時間〈/td〉〈td〉ip〈/td〉〈/tr〉";
while($list1=mysql_fetch_array($result))
{
echo "〈tr〉〈td〉".$list1['visitday']."〈/td〉";
echo "〈td〉".$list1['ip']."〈/td〉〈/tr〉"; }
echo "〈/table〉";
echo $page_number; }
?〉〈/body〉〈/html〉



三、 檔案上傳
1. 表單網頁【提供upload.htm】


〈html〉〈head〉〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉〈title〉檔案上載〈/title〉〈/head〉〈body〉
〈form action="upload.php" method="post" enctype="multipart/form-data"〉
〈input type="hidden" name="MAX_FILE_SIZE" value="1000000"〉
Send this file: 〈input name="userfile" type="file"〉
〈input type="submit" value="Send File"〉
〈/form〉〈/body〉〈/html〉



2. 接收端網頁【提供upload.php,並請修改網頁內容】


〈html〉
〈head〉〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉檔案上傳:接收端網頁〈/title〉〈/head〉〈body〉〈?
$uploaddir='./tmp/';
print "〈pre〉";
$file2=mb_convert_encoding($_FILES["userfile"]["name"],"big5","utf8");
if(move_uploaded_file($_FILES['userfile']['tmp_name'], $uploaddir.$file2))
{ echo "檔名:".$_FILES['userfile']['name']."〈br〉" ;
echo "大小:".$_FILES['userfile']['size']."〈br〉" ;
echo "型態:".$_FILES['userfile']['type']."〈br〉" ; }
else
{
print_r ($_FILES);
$a=$_FILES['userfile']['error'];
echo "a-〉".$a;
if ($a==1)
{ die("檔案大小超出 php.ini:upload_max_filesize 限制"); }
if($a==2)
{ die("檔案大小超出 MAX_FILE_SIZE 限制"); } }
echo $a;
?〉〈/body〉〈/html〉



四、 header( )進階使用
1. 開啟jpg圖檔【提供jpg1.php,並請修改網頁內容】


〈?
$im = imagecreatefromjpeg("test.jpg");
header('Content-type: image/jpg');
imagejpeg($im);
imagedestroy($im); ?〉



2. 與上例有關,增加ip限制【提供lockip.php,並請修改網頁內容】


〈?
function allow_ip($addr)
{
$first = substr($addr,0,(strrpos($addr,".")));
$second = substr($addr,(strrpos($addr,".")+1));
if(($first=="140.111.144") && ($second〉=1 && $second 〈=255))
{ return true; }
else
{ return false; }
}?〉



3. ip限制才能看圖【請自行撰寫】


〈?
require("lockip.php");
$addr_ip=$_SERVER['REMOTE_ADDR'];
$flag=allow_ip($addr_ip);
if(!$flag)
{die("不能看圖,IP位置不合法");}
include('./jpg1.php'); ?〉



4. 連結PHP檔案瀏覽圖片 【提供jpg3.php,並請修改網頁內容】

〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8" /〉
〈title〉檢測縮圖並做連結〈/title〉〈/head〉〈body〉
按下連結可察看原圖〈br〉
〈a href="jpg2.php"〉
〈img src = "jpg1.php" alt="看不到" width="100" height="90" border="0"〉〈/a〉
〈/body〉〈/html〉




五、 開啟網頁前的認證【提供auth1.php,並請修改網頁內容】

〈? header("Content-type: text/html; charset=utf-8");
if (empty($_SERVER['PHP_AUTH_USER'])) {
header('WWW-Authenticate: Basic realm="Please input"');
header('HTTP/1.0 401 Unauthorized');
echo '請輸入正確的帳號及密碼, 不可以取消!';
exit;
} else {
$correctName="pcschool";
$correctpwd="mysql" ;
if (($_SERVER['PHP_AUTH_USER'] != $correctName) or
($_SERVER['PHP_AUTH_PW'] !=$correctpwd)){
echo "登入失敗,請開啟新的瀏覽器重新登入"; }} ?〉




六、 下載的檔案與實際的檔案不同【請自行撰寫】


〈?php
include("auth1.php"); //可不加上此行
header("Content-type: text/html; charset=utf-8");
$file="./9707.zip"; // 實際檔案的路徑+檔名
$filename="0714.zip"; // 下載的檔名
header("Content-type: ".filetype("$file"));//指定類型
header("Content-Disposition: attachment; filename=".$filename."");
readfile($file); ?〉



七、 上傳多個檔案【提供表單網頁upload4.htm及接收資料網頁upload4.php,再請修改】


〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉處理上傳多個檔案〈/title〉〈/head〉 〈body〉〈?
$uploaddir='./upload/';
$i=count($_FILES["fileupload"]["name"]);
for ($j=0;$j〈$i;$j++){
$tmpfile=$_FILES["fileupload"]["tmp_name"][$j];
$file2=mb_convert_encoding($_FILES["fileupload"]["name"][$j],"big5","utf8");
if(move_uploaded_file($tmpfile,$uploaddir.$file2)){
echo "上傳成功〈br〉";
echo "檔案名稱:".$_FILES["fileupload"]["name"][$j]."〈br〉";
echo "檔案類型:".$_FILES["fileupload"]["type"][$j]."〈br〉";
echo "檔案大小:".$_FILES["fileupload"]["size"][$j]."〈br〉";
}else{
echo $_FILES["fileupload"]["tmp_name"][$j]."上傳失敗!〈br /〉";
switch ($_FILES["fileupload"]["error"][$j]){
case 1:
echo "失敗原因:大小超過php.ini內設定 upload_max_filesize"."〈br〉";
break;
case 2:
echo "失敗原因:大小超過表單設定 MAX_FILE_SIZE"."〈br〉";
break;
case 3:
echo "失敗原因:上傳不完整"."〈br〉";
break;
case 4:
echo "失敗原因:沒有檔案上傳"."〈br〉";
break;
case 6:
echo "失敗原因:暫存資料夾不存在"."〈br〉";
break;
case 7:
echo "失敗原因:上傳檔案無法寫入"."〈br〉";
break;
case 8:
echo "失敗原因:上傳停止"."〈br〉";
break;
} } } ?〉
〈/body〉〈/html〉




八、 繪製圖片—graph.php


〈? $height = 200;
$width = 200;
$im = ImageCreate($width, $height);
$white = ImageColorAllocate ($im, 255, 255, 255);
$black = ImageColorAllocate ($im, 0, 0, 0);
ImageFill($im, 0, 0, $black);
ImageLine($im, 0, 0, $width, $height, $white);
ImageString($im, 4, 50, 150, 'Sales', $white);
Header ('Content-type: image/png');
ImagePng ($im);
ImageDestroy($im);?〉



九、 如何呼叫已經產生圖片的網頁? (請自行設計)



〈img src="graph.php" height=200 width=200 alt="顯示php的內容"〉




十、 輸入資料檢驗驗證碼
1. 表單部分


〈html〉 〈head〉〈title〉〈/title〉
〈meta http-equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈/head〉〈body〉〈form action="input.php" method="post"〉
〈input type="text" name="graph" maxlength="7" size="7"〉
〈input type="submit"〉〈/form〉
〈img src="graph.php" width="200" height="200" alt="show image"/〉
〈/body〉〈/html〉



2. 驗證碼部分(graph.php,請修改網頁)

〈? session_start( );
$_SESSION['test']=generatorPassword( );
$height = 200;
$width = 200;
$im = ImageCreate($width, $height);
$white = ImageColorAllocate ($im, 255, 255, 255);
$black = ImageColorAllocate ($im, 0, 0, 0);
ImageFill($im, 0, 0, $black);
ImageLine($im, 0, 0, $width, $height, $white);
ImageString($im, 4, 50, 150, $_SESSION['test'], $white);
Header ('Content-type: image/png');
ImagePng ($im);
ImageDestroy($im);
function generatorPassword( )
{ $password_len = 7;
$password = '';
$word = '0123456789';
$len = strlen($word);
for ($i = 0; $i 〈 $password_len; $i++) {
$password .= $word[rand( ) % $len]; }
return $password; } ?〉




3. 接收資料


〈?session_start( ); ?〉〈html〉〈head〉〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉〈title〉驗證資料〈/title〉〈/head〉〈body〉
〈? if ($_POST['graph']==$_SESSION['test'])
{ ?〉
〈script language="JavaScript"〉
alert("〈? echo "輸入資料正確,轉往google"; ?〉");
location.href="http://www.google.com";
〈/script〉〈? }
else
{ ?〉
〈script language="JavaScript"〉
alert("輸入資料不正確,請重新輸入");
history.back( );
〈/script〉 〈?
} ?〉 〈/body〉〈/html〉




十一、 簡單資料操作
1. 新增

〈? ob_start(); ?〉〈html〉〈head〉
〈meta http-equiv="content-type" content="text/html; charset=utf-8"〉
〈title〉新增〈/title〉
〈/head〉〈body〉
〈?
include("config.php");
$account = $_POST['frm_account'];
$username = $_POST['frm_name'];
$sex = $_POST['frm_sex'];
$temp_date = date("Y-m-d H:m:s");

mysql_query("insert into student (account,username,sex,date1) values('$account','$username','$sex','$temp_date') ") or die("error");

header("location:index.php");
?〉〈/body〉〈/html〉




2. 列表


〈html〉〈head〉
〈meta http-equiv="content-type" content="text/html; charset=utf-8"〉
〈title〉表列資料〈/title〉
〈/head〉〈body〉
〈?
include("config.php");
$res =mysql_query("select * from student") or dir("sql error");
?〉〈a href="add.htm"〉新增〈/a〉〈br〉 〈?
while($row=mysql_fetch_array($res)){
echo "姓名:".$row["username"]."帳號:".$row["account"]."性別:";
if($row["sex"] == 1)
echo "男";
else
echo "女";
echo "日期:".$row["date1"];?〉
〈a href="edit.php?id= 〈? echo $row["id"]; ?〉"〉編輯〈/a〉
〈a href="del.php?id= 〈? echo $row["id"]; ?〉"〉刪除〈/a〉
〈br〉 〈?
}
?〉〈/body〉〈/html〉





3. 編輯


〈? ob_start(); ?〉〈html〉〈head〉
〈meta http-equiv="content-type" content="text/html; charset=utf-8"〉
〈title〉編輯〈/title〉〈/head〉〈body〉
〈?
include("config.php");
$id = $_GET['id'];
$res = mysql_query("select * from student where id ='$id'");
$row = mysql_fetch_array($res);
?〉
〈table width="75%" border="0" align="center"〉
〈tr〉
〈td〉〈form action="edit2.php" method="post" name="form1"〉
〈p〉姓名
〈input name="frm_name" type="text" value="〈? echo $row["username"]; ?〉"〉
〈/p〉〈p〉帳號
〈input name="frm_account" type="text" value="〈? echo $row["account"]; ?〉"〉
〈/p〉〈p〉性別
〈input type="radio" name="frm_sex" value="1" 〈? if($row["sex"] == 1) echo "checked"; ?〉〉男
〈input type="radio" name="frm_sex" value="0" 〈? if($row["sex"] == 0) echo "checked"; ?〉〉女
〈/p〉〈p〉
〈input type="submit" name="Submit" value="送出"〉
〈input name="id" type="hidden" value="〈? echo $row["id"]; ?〉"〉
〈/p〉〈/form〉〈/td〉〈/tr〉〈/table〉〈/body〉〈/html〉






〈? ob_start(); ?〉〈html〉〈head〉
〈meta http-equiv="content-type" content="text/html; charset=utf-8"〉
〈title〉編輯〈/title〉
〈/head〉〈body〉
〈?
include("config.php");
$id = $_POST['id'];
//echo $id."〈br〉";
$account = $_POST['frm_account'];
$username = $_POST['frm_name'];
$sex = $_POST['frm_sex'];
$temp_date = date("Y-m-d H:m:s");
$sql="update student set
account = '$account',
username = '$username',
sex = '$sex',
date1 = '$temp_date'
where id ='$id'";
//echo $sql."〈br〉";
//exit;
mysql_query("update student set
account = '$account',
username = '$username',
sex = '$sex',
date1 = '$temp_date'
where id ='$id'") or die("error");
header("location:index.php");
?〉〈/body〉〈/html〉




4. 刪除


〈? ob_start(); ?〉〈html〉〈head〉
〈meta http-equiv="content-type" content="text/html; charset=utf-8"〉
〈title〉刪除〈/title〉〈/head〉〈body〉
〈?
include("config.php");
$id = $_GET['id'];
mysql_query("delete from student where id ='$id'") or die("SQL Error");
header("location:index.php");
?〉
〈/body〉〈/html〉



十二、 其他

星期五, 11月 05, 2010

PHP&MySQL--8 2010年11月課程講義預覽

PHP&MySQL--8
一、 表單送資料至PHP後查詢資料【提供表單網頁】
1. 練習SQL語法中where語法

select employeeid,firstname from employees where (employeeid 〉= 3);
select employeeid,firstname from employees where (employeeid in(2,4,9));
select employeeid,firstname from employees where (employeeid not in(2,4,9));
select country,companyname from customers where (country in('Argentina','Mexico'));

select region,companyname from customers where (region is null);
select region,companyname from customers where (region is not null);
select firstname from employees where (firstname like 'a%');
select firstname from employees where (firstname like '%a%');
select firstname from employees where (firstname like '%a');
select companyname,contactname,country from customers where (country in('France','Germany') and companyname like 'b%');

select companyname,contactname,country from customers where (country in('France','Germany') or companyname like 'b%');








2. 若直接將post傳送過來的資料送至sql語法?【提供query03.php】


〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈title〉接收資料〈/title〉〈/head〉〈body〉
〈?
include("server.php");
$sql="SELECT * FROM orders where CustomerID=".$_POST['text1'];
$sql2=mysql_query($sql) or die(mysql_error());
echo mysql_num_rows($sql2)."〈br〉";
while($list1= mysql_fetch_row($sql2))
{
echo $list1[0]." ".$list1[1]."〈br〉";
}
?〉〈/body〉〈/html〉




3. 上例修改【請依照講義修改query03.php】


//$sql="SELECT * FROM orders where CustomerID=".$_POST['text1'];
$sql="SELECT * FROM orders where CustomerID='".$_POST['text1']."'";



4. 上例修改【請依照講義修改query03.php】


//$sql="SELECT * FROM orders where CustomerID=".$_POST['text1'];
//$sql="SELECT * FROM orders where CustomerID='".$_POST['text1']."'";
$a=$_POST['text1'];
$sql="SELECT * FROM orders where CustomerID='$a'";



5. 測試各種模糊查詢【提供query04.php,但請修改表單網頁路徑】


〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈title〉接收資料〈/title〉
〈/head〉〈body〉
〈?php
include("server.php");
//測試各種模糊查詢
$sql="SELECT * FROM orders where CustomerID like '%$a%'";
$sql="SELECT * FROM orders where CustomerID like '$a%'";
$sql="SELECT * FROM orders where CustomerID like '%$a'";
$sql2=mysql_query($sql) or die(mysql_error());
echo mysql_num_rows($sql2)."〈br〉";
while($list1= mysql_fetch_row($sql2))
{
echo $list1[0]." ".$list1[1]."〈br〉";
}
?〉〈/body〉〈/html〉



二、 傳遞參數給SQL語法:例如查詢customers筆數與範圍(提供表單)
【請自行撰寫】


〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉回傳各欄位資料〈/title〉〈/head〉〈body〉〈?
@mysql_connect("localhost", "pcschool", "phpmysql") or die("無法連結主機");
mysql_select_db("pcschool") or die("無法連結資料庫");
include("unicode.php");
echo $_SERVER['HTTP_REFERER']."〈br〉";
//$sql1="select * from customers limit ".$_POST['first'].",".$_POST['total'];
//$sql1="select * from customers limit $_POST['first'],$_POST['total']";

$a=$_POST['first'];
$b=$_POST['total'];
$sql1="select * from customers limit $a,$b";
//echo $sql1;
//$sql = @mysql_query($sql1);
$sql = mysql_query($sql1) or die(mysql_error());
$rows = @mysql_num_rows($sql);
//echo $rows;
if($rows==""){
echo "查無資料!";
}else{
echo "有 ".$rows." 筆資料喔!";
}
?〉〈/body〉〈/html〉



限制筆數之SQL語法:

select employeeid,firstname, lastname from employees ;
select employeeid,firstname, lastname from employees limit 1,30;
select employeeid,firstname, lastname from employees limit 0,5;
select employeeid,firstname, lastname from employees limit 6;

select productid,productname,unitprice from products order by unitprice desc limit 1;

select employeeid,firstname, lastname from employees order by rand() limit 6;




三、 人數統計分頁分析【提供display1.php,並請修改網頁內容】



〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉人數統計分析〈/title〉〈/head〉
〈body〉〈?
include("server.php");
if( isset($_GET['page']) )
$page = intval( $_GET['page'] );
else{$page = 1;}
$page_size = 10;
$sql = "select count(*) as total from record";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
$total = $row['total'];
if(isset($total))
{
if( $total 〈 $page_size )
$page_count = 1;
if( $total % $page_size )
$page_count = (int)($total / $page_size) + 1;
else
$page_count = $total / $page_size;
}
else
$page_count = 0;
$page_number = '';
if( $page == 1 )
$page_number .= '[第一頁][上一頁] ';
else {
$page_number.='[〈a href='.$_SERVER['PHP_SELF'].'?page=1〉第一頁〈/a〉]';
$page_number.='[〈a href='.$_SERVER['PHP_SELF'].'?page='.($page-1).'〉'."上一頁〈/a〉]";
}
if(($page == $page_count) || ($page_count == 0))
$page_number .= '[下一頁][尾頁]';
else {
$page_number.='[〈a href='.$_SERVER['PHP_SELF'].'?page='.($page+1).'〉'."下一頁〈/a〉]";
$page_number.='[〈a href='.$_SERVER['PHP_SELF'].'?page='.$page_count.'〉'."尾頁〈/a〉]";
}
if(isset($total)) {
$sql = "select * from record limit ". ($page-1)*$page_size .", $page_size";
$result = mysql_query($sql);
echo "〈table border=1〉";
echo "〈tr〉〈td〉時間〈/td〉〈td〉ip〈/td〉〈/tr〉";
while($list1=mysql_fetch_array($result))
{
echo "〈tr〉〈td〉".$list1['visitday']."〈/td〉";
echo "〈td〉".$list1['ip']."〈/td〉〈/tr〉"; }
echo "〈/table〉";
echo $page_number; }
?〉〈/body〉〈/html〉



四、 檔案上傳
1. 表單網頁【提供upload.htm】


〈html〉〈head〉〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉〈title〉檔案上載〈/title〉〈/head〉〈body〉
〈form action="upload.php" method="post" enctype="multipart/form-data"〉
〈input type="hidden" name="MAX_FILE_SIZE" value="1000000"〉
Send this file: 〈input name="userfile" type="file"〉
〈input type="submit" value="Send File"〉
〈/form〉〈/body〉〈/html〉



2. 接收端網頁【提供upload.php,並請修改網頁內容】

〈html〉
〈head〉〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉檔案上傳:接收端網頁〈/title〉〈/head〉〈body〉〈?
$uploaddir='./tmp/';
print "〈pre〉";
$file2=mb_convert_encoding($_FILES["userfile"]["name"],"big5","utf8");
if(move_uploaded_file($_FILES['userfile']['tmp_name'], $uploaddir.$file2))
{ echo "檔名:".$_FILES['userfile']['name']."〈br〉" ;
echo "大小:".$_FILES['userfile']['size']."〈br〉" ;
echo "型態:".$_FILES['userfile']['type']."〈br〉" ; }
else
{
print_r ($_FILES);
$a=$_FILES['userfile']['error'];
echo "a-〉".$a;
if ($a==1)
{ die("檔案大小超出 php.ini:upload_max_filesize 限制"); }
if($a==2)
{ die("檔案大小超出 MAX_FILE_SIZE 限制"); } }
echo $a;
?〉〈/body〉〈/html〉



五、 header( )進階使用
1. 開啟jpg圖檔【提供jpg1.php,並請修改網頁內容】


〈?
$im = imagecreatefromjpeg("test.jpg");
header('Content-type: image/jpg');
imagejpeg($im);
imagedestroy($im); ?〉




2. 與上例有關,增加ip限制【提供lockip.php,並請修改網頁內容】



〈?
function allow_ip($addr)
{
$first = substr($addr,0,(strrpos($addr,".")));
$second = substr($addr,(strrpos($addr,".")+1));
if(($first=="140.111.144") && ($second〉=1 && $second 〈=255))
{ return true; }
else
{ return false; }
}?〉



3. ip限制才能看圖【請自行撰寫】


〈?
require("lockip.php");
$addr_ip=$_SERVER['REMOTE_ADDR'];
$flag=allow_ip($addr_ip);
if(!$flag)
{die("不能看圖,IP位置不合法");}
include('./jpg1.php'); ?〉



4. 連結PHP檔案瀏覽圖片 【提供jpg3.php,並請修改網頁內容】


〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8" /〉
〈title〉檢測縮圖並做連結〈/title〉〈/head〉〈body〉
按下連結可察看原圖〈br〉
〈a href="jpg2.php"〉
〈img src = "jpg1.php" alt="看不到" width="100" height="90" border="0"〉〈/a〉
〈/body〉〈/html〉



六、 開啟網頁前的認證【提供auth1.php,並請修改網頁內容】

〈? header("Content-type: text/html; charset=utf-8");
if (empty($_SERVER['PHP_AUTH_USER'])) {
header('WWW-Authenticate: Basic realm="Please input"');
header('HTTP/1.0 401 Unauthorized');
echo '請輸入正確的帳號及密碼, 不可以取消!';
exit;
} else {
$correctName="pcschool";
$correctpwd="mysql" ;
if (($_SERVER['PHP_AUTH_USER'] != $correctName) or
($_SERVER['PHP_AUTH_PW'] !=$correctpwd)){
echo "登入失敗,請開啟新的瀏覽器重新登入"; }} ?〉




七、 下載的檔案與實際的檔案不同【請自行撰寫】


〈?php
include("auth1.php"); //可不加上此行
header("Content-type: text/html; charset=utf-8");
$file="./9707.zip"; // 實際檔案的路徑+檔名
$filename="0714.zip"; // 下載的檔名
header("Content-type: ".filetype("$file"));//指定類型
header("Content-Disposition: attachment; filename=".$filename."");
readfile($file); ?〉




八、 其它

星期三, 11月 03, 2010

2010年11月:東區認證中心VB.Net 物件導向課程

這次課程將跟大家說明物件導向的封裝、繼承與多形等觀念。
程式設計採視窗程式設計(Windows Form),所以與坊間書籍於終端機環境
設計有很大的不同,但觀念相同。
課程中會使用講義,講義用來控制課程進度與課程重點提示,
課本請各位帶回家,於家中做練習之用。
如對課程內容有疑問..歡迎與我聯繫與討論。

PHP&MySQL--7 2010年10月課程講義說明

因為第六次上課對於MySQL新增使用者說明與操作的時間較久,所以
第七次上課講義同第六次上課第二頁之後的講義。

星期五, 10月 29, 2010

PHP&MySQL--6 2010年10月課程講義預覽

PHP&MySQL--6
一、 新增帳號


grant all privileges on *.* to pcschool@localhost identified by 'phpmysql';

grant all privileges on board.* to php1@localhost identified by 'mysqlstart1';

grant select on board2.* to php3@localhost identified by 'mysqlstart3';



二、 PHP連結資料庫語法(db.php,資料庫為pcschool)


〈html〉〈head〉〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉資料庫連線〈/title〉〈/head〉〈body〉〈?
$link = @mysql_connect("localhost", "pcschool", "phpmysql");
$link_db = @mysql_select_db("pcschool123446");
if($link) { echo "主機連結OK!....";
if($link_db) { echo "資料庫連結OK!...."; }
if(!$link_db) { echo "資料庫連結失敗!....."; }}
if(!$link) { die( "連結主機失敗!....");}
echo "Hello"; ?〉 〈/body〉〈/html〉



1. 問題:如果mysql_connect出錯,會如何?
2. 問題:如果mysql_selsect_db出錯,會如何?
3. 問題:在發生錯誤之後,可否在顯示訊息後跳出網頁?
三、 mysql編碼與傳輸:unicode.php


〈?
mysql_query("SET NAMES utf8");
mysql_query("CHARACTER SET utf8");
mysql_query("SET CHARACTER_SET_CLIENT =utf8");
mysql_query("SET COLLATION_CONNECTION=utf8_general_ci");
mysql_query("SET CHARACTER_SET_RESULTS =utf8");
mysql_query("SET CHARACTER_SET_SERVER = utf8");
mysql_query("SET character_set_connection=utf8"); ?〉



四、 PHP函數--送出執行語法:mysql_query("SQL 語法")
【提供mysql_query1.php】


〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉mysql_query〈/title〉〈/head〉〈body〉〈?
mysql_connect("localhost", "pcschool", "phpmysql") or die("無法連結主機");
mysql_select_db("pcschool") or die("無法連結資料庫");
//include("unicode.php");
//以下新增語法請做練習
//以下兩行為一行
//$sql = "insert into list (username, email, sex) values('pcschool', 'quota123@ms14.url.com.tw', '男')";
//$sql = "insert into list (email, sex) values('quota123@ms14.url.com.tw', '男')";
//$sql = "insert into list values('quota123@ms14.url.com.tw','pcschool','男')";
$sql = "insert into list values('','pcschool','男')";
mysql_query($sql) or die(mysql_error( ) );
?〉〈/body〉〈/html〉



若沒有加入unicode.php,MySQL內的資料會如何呢?
若資料表名稱錯誤或儲存的欄位數量不對,系統會如何回應呢?
【提供mysql_query01.php】


〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈title〉以下的Query是否有錯?〈/title〉〈/head〉〈body〉
〈?php
include("server.php");
$a='test';
$b='test2';
$sql="INSERT INTO orders(CustomerID,EmployeeID)VALUES ($a,$b);";
mysql_query($sql);
//mysql_query($sql) or die(mysql_error( ) );
//echo $sql;
?〉〈/body〉〈/html〉



若兩個query一起執行,但其中一個有問題會如何呢?【提供mysql_query02.php】

〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈title〉兩個Query執行會如何?〈/title〉〈/head〉〈body〉
〈?php
include("server.php");
$sql="INSERT INTO orders(CustomerID,EmployeeID)VALUES ('er', '23');";
mysql_query($sql);
$a='test';
$b='test2';
$sql="INSERT INTO orders(CustomerID,EmployeeID)VALUES ($a,$b);";
mysql_query($sql);
//mysql_query($sql) or die(mysql_error( ) );
//echo $sql;//得到與mysql_error( )
?〉〈/body〉〈/html〉




上述兩個練習請修正【修改為mysql_query03.php】:

$a='test';
$b='test2';
$sql="INSERT INTO orders(CustomerID,EmployeeID)VALUES ('$a',$b);";




複製資料表語法


create table insertemployees select * from employees;
create table insertjob select * from job;
create table updateemployees1 select * from employees;
create table updateemployees2 select * from employees;
create table updateemployees3 select * from employees;
create table updatejob select * from job;
create table updateproducts select * from products;
create table delemployees1 select * from employees;
create table delemployees2 select * from employees;
create table delemployees3 select * from employees;
create table deljob select * from job;



新增資料語法


insert into insertjob values (12);

insert into insertjob values ('manager',12);
select * from insertjob;

insert into insertjob values (12,'manager');
select * from insertjob;

insert into employees(lastname,firstname,employeeid) values ('jiannrong','yeh',2009);」
select lastname,firstname,employeeid from employees where employeeid=2009;

insert into insertjob(employeeid,title) select employeeid ,concat(firstname,"-",lastname) from employees;
select * from insertjob



五、 資料查詢語法
1.基本查詢


select * from employees;
select firstname, lastname from employees;
select city from employees;
select distinct city from employees;



2.As的使用


select firstname as f, lastname as l from employees;

select productname, unitprice ,unitsinstock ,unitprice * unitsinstock as total from products;

select firstname,hiredate,curdate( ) as nowdate,(year(curdate( ) )-year(hiredate)) as years from employees;

select firstname,lastname,concat(firstname, "-- ",lastname) as yourname from employees;



3.排序


select firstname, lastname, hiredate from employees order by firstname;
select firstname, lastname, hiredate from employees order by firstname desc;
select firstname, lastname, hiredate from employees order by firstname asc;
select firstname, lastname from employees order by hiredate desc;
select firstname, lastname from employees order by firstname desc,lastname asc;
select firstname, lastname from employees order by lastname asc ,firstname desc;



六、 PHP函數-- mysql_num_rows( ) 計算查詢後的筆數【請自行撰寫】


〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉計算筆數〈/title〉〈/head〉〈body〉〈?
@mysql_connect("localhost", "pcschool", "phpmysql") or die("無法連結主機");
@mysql_select_db(“pcschool”) or die("無法連結資料庫");
include("unicode.php");
$abc="select username from list";
echo $abc."〈br〉";
$sql = mysql_query($abc) or die(mysql_error( ) );;
$rows = mysql_num_rows($sql);
if($rows==""){
echo "查無資料!";
}else{ echo "有 ".$rows." 筆資料喔!";}
?〉〈/body〉〈/html〉



七、 回傳欄位資料【請依照講義修改mysql_query04.php】


〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈title〉顯示記錄〈/title〉〈/head〉〈body〉
〈?php
include("server.php");
$sql="SELECT * FROM orders";
$sql2=mysql_query($sql) or die(mysql_error( ) );
echo $sql2;
$list1= mysql_fetch_array($sql2);
echo $list1['OrderID'];
?〉
〈/body〉〈/html〉



請修改上例,觀察顯示內容【請將mysql_query04.php另存新檔後依講義修改】

$list1= mysql_fetch_array($sql2);
echo $list1['OrderID']."〈br〉";
echo $list1['OrderID']."〈br〉";
$list1= mysql_fetch_array($sql2);
echo $list1['OrderID']."〈br〉";




八、 顯示資料【以array方式設計】 【請依照講義修改mysql_query06.php】


〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈title〉顯示所有資料〈/title〉〈/head〉〈body〉
〈?
include("server.php");
$sql="SELECT * FROM orders";
$sql2=mysql_query($sql) or die(mysql_error( ) );
while($list1= mysql_fetch_array($sql2))
{
echo $list1['OrderID']." ".$list1['OrderDate']."〈br〉";
}
?〉〈/body〉〈/html〉



九、 表單送資料至PHP後查詢資料【提供表單網頁】
1. 練習SQL語法中where語法

select employeeid,firstname from employees where (employeeid 〉= 3);
select employeeid,firstname from employees where (employeeid in(2,4,9));
select employeeid,firstname from employees where (employeeid not in(2,4,9));
select country,companyname from customers where (country in('Argentina','Mexico'));

select region,companyname from customers where (region is null);
select region,companyname from customers where (region is not null);
select firstname from employees where (firstname like 'a%');
select firstname from employees where (firstname like '%a%');
select firstname from employees where (firstname like '%a');
select companyname,contactname,country from customers where (country in('France','Germany') and companyname like 'b%');

select companyname,contactname,country from customers where (country in('France','Germany') or companyname like 'b%');




2. 若直接將post傳送過來的資料送至sql語法?【提供query03.php】


〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈title〉接收資料〈/title〉〈/head〉〈body〉
〈?
include("server.php");
$sql="SELECT * FROM orders where CustomerID=".$_POST['text1'];
$sql2=mysql_query($sql) or die(mysql_error());
echo mysql_num_rows($sql2)."〈br〉";
while($list1= mysql_fetch_row($sql2))
{
echo $list1[0]." ".$list1[1]."〈br〉";
}
?〉〈/body〉〈/html〉



3. 上例修改【請依照講義修改query03.php】


//$sql="SELECT * FROM orders where CustomerID=".$_POST['text1'];
$sql="SELECT * FROM orders where CustomerID='".$_POST['text1']."'";



4. 上例修改【請依照講義修改query03.php】


//$sql="SELECT * FROM orders where CustomerID=".$_POST['text1'];
//$sql="SELECT * FROM orders where CustomerID='".$_POST['text1']."'";
$a=$_POST['text1'];
$sql="SELECT * FROM orders where CustomerID='$a'";



5. 測試各種模糊查詢【提供query04.php,但請修改表單網頁路徑】


〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈title〉接收資料〈/title〉
〈/head〉〈body〉
〈?php
include("server.php");
//測試各種模糊查詢
$sql="SELECT * FROM orders where CustomerID like '%$a%'";
$sql="SELECT * FROM orders where CustomerID like '$a%'";
$sql="SELECT * FROM orders where CustomerID like '%$a'";
$sql2=mysql_query($sql) or die(mysql_error());
echo mysql_num_rows($sql2)."〈br〉";
while($list1= mysql_fetch_row($sql2))
{
echo $list1[0]." ".$list1[1]."〈br〉";
}
?〉〈/body〉〈/html〉



十、 傳遞參數給SQL語法:例如查詢customers筆數與範圍(提供表單)
【請自行撰寫】


〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉回傳各欄位資料〈/title〉〈/head〉〈body〉〈?
@mysql_connect("localhost", "pcschool", "phpmysql") or die("無法連結主機");
mysql_select_db("pcschool") or die("無法連結資料庫");
include("unicode.php");
echo $_SERVER['HTTP_REFERER']."〈br〉";
//$sql1="select * from customers limit ".$_POST['first'].",".$_POST['total'];
//$sql1="select * from customers limit $_POST['first'],$_POST['total']";

$a=$_POST['first'];
$b=$_POST['total'];
$sql1="select * from customers limit $a,$b";
//echo $sql1;
//$sql = @mysql_query($sql1);
$sql = mysql_query($sql1) or die(mysql_error());
$rows = @mysql_num_rows($sql);
//echo $rows;
if($rows==""){
echo "查無資料!";
}else{
echo "有 ".$rows." 筆資料喔!";
}
?〉〈/body〉〈/html〉



限制筆數之SQL語法:


select employeeid,firstname, lastname from employees ;
select employeeid,firstname, lastname from employees limit 1,30;
select employeeid,firstname, lastname from employees limit 0,5;
select employeeid,firstname, lastname from employees limit 6;

select productid,productname,unitprice from products order by unitprice desc limit 1;

select employeeid,firstname, lastname from employees order by rand() limit 6;



十一、 其他

星期三, 10月 27, 2010

PHP&MySQL--5 2010年10月課程講義預覽

PHP&MySQL--5
一、 資料庫是什麼?
二、 若忘記root密碼:【請參考網路硬碟提供的指令依序進行密碼設定】
三、 一些MySQL指令
說明 指令
顯示目前資料庫 show databases;
檢視資料表:例如檢視mysql資料庫內的資料表 【請先使用資料庫】 show tables from mysql;
顯示資料表 【請先使用資料庫】 show tables;
檢視欄位 【請先使用資料庫】 show columns from db from mysql;
建立資料庫 create database pcschooldb;
刪除資料庫 drop database pcschooldb;
使用資料庫 use test;
四、 資料庫設計
五、 什麼是SQL檔案
六、 利用phpMyAdmin建pcschool資料庫,將student_create_table.sql匯入
七、 將student.sql匯入,請觀察匯入的結果。【student.sql檔案為big5編碼】
八、 利用phpMyAdmin建立pcschool2資料庫,但校對請選擇「Big5」,並請您將student.sql匯入,請觀察匯入的結果。【student.sql檔案為big5編碼】
九、 談資料表索引
十、 資料的匯出:mysqldump

mysqldump –uroot -pphpmysql pcschool 〉pcschool.sql

mysqldump -uroot -pphpmysql --skip-opt pcschool 〉pcschool2.sql

mysqldump -uroot -pphpmysql pcschool student 〉pcschool3.sql

mysqldump -uroot -pphpmysql --skip-opt pcschool student 〉pcschool4.sql

mysqldump -uroot -pphpmysql pcschool student list 〉pcschool5.sql

mysqldump -uroot -pphpmysql --no-data pcschool 〉pcschool6.sql

mysqldump -uroot -pphpmysql --no-data pcschool list〉pcschool7.sql

mysqldump -uroot -pphpmysql --no-create-info pcschool 〉pcschool8.sql

mysqldump -uroot -pphpmysql --skip-opt --no-create-info pcschool 〉pcschool9.sql

mysqldump -uroot -pphpmysql -B pcschool 〉pcschooldb.sql




十一、 資料的匯入:mysql


mysql -uroot -pphpmysql pcschool〈pcschool.sql
mysql -uroot -pphpmysql counter〈record.sql



如果將pcschool資料庫刪除呢


mysql -uroot -pphpmysql pcschool〈 pcschooldb.sql
mysql -uroot -pphpmysql test〈 pcschooldb.sql



十二、 Big5資料的匯入:


mysql -uroot -pphpmysql --default-character-set=big5 test〈create_list_big5.sql



十三、 Big5資料的匯出:


mysqldump -uroot -pphpmysql --default-character-set=big5 test listbig5 〉listbig53.sql



十四、 新增帳號


grant all privileges on *.* to pcschool@localhost identified by 'phpmysql';

grant all privileges on board.* to php1@localhost identified by 'mysqlstart1';

grant select on board2.* to php3@localhost identified by 'mysqlstart3';



十五、 PHP連結資料庫語法(db.php,資料庫為pcschool)

〈html〉〈head〉〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉資料庫連線〈/title〉〈/head〉〈body〉〈?
$link = @mysql_connect("localhost", "pcschool", "phpmysql");
$link_db = @mysql_select_db("pcschool123446");
if($link) { echo "主機連結OK!....";
if($link_db) { echo "資料庫連結OK!...."; }
if(!$link_db) { echo "資料庫連結失敗!....."; }}
if(!$link) { die( "連結主機失敗!....");}
echo "Hello"; ?〉 〈/body〉〈/html〉




1. 問題:如果mysql_connect出錯,會如何?
2. 問題:如果mysql_selsect_db出錯,會如何?
3. 問題:在發生錯誤之後,可否在顯示訊息後跳出網頁?
十六、 mysql編碼與傳輸:unicode.php


〈?
mysql_query("SET NAMES utf8");
mysql_query("CHARACTER SET utf8");
mysql_query("SET CHARACTER_SET_CLIENT =utf8");
mysql_query("SET COLLATION_CONNECTION=utf8_general_ci");
mysql_query("SET CHARACTER_SET_RESULTS =utf8");
mysql_query("SET CHARACTER_SET_SERVER = utf8");
mysql_query("SET character_set_connection=utf8"); ?〉



十七、 PHP函數--送出執行語法:mysql_query("SQL 語法")
【提供mysql_query1.php】

〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉mysql_query〈/title〉〈/head〉〈body〉〈?
mysql_connect("localhost", "pcschool", "phpmysql") or die("無法連結主機");
mysql_select_db("pcschool") or die("無法連結資料庫");
//include("unicode.php");
//以下新增語法請做練習
//以下兩行為一行
//$sql = "insert into list (username, email, sex) values('pcschool', 'quota123@ms14.url.com.tw', '男')";
//$sql = "insert into list (email, sex) values('quota123@ms14.url.com.tw', '男')";
//$sql = "insert into list values('quota123@ms14.url.com.tw','pcschool','男')";
$sql = "insert into list values('','pcschool','男')";
mysql_query($sql) or die(mysql_error( ) );
?〉〈/body〉〈/html〉




若沒有加入unicode.php,MySQL內的資料會如何呢?
若資料表名稱錯誤或儲存的欄位數量不對,系統會如何回應呢?
【提供mysql_query01.php】


〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈title〉以下的Query是否有錯?〈/title〉〈/head〉〈body〉
〈?php
include("server.php");
$a='test';
$b='test2';
$sql="INSERT INTO orders(CustomerID,EmployeeID)VALUES ($a,$b);";
mysql_query($sql);
//mysql_query($sql) or die(mysql_error( ) );
//echo $sql;
?〉〈/body〉〈/html〉



若兩個query一起執行,但其中一個有問題會如何呢?【提供mysql_query02.php】

〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈title〉兩個Query執行會如何?〈/title〉〈/head〉〈body〉
〈?php
include("server.php");
$sql="INSERT INTO orders(CustomerID,EmployeeID)VALUES ('er', '23');";
mysql_query($sql);
$a='test';
$b='test2';
$sql="INSERT INTO orders(CustomerID,EmployeeID)VALUES ($a,$b);";
mysql_query($sql);
//mysql_query($sql) or die(mysql_error( ) );
//echo $sql;//得到與mysql_error( )
?〉〈/body〉〈/html〉




上述兩個練習請修正【修改為mysql_query03.php】:

$a='test';
$b='test2';
$sql="INSERT INTO orders(CustomerID,EmployeeID)VALUES ('$a',$b);";




複製資料表語法

create table insertemployees select * from employees;
create table insertjob select * from job;
create table updateemployees1 select * from employees;
create table updateemployees2 select * from employees;
create table updateemployees3 select * from employees;
create table updatejob select * from job;
create table updateproducts select * from products;
create table delemployees1 select * from employees;
create table delemployees2 select * from employees;
create table delemployees3 select * from employees;
create table deljob select * from job;




新增資料語法

insert into insertjob values (12);

insert into insertjob values ('manager',12);
select * from insertjob;

insert into insertjob values (12,'manager');
select * from insertjob;

insert into employees(lastname,firstname,employeeid) values ('jiannrong','yeh',2009);」
select lastname,firstname,employeeid from employees where employeeid=2009;

insert into insertjob(employeeid,title) select employeeid ,concat(firstname,"-",lastname) from employees;
select * from insertjob




十八、 資料查詢語法
1.基本查詢


select * from employees;
select firstname, lastname from employees;
select city from employees;
select distinct city from employees;



2.As的使用


select firstname as f, lastname as l from employees;

select productname, unitprice ,unitsinstock ,unitprice * unitsinstock as total from products;

select firstname,hiredate,curdate( ) as nowdate,(year(curdate( ) )-year(hiredate)) as years from employees;

select firstname,lastname,concat(firstname, "-- ",lastname) as yourname from employees;



3.排序


select firstname, lastname, hiredate from employees order by firstname;
select firstname, lastname, hiredate from employees order by firstname desc;
select firstname, lastname, hiredate from employees order by firstname asc;
select firstname, lastname from employees order by hiredate desc;
select firstname, lastname from employees order by firstname desc,lastname asc;
select firstname, lastname from employees order by lastname asc ,firstname desc;



十九、 PHP函數-- mysql_num_rows( ) 計算查詢後的筆數【請自行撰寫】


〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉計算筆數〈/title〉〈/head〉〈body〉〈?
@mysql_connect("localhost", "pcschool", "phpmysql") or die("無法連結主機");
@mysql_select_db(“pcschool”) or die("無法連結資料庫");
include("unicode.php");
$abc="select username from list";
echo $abc."〈br〉";
$sql = mysql_query($abc) or die(mysql_error( ) );;
$rows = mysql_num_rows($sql);
if($rows==""){
echo "查無資料!";
}else{ echo "有 ".$rows." 筆資料喔!";}
?〉〈/body〉〈/html〉



二十、 回傳欄位資料【請依照講義修改mysql_query04.php】


〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈title〉顯示記錄〈/title〉〈/head〉〈body〉
〈?php
include("server.php");
$sql="SELECT * FROM orders";
$sql2=mysql_query($sql) or die(mysql_error( ) );
echo $sql2;
$list1= mysql_fetch_array($sql2);
echo $list1['OrderID'];
?〉
〈/body〉〈/html〉



請修改上例,觀察顯示內容【請將mysql_query04.php另存新檔後依講義修改】


$list1= mysql_fetch_array($sql2);
echo $list1['OrderID']."〈br〉";
echo $list1['OrderID']."〈br〉";
$list1= mysql_fetch_array($sql2);
echo $list1['OrderID']."〈br〉";



二十一、 顯示資料【以array方式設計】
【請依照講義修改mysql_query06.php】


〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈title〉顯示所有資料〈/title〉〈/head〉〈body〉
〈?
include("server.php");
$sql="SELECT * FROM orders";
$sql2=mysql_query($sql) or die(mysql_error( ) );
while($list1= mysql_fetch_array($sql2))
{
echo $list1['OrderID']." ".$list1['OrderDate']."〈br〉";
}
?〉〈/body〉〈/html〉



二十二、 其他

星期二, 10月 26, 2010

PHP&MySQL--4 2010年10月課程講義預覽

PHP&MySQL--4
一、 陣列簡介
1. 陣列初始化


〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉第一個陣列〈/title〉〈/head〉
〈body〉〈?
$chinese[0] = 80;
$chinese[1] = 60;
$chinese[2] = 90;
$chinese[3] = 50;
$chinese[4] = 70;
for ($a=0; $a〈5; $a++)
echo "$chinese[$a] 〈br〉" ;
?〉〈/body〉〈/html〉



2. 請問以下陣列,網路硬碟上所提供的資料是否有缺?


〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉第一個陣列,不給予索引值編號〈/title〉〈/head〉
〈body〉〈?
$chinese[ ] = 80;
$chinese[ ] = 60;
$chinese[ ] = 90;
$chinese[ ] = 50;
$chinese[ ] = 70;
for ($a=0;$a〈5;$a++)
echo "座號".$a."同學的成績為:".$chinese[$a]."〈br〉" ;
?〉〈/body〉〈/html〉



3. 陣列可用array方式規劃,網路硬碟上所提供的資料是否有缺?

〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉以array方式建立陣列,索引值編號任意給〈/title〉〈/head〉
〈body〉〈?
$chinese=array(
1=〉80,
3=〉60,
6=〉90,
8=〉50,
9=〉70
);
for ($a=0;$a〈=10;$a++)
echo "座號".$a."同學的成績為:".$chinese[$a]."〈br〉" ;
?〉〈/body〉〈/html〉





4. 陣列可用文字做為索引,不過要如何看到全部資料?


〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉以array方式建立文字型態索引值陣列〈/title〉〈/head〉
〈body〉〈?
$a=array(
"Jan" =〉 "一月",
"Feb" =〉 "二月",
"Mar" =〉 "三月"
);
echo $a["Mar"]."〈br〉";
?〉〈/body〉〈/html〉



二、 for與foreach差別
1. 由「陣列可用array方式規劃」練習修改後比較,有何差別?

for ($a=0;$a〈=10;$a++)
echo $chinese[$a]."〈br〉" ;
foreach ($chinese as $value1)
echo $value1."〈br〉";




2. 由「陣列可用文字做為索引」練習修改,加入foreach讀取所有資料


foreach ($a as $value1)
echo $value1."〈br〉";



3. 由「陣列可用array方式規劃」練習修改,更快速顯示索引值與內容

foreach ($chinese as $key1 =〉$value1)
echo "座號".$key1."同學的成績為:".$value1."〈br〉" ;




三、 表單與Session:
1. 表單部分(1.php)


〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉表單登入〈/title〉〈/head〉〈body〉
〈form name="form1" method="post" action="2.php"〉請輸入姓名:
〈input type="text" name="username" maxlength="6" size="8"〉〈br〉
請輸入密碼:
〈input type="password" name="passwd" maxlength=”6” size="8"〉〈br〉
〈input type="submit"〉〈input type="reset"〉〈/form〉 〈/body〉〈/html〉



2. 接收表單資料

〈? session_start( ); ?〉〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉驗證表單資料〈/title〉〈/head〉〈body〉 〈?
if(!isset($_POST['username']))
{
?〉
〈script〉
window.alert('請輸入帳號');
//history.back( );
location.href="http://localhost/class3/session/1.php";
〈/script〉〈?
}
if(!isset($_POST['passwd']))
{
?〉
〈script〉
window.alert('請輸入密碼');
//history.back( );
location.href="http://localhost/class3/session/1.php";
〈/script〉
〈?
}
if($_POST['username']=="")
{ ?〉
〈script〉
window.alert('請輸入帳號');
history.back( );
//location.href="http://localhost/class3/session/1.php";
〈/script〉〈?
}
if($_POST['passwd']=="")
{
?〉
〈script〉
window.alert('請輸入密碼');
history.back( );
//location.href="http://localhost/class3/session/1.php";
〈/script〉
〈?
}
$_SESSION['username']=$_POST['username'];
$_SESSION['passwd']=$_POST['passwd'];
echo '〈br/〉〈a href="3.php"〉第三頁〈/a〉';
?〉〈/body〉〈/html〉
3. 第三頁的連結
〈? session_start( ); ?〉〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉查閱資料〈/title〉〈/head〉〈body〉 〈?
if(!isset($_SESSION['username']))
{
?〉
〈script〉
window.alert('請輸入帳號');
location.href='./1.php';
〈/script〉
〈?
}
if(!isset($_SESSION['passwd']))
{
?〉
〈script〉
window.alert('請輸入密碼');
location.href='./1.php';
〈/script〉
〈?
}
echo '歡迎光臨';
echo '〈br/〉〈a href="4.php"〉登出〈/a〉';
?〉〈/body〉〈/html〉



4. 第四頁登出


〈? session_start( ); ?〉〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉登出〈/title〉〈/head〉〈body〉 〈?
session_unset( );
session_destroy( );
?〉
〈script〉
//window.alert('請輸入帳號');
location.href='./1.php';
〈/script〉
〈/body〉〈/html〉



四、 修改:當網頁關閉時強制刪除session資料

〈title〉查閱資料〈/title〉〈/head〉〈body onUnLoad="checkunload();"〉
〈script〉
function checkunload()
{
location.href="./4.php";
}
〈/script〉




五、 header運用—轉移網頁
〈? header("Location: http://www.google.com"); ?〉
Q1:如果此例的utf-8含有BOM,執行時會如何呢?
Q2:如果網頁上還有其他資訊顯示,網頁可以轉移嗎?

〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉轉換網址〈/title〉〈/head〉
〈body〉〈?
header("Location: http://www.google.com");
?〉
?〉〈/body〉〈/html〉





六、 以下幾種函數都必須放在第一行,
且之前不能有任何輸出,包含空白:
header( )、setcookie( )、session_start( )、ob_start( )
七、 header運用—驗證身份後做網頁轉換【提供表單網頁】


〈? ob_start( ) ;?〉
〈html〉〈head〉〈meta http-equiv="content-type" content="text/html;charset=utf-8"〉
〈title〉驗證帳號與密碼〈/title〉〈/head〉
〈body〉〈?
if (($_POST['username']=='php') and ($_POST['passwd']=='mysql'))
header("Location: success.php");
else header("Location: failed.php");
?〉〈/body〉〈/html〉






八、 header運用—網頁每隔20秒向server讀取資料



〈? ob_start( ) ;?〉
〈html〉〈head〉〈meta http-equiv="content-type" content="text/html;charset=utf-8"〉
〈title〉網頁每隔20秒向server讀取資料〈/title〉〈/head〉
〈body〉〈?
header("refresh:20");
echo "現在時間:".date("h:i:s");
?〉〈/body〉〈/html〉



九、 header運用—網頁5秒後將轉移至google



〈? ob_start() ;?〉
〈html〉〈head〉〈meta http-equiv="content-type" content="text/html;charset=utf-8"〉
〈title〉網頁5秒後將轉移至google〈/title〉〈/head〉
〈body〉〈?
header('refresh:5; url="http://www.google.com"');
echo "五秒後連結google";
?〉〈/body〉〈/html〉




十、 與搜尋引擎有關:robots.txt


user-agent: *
disallow: /cig-bin
disallow:/members/data
disallow:/*.pdf$
十一、 如果欲禁止google抓取jpg圖片:robots.txt
user-agent: Googlebot-image
disallow:/*.jpg$




十二、 關於include

〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉foreach與print_r〈/title〉〈/head〉
〈body〉
〈TABLE align=center border=1 width=80%〉
〈TR〉〈TD〉〈? include("menu.php"); ?〉〈/TD〉
〈TD〉〈P〉這裡請輸入你的內容〈/TD〉〈/TR〉
〈/TABLE〉
〈/body〉〈/html〉





若改為require會有怎樣的情況發生呢?
若將menu.php刪除 會有怎樣的反應呢?
如果不想顯示錯誤訊息,該如何處理呢?
〈TABLE align=center border=1 width=80%〉
〈TR〉〈TD〉〈? if (!@include("menu.php")) die("連結錯誤"); ?〉〈/TD〉
〈TD〉〈P〉這裡請輸入你的內容〈/TD〉〈/TR〉
〈/TABLE〉〈/body〉〈/html〉
十三、 資料庫是什麼?
十四、 若忘記root密碼:【請參考網路硬碟提供的指令依序進行密碼設定】
十五、 一些MySQL指令
說明 指令
顯示目前資料庫 show databases;
檢視資料表:例如檢視mysql資料庫內的資料表 【請先使用資料庫】 show tables from mysql;
顯示資料表 【請先使用資料庫】 show tables;
檢視欄位 【請先使用資料庫】 show columns from db from mysql;
建立資料庫 create database pcschooldb;
刪除資料庫 drop database pcschooldb;
使用資料庫 use test;
十六、 資料庫設計
十七、 什麼是SQL檔案
十八、 利用phpMyAdmin建pcschool資料庫,將student_create_table.sql匯入
十九、 將student.sql匯入,請觀察匯入的結果。【student.sql檔案為big5編碼】
二十、 利用phpMyAdmin建立pcschool2資料庫,但校對請選擇「Big5」,並請您將student.sql匯入,請觀察匯入的結果。【student.sql檔案為big5編碼】
二十一、 談資料表索引
二十二、 其他

星期四, 10月 21, 2010

PHP&MySQL--3 2010年10月課程講義預覽

PHP&MySQL--3
一、 參考書籍:上奇出版的「PHP 6 與MySQL基礎學習教室」
二、 php5的時間調整:date.timezone -〉 Asia/Tokyo或Asia/Taipei
三、 session時間的初始化

〈? //ob_start( );
session_start( ); //啟動 session
ini_set('date.timezone','Asia/Tokyo');
?〉



備註:session暫存路徑修改 ini_set('session.save_path','../');
四、 session的生命週期


〈? session_start( ); ?〉〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉 session的生命週期〈/title〉〈/head〉〈body〉
〈?
echo "a:".$_SESSION['a']."〈br〉";
echo session_id( )."〈br〉";
$_SESSION['a']=5;
echo "a:".$_SESSION['a']."〈br〉";
echo session_id( )."〈br〉";
session_unset( );
echo "a:".$_SESSION['a']."〈br〉";
echo session_id( )."〈br〉";
session_destroy( );
echo "a:".$_SESSION['a']."〈br〉";
echo session_id( )."〈br〉";
?〉 〈a href="session_test2.php"〉session是否可以看見?〈/a〉〈/body〉〈/html〉



說明:變數註冊為session變數與反註冊session變數於PHP6內取消
請檢視若只有執行或執行,第二頁的session變數是否可以顯示內容?

〈? session_start( ); ?〉
〈head〉〈meta http-equiv="Content-Type" content="text/html; charset=utf-8" /〉
〈title〉無標題文件〈/title〉〈/head〉
〈body〉〈? echo "a:".$_SESSION['a']."〈br〉"; ?〉〈/body〉〈/html〉





五、 單一變數的清除與刪除( session_test3.php)

〈?session_start( ); ?〉〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=UTF-8"〉
〈title〉判斷session類型變數是否存在〈/title〉〈/head〉
〈body〉〈?
if (isset($_SESSION['b']))
{
echo "b存在"."〈br〉";
echo $_SESSION['b']."〈br〉";
}
else
{
echo "b不存在"."〈br〉";
$_SESSION['b']=50;
echo $_SESSION['b']."〈br〉";
}
?〉
〈a href="session_test4.php"〉session是否可以看見?〈/a〉〈/body〉〈/html〉






session_test4.php


〈? session_start( ); ?〉
〈head〉〈meta http-equiv="Content-Type" content="text/html; charset=utf-8" /〉
〈title〉變數清除與刪除〈/title〉〈/head〉〈body〉
〈?
echo "1b:".$_SESSION['b']."〈br〉";
//$_SESSION['b']="";
unset($_SESSION['b']);
echo "2b:".$_SESSION['b']."〈br〉";
?〉〈a href="session_test3.php"〉返回 〈/a〉〈/body〉〈/html〉




六、 網頁人數統計,加上session限制

〈?ob_start( );?〉
〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉session只能使用一次〈/title〉〈/head〉〈body〉
〈?
session_start( );
if(!isset($_SESSION['counter']))
{
$_SESSION['counter']=1;
echo "歡迎光臨";
}
else
echo "已登錄過請勿重複登入";
?〉〈/body〉〈/html〉





七、 表單與Session:
1. 表單部分(1.php)

〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉表單登入〈/title〉〈/head〉〈body〉
〈form name="form1" method="post" action="2.php"〉請輸入姓名:
〈input type="text" name="username" maxlength="6" size="8"〉〈br〉
請輸入密碼:
〈input type="password" name="passwd" maxlength=”6” size="8"〉〈br〉
〈input type="submit"〉〈input type="reset"〉〈/form〉 〈/body〉〈/html〉




2. 接收表單資料

〈? session_start( ); ?〉〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉驗證表單資料〈/title〉〈/head〉〈body〉 〈?
if(!isset($_POST['username']))
{
?〉
〈script〉
window.alert('請輸入帳號');
//history.back( );
location.href="http://localhost/class3/session/1.php";
〈/script〉〈?
}
if(!isset($_POST['passwd']))
{
?〉
〈script〉
window.alert('請輸入密碼');
//history.back( );
location.href="http://localhost/class3/session/1.php";
〈/script〉
〈?
}
if($_POST['username']=="")
{ ?〉
〈script〉
window.alert('請輸入帳號');
history.back( );
//location.href="http://localhost/class3/session/1.php";
〈/script〉〈?
}
if($_POST['passwd']=="")
{
?〉
〈script〉
window.alert('請輸入密碼');
history.back( );
//location.href="http://localhost/class3/session/1.php";
〈/script〉
〈?
}
$_SESSION['username']=$_POST['username'];
$_SESSION['passwd']=$_POST['passwd'];
echo '〈br/〉〈a href="3.php"〉第三頁〈/a〉';
?〉〈/body〉〈/html〉




3. 第三頁的連結

〈? session_start( ); ?〉〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉查閱資料〈/title〉〈/head〉〈body〉 〈?
if(!isset($_SESSION['username']))
{
?〉
〈script〉
window.alert('請輸入帳號');
location.href='./1.php';
〈/script〉
〈?
}
if(!isset($_SESSION['passwd']))
{
?〉
〈script〉
window.alert('請輸入密碼');
location.href='./1.php';
〈/script〉
〈?
}
echo '歡迎光臨';
echo '〈br/〉〈a href="4.php"〉登出〈/a〉';
?〉〈/body〉〈/html〉




4. 第四頁登出




〈? session_start( ); ?〉〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉登出〈/title〉〈/head〉〈body〉 〈?
session_unset( );
session_destroy( );
?〉
〈script〉
//window.alert('請輸入帳號');
location.href='./1.php';
〈/script〉
〈/body〉〈/html〉




八、 Cookie基本練習
1. 儲存Cookie


〈? ob_start( ) ;?〉
〈html〉〈head〉〈meta http-equiv="content-type" content="text/html;charset=utf-8"〉
〈title〉儲存Cookie〈/title〉〈/head〉
〈body〉〈?
setcookie ("a", "php", time( )+1800);//1800代表1800秒
?〉〈/body〉〈/html〉



2. 顯示Cookie


〈html〉〈head〉〈meta http-equiv="content-type" content="text/html;charset=utf-8"〉
〈title〉顯示Cookie〈/title〉〈/head〉
〈body〉〈?
if (isset($_COOKIE["a"]))
echo "cookie內容為".$_COOKIE["a"];
else
echo "沒有資料";
?〉〈/body〉〈/html〉



3. 刪除Cookie(時間到期)

〈? ob_start( ) ;?〉
〈html〉〈head〉〈meta http-equiv="content-type" content="text/html;charset=utf-8"〉
〈title〉刪除Cookie〈/title〉〈/head〉
〈body〉〈?
setcookie ("a", "php", time( )-1800);
?〉〈/body〉〈/html〉




4. 刪除Cookie(變數沒有內容)

〈? ob_start( ) ;?〉
〈html〉〈head〉〈meta http-equiv="content-type" content="text/html;charset=utf-8"〉
〈title〉儲存Cookie〈/title〉〈/head〉
〈body〉〈?
setcookie ("a", "", time( )+1800);
?〉〈/body〉〈/html〉




九、 Cookie綜合練習
1. 設定Cookie,檔名為showcookie1.php

〈?ob_start( );?〉
〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉 cookies設定綜合練習〈/title〉〈/head〉〈body〉
〈?$tomorrow = mktime (0,0,0,date("m"),date("d")+1,date("Y")); //mktime
$nextmonth = mktime (0,0,0,date("m")+1,date("d"),date("Y"));
$nextyear = mktime (0,0,0,date("m"),date("d"),date("Y")+1);
$yourname="第6個cookie,Cookie變數的內容由變數取得資料";
$testtime=mktime(0,0,0,3,13,2009);
setcookie ("a[0]","設定保存1800秒", time( )+1800);
setcookie ("a[1]","設定保存至2009年3月13日",$testtime);
setcookie ("a[2]","設定保存至明天",$tomorrow);
setcookie ("a[3]","設定保存至下一個月",$nextmonth);
setcookie ("a[4]","設定保存至明年",$nextyear);
setcookie("a[5]","$yourname",time( )+1800);
echo '〈a href="showcookie2.php"〉查詢Cookies〈/a〉';
?〉〈/body〉〈/html〉





2. 查詢Cookie,檔名為showcookie2.php


〈?ob_start( );?〉
〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉 cookies設定綜合練習查詢〈/title〉〈/head〉〈body〉〈?
$value1=$_COOKIE["a"];
for ($for1=0;$for1〈5;$for1++)
{ $a=$for1+1;
echo "第".$a."的內容為".$value1[$for1]."〈br〉";
}
echo '〈a href="showcookie1php"〉重新建立Cookies〈/a〉〈br〉';
echo '〈a href="showcookie3.php"〉刪除幾個Cookies〈/a〉〈br〉';
?〉〈/body〉〈/html〉



3. 刪除Cookie:檔名為showcookie3.php


〈?ob_start( );?〉
〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉 cookies設定綜合練習刪除〈/title〉〈/head〉〈body〉〈?
echo "設定保存1800秒,現在減1800秒"."〈br〉";
echo "設定保存至2009年3月13日,現在內容沒了"."〈br〉";
setcookie ("a[0]","",time( )-1800);//內容沒了
setcookie ("a[1]","");//內容沒了
echo '〈a href="showcookie1php"〉重新建立Cookies〈/a〉〈br〉';
echo '〈a href="showcookie3.php"〉刪除幾個Cookies〈/a〉〈br〉';
?〉〈/body〉〈/html〉




十、 header運用—轉移網頁
〈? header("Location: http://www.google.com"); ?〉
Q1:如果此例的utf-8含有BOM,執行時會如何呢?
Q2:如果網頁上還有其他資訊顯示,網頁可以轉移嗎?


〈html〉〈head〉
〈meta http-equiv="Content-Type" content="text/html; charset=utf-8"〉
〈title〉轉換網址〈/title〉〈/head〉
〈body〉〈?
header("Location: http://www.google.com");

?〉
?〉〈/body〉〈/html〉



十一、 以下幾種函數都必須放在第一行,
且之前不能有任何輸出,包含空白:
header( )、setcookie( )、session_start( )、ob_start( )
十二、 header運用—驗證身份後做網頁轉換【提供表單網頁】



〈? ob_start( ) ;?〉
〈html〉〈head〉〈meta http-equiv="content-type" content="text/html;charset=utf-8"〉
〈title〉驗證帳號與密碼〈/title〉〈/head〉
〈body〉〈?
if (($_POST['username']=='php') and ($_POST['passwd']=='mysql'))
header("Location: success.php");
else header("Location: failed.php");
?〉〈/body〉〈/html〉




十三、 header運用—網頁每隔20秒向server讀取資料


〈? ob_start( ) ;?〉
〈html〉〈head〉〈meta http-equiv="content-type" content="text/html;charset=utf-8"〉
〈title〉網頁每隔20秒向server讀取資料〈/title〉〈/head〉
〈body〉〈?
header("refresh:20");
echo "現在時間:".date("h:i:s");
?〉〈/body〉〈/html〉



十四、 其他