只要表單的欄位名稱和資料庫的欄位名稱相同時,即可以順利儲存。
(註:若名稱不同會無法存入,請務必要讓名稱相同。)
(註2:非使用表單如送出,或是只是內部判斷密碼是否相同的情形,請勿設定「name」。
最好的方式是判斷資料使用java script做判斷處理)
另外,由於有時候會有額外的資訊,比如說和時間有關的即時生成資料。
故此物件也加入了額外資料的處理方法。
使用方法的程式段:
無額外資料段使用法:
require("class.add_data.php");
$add_data = new add_data("資料表名稱");
$add_data -> sql_save();
有額外資料段使用法:
require("class.add_data.php");
$add_data = new add_data("資料表名稱");
$add_data -> add_text("欄位名稱","資料內容")
$add_data -> sql_save();
物件檔名:class.add_data.php
class add_data{
function __construct($table){
$this->table = $table;
$this->Post_dearray();
}
function Post_dearray(){
foreach($_POST as $key => $value){
if ($key != "submit" && $key != "MM_insert" && $key != "button") {
$this->add_text($key,$value);
}
}
}
function add_text($add_field,$add_value){
$this->field .= $add_field.",";
$this->data .= "'".htmlentities($add_value,ENT_QUOTES,"utf-8")."',";
}
function sql_text(){
$this->field = substr($this->field,0,strlen($this->field) -1);
$this->data = substr($this->data,0,strlen($this->data) -1);
return "INSERT INTO `".$this->table."` (".$this->field.")
VALUES (".$this->data.")";
}
function sql_save(){
mysql_query($this->sql_text()) or die("NO SAVE");
}
}

發文用 < pre > 包起來, 才看的到空格
回覆刪除原本Post_dearray()的內容是寫在建構式。
回覆刪除但是將他從建構式提取出來封裝成一個函式。
讓建構式的功能很單純的用做預設變數指定及預設函式叫用的功能。