把來自表單的數據插入數據庫
陽城網站制作公司哪家好,找創新互聯建站!從網頁設計、網站建設、微信開發、APP開發、成都響應式網站建設公司等網站項目制作,到程序開發,運營維護。創新互聯建站自2013年起到現在10年的時間,我們擁有了豐富的建站經驗和運維經驗,來保證我們的工作的順利進行。專注于網站建設就選創新互聯建站。
現在,我們創建一個 HTML 表單,這個表單可把新記錄插入 "Persons" 表。
這是這個 HTML 表單:
html
body
form?action="insert.php"?method="post"
Firstname:?input?type="text"?name="firstname"?/
Lastname:?input?type="text"?name="lastname"?/
Age:?input?type="text"?name="age"?/
input?type="submit"?/
/form
/body
/html
當用戶點擊上例中 HTML 表單中的提交按鈕時,表單數據被發送到 "insert.php"。"insert.php" 文件連接數據庫,并通過 $_POST 變量從表單取回值。然后,mysql_query() 函數執行 INSERT INTO 語句,一條新的記錄會添加到數據庫表中。
下面是 "insert.php" 頁面的代碼:
?php
$con?=?mysql_connect("localhost","peter","abc123");
if?(!$con)
{
die('Could?not?connect:?'?.?mysql_error());
}
mysql_select_db("my_db",?$con);
$sql="INSERT?INTO?Persons?(FirstName,?LastName,?Age)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";
if?(!mysql_query($sql,$con))
{
die('Error:?'?.?mysql_error());
}
echo?"1?record?added";
mysql_close($con)
?
常規方式
常規方式就是按部就班的讀取文件了。其余的話和上述方案一致。
// 讀取配置文件內容
$handle = fopen("filepath", "r"); ? ? ? ? ? ?$content = fread($handle, filesize("filepath"));123
PHP解析XML
上述兩種讀取文件,其實都是為了PHP解析XML來做準備的。關于PHP解析XML的方式的博客有很多。方式也有很多,像simplexml,XMLReader,DOM啦等等。但是對于比較小型的xml配置文件,simplexml就足夠了。
配置文件
?xml version="1.0" encoding="UTF-8" ?mysql
!-- 為防止出現意外,請按照此標準順序書寫.其實也無所謂了 --
hostlocalhost/host
userroot/user
password123456/password
dbtest/db
port3306/port/mysql12345678910
解析
?php/**
* 作為解析XML配置文件必備工具
*/class XMLUtil {
public static $dbconfigpath = "./db.config.xml"; ? ?public static function getDBConfiguration() {
$dbconfig = array (); ? ? ? ?try { ? ? ? ? ? ?// 讀取配置文件內容
$handle = fopen(self::$dbconfigpath, "r"); ? ? ? ? ? ?$content = fread($handle, filesize(self::$dbconfigpath)); ? ? ? ? ? ?// 獲取xml文檔根節點,進而獲取相關的數據庫信息
$mysql = simplexml_load_string($content); ? ? ? ? ? ?// 將獲取到的xml節點信息賦值給關聯數組,方便接下來的方法調用
$dbconfig['host'] = $mysql-host; ? ? ? ? ? ?$dbconfig['user'] = $mysql-user; ? ? ? ? ? ?$dbconfig['password'] = $mysql-password; ? ? ? ? ? ?$dbconfig['db'] = $mysql-db; ? ? ? ? ? ?$dbconfig['port'] = $mysql-port; ? ? ? ? ? ?// 將配置信息以關聯數組的形式返回
return $dbconfig;
} catch ( Exception $e ) { ? ? ? ? ? ?throw new RuntimeException ( "mark讀取數據庫配置文件信息出錯!/markbr /" );
} ? ? ? ?return $dbconfig;
}
}1234567891011121314151617181920212223242526272829
數據庫連接池
對于PHP程序而言,優化永無止境。而數據庫連接池就在一定程度上起到了優化的作用。其使得對用戶的每一個請求而言,無需每次都像數據庫申請鏈接資源。而是通過已存在的數據庫連接池中的鏈接來返回,從時間上,效率上,都是一個大大的提升。
于是,這里簡單的模擬了一下數據庫連接池的實現。核心在于維護一個“池”。
從池子中取,用畢,歸還給池子。
?php/**x
* ?PHP中的數據庫 工具類設計
* ?郭璞
* ?2016年12月23日
*
**/class DbHelper { ? ?private $dbconfig; ? ?private $dbpool; ? ?public $poolsize; ? ?public function __construct($poolsize = 20) { ? ? ? ?if (! file_exists ( "./utils.php" )) { ? ? ? ? ? ?throw new RuntimeException ( "markutils.php文件丟失,無法進行配置文件的初始化操作!/markbr /" );
}else {
require './utils.php';
} ? ? ? ?// 初始化 配置文件信息
$this-dbconfig = XMLUtil::getDBConfiguration (); ? ? ? ?// 準備好數據庫連接池“偽隊列”
$this-poolsize = $poolsize;
$this-dbpool = array (); ? ? ? ?for($index = 1; $index = $this-poolsize; $index ++) {
$conn = mysqli_connect ( $this-dbconfig ['host'], $this-dbconfig ['user'], $this-dbconfig ['password'], $this-dbconfig ['db'] ) or die ( "mark連接數據庫失敗!/markbr /" );
array_push ( $this-dbpool, $conn );
}
} ? ?/**
* 從數據庫連接池中獲取一個數據庫鏈接資源
*
* @throws ErrorException
* @return mixed
*/
public function getConn() { ? ? ? ?if (count ( $this-dbpool ) = 0) { ? ? ? ? ? ?throw new ErrorException ( "mark數據庫連接池中已無鏈接資源,請稍后重試!/mark" );
} else { ? ? ? ? ? ?return array_pop ( $this-dbpool );
}
} ? ?/**
* 將用完的數據庫鏈接資源放回到數據庫連接池
*
* @param unknown $conn
* @throws ErrorException
*/
public function release($conn) { ? ? ? ?if (count ( $this-dbpool ) = $this-poolsize) { ? ? ? ? ? ?throw new ErrorException ( "mark數據庫連接池已滿/markbr /" );
} else {
array_push ( $this-dbpool, $conn );
}
}
}
你寫的這個只是數據庫連接的代碼,你只是連接了數據庫,可以對你的“”數據庫進行"CURD"操作,$conn返回的是resource,mysql_select_db()和
mysql_query()返回的則是布爾類型,所以在瀏覽器預覽的時候是沒有任何內容的,有內容也只是一個TRUE
連接數據庫的代碼如下:
數據庫操作類
class
mysql
{
private
$db_host;
//數據庫主機
private
$db_user;
//數據庫用戶名
private
$db_pwd;
//數據庫密碼
private
$db_database;
//數據庫名
private
$conn;
//數據庫連接標識;
private
$sql;
//sql執行的語句
private
$result;
//query的資源標識符
private
$coding;
//數據庫編碼,gbk,utf8,gb2312
private
$show_error
=
true;
//本地調試使用,打印錯誤
public
function
__construct($db_host,
$db_user,
$db_pwd,
$db_database,
$coding)
{
$this-db_host
=
$db_host;
$this-db_user
=
$db_user;
$this-db_pwd
=
$db_pwd;
$this-db_database
=
$db_database;
$this-coding
=
$coding;
$this-connect();
}
private
function
connect()
{
$this-conn
=
@mysql_connect($this-db_host,
$this-db_user,
$this-db_pwd);
if
(!$this-conn)
{
//show_error開啟時,打印錯誤
if
($this-show_error)
{
$this-show_error('錯誤提示:鏈接數據庫失敗!');
}
}
if
(!@mysql_select_db($this-db_database,
$this-conn))
{
//打開數據庫失敗
if
($this-show_error)
{
$this-show_error('錯誤提示:打開數據庫失敗!');
}
}
if
(!@mysql_query("set
names
$this-coding"))
{
//設置編碼失敗
if
($this-show_error)
{
$this-show_error('錯誤提示:設置編碼失敗!');
}
}
}
}
首先登陸要用用戶名,密碼等等信息,這些都要存在數據庫中,這樣才能用戶登陸時驗證是否能夠登陸,
1.創建一個數據庫,建表例表一(用戶名,密碼)等等。。
1.設計頁面代碼包含連接數據庫,數據庫操作代碼等等(加入用戶,刪除用戶。。)
2.代碼中包含用戶登陸輸入
用戶名,密碼
3.打開數據庫表,查找是否有該用戶名,如果存在,密碼是否正確,不正確byebye
(請看java書本
數據庫內容,包含建立數據庫,添加刪除基本操作)
網站題目:php網頁數據庫數據 php中數據庫怎么設計
轉載注明:http://vcdvsql.cn/article8/dopegip.html
成都網站建設公司_創新互聯,為您提供網站設計、域名注冊、微信小程序、手機網站建設、Google、網站策劃
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯