close

這是本週資料輸入的練習參考範本,學員雖可以加以複製使用,但,一定要先瞭解其中的意思!


在SQL語法中,「文字」資料前後要用「單引號」' 框註;數值資料,不管整數或小數,則不必!


 




<?php
$theForm=$_POST['frm_in'];
if(empty($theForm))
{
header("Location: index.php")
;   // 指示瀏覽器重新導向指定的網址


exit  // 離開此php網頁--避免其他程式碼被執行
}
else
{
 switch($theForm)
 {
  case 'cst':
   $table='customers';
   $datas="null,'" . $_POST['nm'] . "'";
   $datas=$datas . "," . $_POST['sx'];
   $datas=$datas . ",'" . $_POST['ad'] . "'";
   $datas=$datas . ",'" . $_POST['ml'] . "'";
   break;
  case 'srt':
   $table='sorts';
   $datas="null,'" . $_POST['snm'] . "'";
   break;
  case 'prd':
   $table='products';
   $datas="'" . $_POST['pid'] . "'";
   $datas=$datas . "," . $_POST['sid'];
   $datas=$datas . ",'" . $_POST['pnm'] . "'";
   $datas=$datas . "," . $_POST['prc'];
   break;
  case 'rdr':
   $table='orders';
   $datas="null," . $_POST['cid'];
   $datas=$datas . "," . $_POST['sm'];
   $datas=$datas . ",'" . $_POST['yr'];
   $datas=$datas . "-" . $_POST['mth'];
   $datas=$datas . "-" . $_POST['dt'] . "'";
   break;
  case 'tms':
   $table='items';
   $datas=$_POST['oid'];
   $datas=$datas . ",'" . $_POST['pid'] . "'";
   $datas=$datas . "," . $_POST['qnt'];
   break;
  default:
   header("Location: index.php");
   exit;
 }
}


// 以你的資料庫使用者[名稱]與[密碼]登入到php網站的MySQL資料庫系統的特定資料庫
$link = mysqli_connect('db.host.sk', '使用者名稱', '使用者密碼', '資料庫名稱');
if (!$link)
{
   printf("無法進入host.sk網站的MySQL資料庫系統: %s\n", mysqli_connect_error());
   exit();
}


// 撰寫新增資料到指定資料表的DML-SQL,將該些文字交給區域變數 $query 暫存起來
$query = 'insert into ' . $table . ' values (' . $datas . ')';


// 透過mysqli_query()執行 $query 中的DML-SQL指令
$result = mysqli_query($link, $query);
?>


<html>


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


<meta http-equiv="Content-Type" content="text/html; charset=big5">
<bgsound id="MySound" src="musics/piano1.mid" loop="-1">
<script src="change.js"></script>
<title>社大PHP網頁製作練習</title>
<style fprolloverstyle>
A:hover {color: #008080; text-decoration: none; font-weight: bold}
</style>
</head>


<body background="images/flower_strips.gif" bgproperties="fixed" marginheight="0" marginwidth="0" topmargin="0" leftmargin="5" rightmargin="0" bottommargin="0" onLoad="changeBG(); changeBS();" link="#808000" vlink="#008000" alink="#996633">


<table border="1" cellspacing="1" width="100%" id="AutoNumber1" height="328">
  <tr>
    <td width="40%" colspan="2" rowspan="2" height="120">
    <img border="0" src="images/greatday.gif" width="290" height="120"></td>
    <td width="60%" colspan="4" height="69"><b><font size="5">
    <font color="#0000FF">XX社區大學</font>&nbsp; </font>
    <font color="#008080" size="6">PHP動態網頁設計班</font></b></td>
  </tr>
  <tr>
    <td width="30%" colspan="2" height="51"><font face="標楷體"><b><font size="4">
    地點:</font><font size="5" color="#000080">CL高中</font></b></font></td>
    <td width="30%" colspan="2" height="51"><b><font size="4" face="標楷體">授課講師:</font><font size="5" color="#808000">Classic</font></b></td>
  </tr>
  <tr>
    <td width="100%" colspan="6" height="16">&nbsp;</td>
  </tr>
  <tr>
    <td width="25%" height="16" align="center">
      <b><a href="index.php">首&nbsp; 頁</a></b></td>
    <td width="15%" height="16" align="center">
      <b><a href="index.php?cat=cus">客戶資料增修</a></b></td>
    <td width="15%" height="16" align="center">
      <b><a href="index.php?cat=sor">產品類別增修</a></b></td>
    <td width="15%" height="16" align="center">
      <b><a href="index.php?cat=pro">產品資料增修</a></b></td>
    <td width="15%" height="16" align="center">
      <b><a href="index.php?cat=ord">訂單資料增修</a></b></td>
    <td width="15%" height="16" align="center">
      <b><a href="index.php?cat=ite">訂單細目增修</a></b></td>
  </tr>
  <tr>
    <td width="25%" valign="top">
    <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" id="AutoNumber2">
      <tr>
        <td width="100%">
        <p align="center">相 關 連 結</td>
      </tr>
      <tr>
        <td width="100%"><b>AAA</b></td>
      </tr>
      <tr>
        <td width="100%"><b>BBB</b></td>
      </tr>
      <tr>
        <td width="100%"><b>CCC</b></td>
      </tr>
      <tr>
        <td width="100%"><b>DDD</b></td>
      </tr>
      <tr>
        <td width="100%"><b>EEE</b></td>
      </tr>
      <tr>
        <td width="100%">
        <p align="center">
        <b>
        <a target="_blank" href="http://tw.myblog.yahoo.com/tutor-classic">
        教 學 部 落</a></b></td>
      </tr>
    </table>
    </td>
    <td width="60%" colspan="4" valign="top">


<?php
if (!$result)
    echo '資料無法寫入網站MySQL系統的'. $table . ' 資料表!';
else
    echo mysqli_affected_rows($link) . '筆資料已經成功寫入 ' . $table . ' 資料表!'
;
?>


    </td>
    <td width="15%" valign="top" align="center">
    <p>
  <table width="100%" border="0" align="center">
    <form>
     <tr>
      <td align="center">
      <p><input type="button" value="換背景圖片" onClick="changeBG()"></p></td>
     </tr>
     <tr>
      <td align="center">
      <p><input type="button" value="換背景音樂" onClick="changeBS()"></p></td>
     </tr>
    </form>
  </table>
 </p>
 <p><img border="0" src="images/flower_green.gif" width="80" height="80"></td>
  </tr>
  <tr>
    <td width="100%" colspan="6" height="16">
    <script src="aloha.js"></script>
    </td>
  </tr>
</table>


</body>


</html>




arrow
arrow
    全站熱搜

    夜貓 發表在 痞客邦 留言(8) 人氣()