帳號登錄
帳號:
密碼:
記住我 | 忘記密碼 | 帳號註冊
網站導覽
模組開發 : 模組基本內容-將資料放入資料庫
作者 elie 於 2010年09月02日 21:59:19 (4344 次閱讀)

原始出處 http://www.xoops.org/modules/mediawiki/index.php/Basic_content_of_a_module

 

將資料放入資料庫

讓我們繼續下去,我們現在要把表單資料放入資料表格。

<?php

// Tutorial

// Created by KaotiK

require('../../mainfile.php');

require(XOOPS_ROOT_PATH.'/header.php');

 

if (isset($_POST['submit'])){

if (empty($_POST['name'])){

echo 'please fill in a name';

} else {

$name=$_POST['name'];

$address=$_POST['address'];

$tel=$_POST['tel'];

$email=$_POST['email'];

$query = "Insert into ".$xoopsDB->prefix("tutorial_myform")." (name, address, telephone, email) values ('$name', '$address', '$tel', '$email' )";

$res=$xoopsDB->query($query);

if(!$res) {

echo "error: $query";

} else {

echo "Data was correctly inserted into DB!";

}

}

}

?>

新增的前四行指令是新增變數並將變數值指定為表單的變數值。這樣的用意是讓你更容易了解發生了什麼事情。接下來的$query 建立了$xoopsDB 會用到的指令如下:

$res=$xoopsDB->query($query);

下一行:

if(!$res)


是檢查連結資料庫時是否有任何錯誤發生。如果一切正常的話則顯示"Data was correctly inserted into DB!"。在接下來的文件中我將告訴你字串消毒與它的重要性。


可列印模式 轉寄給朋友

The XOOPS organisation ("XOOPS") is committed to protecting the privacy of users of the XOOPS.org sites.
The website used Ver.2.4.5 POWERED BY XOOPS PROJECT.