$action=$_GET['action'];
if($action=="insert")
{
$res=$bw->insert("xsd_markets",$_POST);
if($res==1)
{
echo "";
}
else
{
echo "";
}
}
?>
//設定檔
//串連資料庫
$host = "localhost";
$dbName= "xsdgoodcom";//資料庫名稱
$dbUser= "root";//使用者名稱
$dbPwd = "changeme";//密碼
$query=$bw->query($sql);
//資料插入
/*
$tbName : 表名
$post : post 提交資料, 也可以不是表單提交的數組資料
傳回值true or false
*/
function insert($tbName,$post)
{
$elem = array();
$val = array();
foreach($post as $key=>$value)
{
$elem[] = $key;
$val[] = trim($value);
}
$elemSum = count($elem);
for($i=0; $i<$elemSum; $i++)
{
$elemStr .=$elem[$i];
$valStr .="'".$val[$i]."'";
if($i<$elemSum-1)
{
$elemStr .= ',';
$valStr .= ',';
}
}
$insertSql = "INSERT INTO {$tbName}(".$elemStr.") VALUES (".$valStr.")";
//die($insertSql);
$return = $this->query($insertSql);
if($return)
{
return true;
}else{
return false;
}
//end
}
回複討論(解決方案)
你是怎麼調用的
$res=$bw->insert("xsd_markets",$_POST);
if($res==1)
{
echo "";
}
else
{
echo "";
}
我想知道,你這個$bw變數,在其它頁面裡可用嗎?
同樣調用資料庫連接了嗎?還是....
你把資料庫連接檔案include進來了嗎?
你確定方法能調用,變數的值是正常的嗎?