[上課筆記] 使用PHP做一個資料管理

作業練習

1. 建立一個新的TABLE,欄位4個,規格與範例如下

[索引鍵(int)][動物名稱(text)][動物種類(text)][體重(int)]
1阿瘦貓熊30

2. 建立一個頁面,提供文字欄位3個與按鈕1個,可以新增動物資料

3. 建立一個頁面,可以顯示所有動物的資料,例如:

1阿瘦貓熊30

4. 建立一個頁面,可以刪除指定的動物資料(輸入索引鍵刪除)

5. 建立一個頁面,可以修改指定的動物資料,藉由輸入索引確定欲修改的資料(但索引鍵不能被修改)


會用到的PHP功能

include("head.php");
可以將別的網頁整合到目前內容,通常被用PHP做分割頁面做header.php+body.php=index.php,以及一些通用的語法可以做個include方便節省

switch($page){
 case "main":
  include('main.php');
  break;
}
SWITCH判斷,比較多項目要做不同結果執行時,比較清楚條列的判斷式用途

$link=mysqli_connect("localhost","root","","a2018");
mysqli_query($link,"set names utf8");
透過php去登入SQL,需要輸入位置,帳號,密碼,跟資料庫名稱。而設定UTF8作為避免寫入中文時會發生亂碼,所以第兩行通常會跟著馬上寫。同樣這兩行會習慣獨立寫在例如linkdb.php內,再透過include("linkdb.php");的方式快速放在會操作SQL的網頁中。

header("location:???");
設計form表單送出資料時,如果你操作沒有導向其他頁面,其之前的資料還會存在,會導致你重整F5時,資料又再重複送出。所以會利用header要求到其他頁面脫離的表單的服務。

$sql="SELECT * from w04_animal;";
mysqli_query($link,$sql);
每次要執行上傳遠端的SQL語法時,每次都要透過mysqli_query去進行連結SQL。否則PHP不會自動去跟資料庫傳送指令。

$ro=mysqli_query($link,$sql);
$row=mysqli_fetch_assoc($ro);
如果你回傳的資料是很多的欄位值,用mysqli_fetch_assoc做個轉換,儲存到陣列型態的變數$row內,方便之後取出。但每一次只會倒出一筆。FIFO結構(first in first out),所以可以搭配do while做連續印出

do{
 echo $row['ani_id']."|".$row['ani_name']."|".$row['ani_type']."|".$row['ani_weight']."<br/>";
}while ($row=mysqli_fetch_assoc($ro));
每次被執行mysqli_fetch_assoc一次,$ro的第一組資料會給$row並清除自身(FIFO)

開始寫碼
我們利用一個index.php當首頁,並分為header.php與main.php兩個區域,header.php做為我們的題目選單,根據不同的選擇做main的抽換。接著根據題目要求設計出SQL語法查詢(利用網頁執行SQL碼);然後有顯示、新增、修改、刪除,以及都有連結DB,所以再設計一個linkdb.php嵌入到header.php內。

/**************************index.php**************************/
<?php
if(!empty($_GET['page'])) $page=$_GET['page'];
else $page="main";

include("header.php");
switch($page){
 case "main":
  include('main.php');
  break;
 case "print_sql":
  include('print_sql.php');
  break;
 case "add_animal":
  include('add_animal.php');
  break;
 case "show_all":
  include('show_all.php');
  break;
 case "del_animal":
  include('del_animal.php');
  break;
 case "modify_animal":
  include('modify_animal.php');
  break;
}
?>

/**************************header.php**************************/
<head><style>
table,tbody,tr,th,td{
 border: 1px solid black;
}
table{
 min-width:200px;
 border-collapse: collapse;
}
</style></head>

<form method="GET">
 <button type="submit" name="page" value="main">回首頁</button>
 <button type="submit" name="page" value="print_sql">查詢Create Table SQL語法</button>
 <button type="submit" name="page" value="add_animal">新增</button>
 <button type="submit" name="page" value="show_all">顯示所有</button>
 <button type="submit" name="page" value="del_animal">刪除</button>
 <button type="submit" name="page" value="modify_animal">修改</button>
</form>
<hr/>
<?php include_once("linkdb.php");?>
/**************************main.php**************************/
<h2>作業練習</h1>
<h4>1. 建立一個新的TABLE,欄位4個,規格與範例如下</h4>
<table>
 <tr>
  <th>[索引鍵(int)]</th><th>[動物名稱(text)]</th><th>[動物種類(text)]</th><th>[體重(int)]</th>
 </tr>
 <tr>
  <td>1</td><td>阿瘦</td><td>貓熊</td><td>30</td>
 </tr>
</table>
<h4>2. 建立一個頁面,提供文字欄位3個與按鈕1個,可以新增動物資料</h4>

<h4>3. 建立一個頁面,可以顯示所有動物的資料,例如:</h4>
<table>
 <tr>
  <td>1</td><td>阿瘦</td><td>貓熊</td><td>30</td>
 </tr>
</table>
<h4>4. 建立一個頁面,可以刪除指定的動物資料(輸入索引鍵刪除)</h4>
<h4>5. 建立一個頁面,可以修改指定的動物資料,藉由輸入索引確定欲修改的資料(但索引鍵不能被修改)</h4>
/**************************print_sql.php**************************/
<?php
$sql_html="
 <b>CREATE TABLE</b> w04_animal(               <br/>
  <span style='padding-left:20px'/>ani_id int(10) COMMENT'索引鍵' AUTO_INCREMENT,  <br/>
  <span style='padding-left:20px'/>ani_name text COMMENT'動物名稱',     <br/>
  <span style='padding-left:20px'/>ani_type text COMMENT'動物總類',     <br/>
  <span style='padding-left:20px'/>ani_weight int COMMENT'體重',      <br/>
  <span style='padding-left:20px'/><b>PRIMARY KEY</b>(ani_id)       <br/>
 );       
";
echo $sql_html;
/***************************************************** use php to link sql
$sql="
CREATE TABLE w04_animal(
 ani_id int(10) COMMENT'索引鍵' AUTO_INCREMENT,
 ani_name text COMMENT'動物名稱',
 ani_type text COMMENT'動物總類',
 ani_weight int COMMENT'體重',
 PRIMARY KEY(ani_id));
";
mysqli_query($link,$sql);
//*****************************************************/

?>

/**************************add_animal.php**************************/

<?php
if(!empty($_POST['ani_name'])){
 $sql="INSERT INTO w04_animal VALUES(null,'".$_POST['ani_name']."','".$_POST['ani_type']."',".$_POST['ani_weight'].");";
 mysqli_query($link,$sql);
 header("location:?page=show_all");
}
?>
<meta charset="UTF-8">
<form method="POST">
 <p>動物名字 <input name="ani_name" type="text"></p>
 <p>動物類型 </span><input name="ani_type" type="text"></p>
 <p>體重(KG) </span><input name="ani_weight" type="int"></p>
 <input type="submit" value="新增">
</form>


/**************************show_all.php**************************/
<?php
 $sql="SELECT * from w04_animal;";
 //echo $sql;
 $ro=mysqli_query($link,$sql);
 $row=mysqli_fetch_assoc($ro);
?>
<table>
 <tr>
  <th>[索引]</th><th>[動物名稱]</th><th>[動物種類]</th><th>[體重(kg)]</th>
 </tr>
 <?php do{ ?>
 <tr>
  <?php echo "<td>".$row['ani_id']."</td><td>".$row['ani_name']."</td><td>".$row['ani_type']."</td><td>".$row['ani_weight']."</td>";?>
 </tr>
 <?php }while($row=mysqli_fetch_assoc($ro)); ?>

 <?php
 /* line 13~16 same this
  do{
   echo"<tr>";
    echo "<td>".$row['ani_id']."</td><td>".$row['ani_name']."</td><td>".$row['ani_type']."</td><td>".$row['ani_weight']."</td>";
   echo"</tr>";
  }while($row=mysqli_fetch_assoc($ro));
 */
 ?>
 
</table>

/**************************del_animal.php**************************/
<?php
if(!empty($_POST['ani_id'])){
 $sql="DELETE FROM w04_animal WHERE ani_id=".$_POST['ani_id'];
 echo $sql;
 mysqli_query($link,$sql);
 header("location:?page=show_all");
}
?>
<meta charset="UTF-8">
<form method="POST">
 <p>輸入欲刪除之動物索引編號<input name="ani_id" type="int"></p>
 <input type="submit" value="刪除">
</form>

/**************************modify_animal.php**************************/
<?php
if(!empty($_POST['ani_id'])){
 $sql="
  UPDATE w04_animal
  SET ani_name='".$_POST['ani_name']."',ani_type='".$_POST['ani_type']."',ani_weight=".$_POST['ani_weight']."
  WHERE ani_id=".$_POST['ani_id'].";
 ";
 echo $sql;
 mysqli_query($link,$sql);
 header("location:?page=show_all");
}
?>
<meta charset="UTF-8">
<form method="POST">
 <h4>輸入指定之動物索引編號,以及修改之內容</h4>
 <p>索引 <input name="ani_id" type="int"></p>
 <p>動物名字 <input name="ani_name" type="text"></p>
 <p>動物類型 </span><input name="ani_type" type="text"></p>
 <p>體重(KG) </span><input name="ani_weight" type="int"></p>
 <input type="submit" value="修正">
</form>



到目前為止已作完題目,但為了更再優化一下,將新增修改刪除都一起做到show_all.php,所以show_all.php修改為

/**************************show_all.php**************************/
<?php
 if(!empty($_POST["del_ani_btn"])) { //for del_bth
  $sql="
   DELETE FROM w04_animal 
   WHERE ani_id=".$_POST['del_ani_btn'];
  mysqli_query($link,$sql) or die("刪除失敗");
  header("location:?page=show_all");
 }
 if(!empty($_POST["mdy_ani_btn"])) { //for mdy_bth
  $sql="
   UPDATE w04_animal 
   SET ani_name='".$_POST['ani_name']."',ani_type='".$_POST['ani_type']."',ani_weight=".$_POST['ani_weight']."
   WHERE ani_id=".$_POST['mdy_ani_btn'];
  mysqli_query($link,$sql) or die("修改失敗");
  header("location:?page=show_all");
 }
 if(!empty($_POST["add_ani_btn"])) { //for add_bth
  $sql="
   INSERT INTO w04_animal 
   VALUES(null,'".$_POST['ani_name']."','".$_POST['ani_type']."',".$_POST['ani_weight'].")";
  mysqli_query($link,$sql) or die("新增失敗");
  header("location:?page=show_all");
 }
 $sql="SELECT * from w04_animal;"; //for show all
 //echo $sql;
 $ro=mysqli_query($link,$sql);
 $row=mysqli_fetch_assoc($ro);
?>
<table>
 <tr>
  <th>[索引]</th><th>[動物名稱]</th><th>[動物種類]</th><th>[體重(kg)]</th><th>[操作]</th>
 </tr>
 <?php do{ ?>
 <tr>
  <?php echo 
  "<form method='post'>
  <td>".$row['ani_id']."</td>
  <td><input type='text' name='ani_name' value='".$row['ani_name']."'></td>
  <td><input type='text' name='ani_type' value='".$row['ani_type']."'></td>
  <td><input type='text' name='ani_weight' value='".$row['ani_weight']."'></td>
  <td>
   <button type='submit' name='del_ani_btn' value='".$row['ani_id']."'>刪除</button>
   <button type='submit' name='mdy_ani_btn' value='".$row['ani_id']."'>修改</button>
  </td>
  </form>"
  ;?>
 </tr>
 <?php }while($row=mysqli_fetch_assoc($ro)); ?>

 <tr>
  <form method="post">
  <td></td>
  <td><input name="ani_name" type="text"></td>
  <td><input name="ani_type" type="text"></td>
  <td><input name="ani_weight" type="int"></td>
  <td><input type="submit" value="新增" name="add_ani_btn"></td>
  </form>
 </tr>
</table>

留言