十年網(wǎng)站開發(fā)經(jīng)驗(yàn) + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊(duì)
量身定制 + 運(yùn)營維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
先創(chuàng)建一個php頁面,例如叫doup.php,在其中穿件一個form:

成都創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供鹽津網(wǎng)站建設(shè)、鹽津做網(wǎng)站、鹽津網(wǎng)站設(shè)計(jì)、鹽津網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、鹽津企業(yè)網(wǎng)站模板建站服務(wù),十載鹽津做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
form enctype="multipart/form-data" action="doup.php" method="POST"
lable
input name="sdd" type="text" /
input name="sdd" type="submit" value="提交"/
/lable
/form
?php
$conn=mysql_connect("localhost","root","123")or die("連接錯誤".mysql_error());
mysql_select_db("數(shù)據(jù)庫名",$conn);
$myvalue=$_post["sdd"];
$sql="insert into dfgghy(myvalue) value('$myvalue')";
$re=mysql_querry($sql);
?
現(xiàn)在,我們創(chuàng)建一個
HTML
表單,這個表單可把新記錄插入
"Persons"
表。
這是這個
HTML
表單:
123456789101112
htmlbody
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
當(dāng)用戶點(diǎn)擊上例中
HTML
表單中的提交按鈕時(shí),表單數(shù)據(jù)被發(fā)送到
"insert.php"。"insert.php"
文件連接數(shù)據(jù)庫,并通過
$_POST
變量從表單取回值。然后,mysql_query()
函數(shù)執(zhí)行
INSERT
INTO
語句,一條新的記錄會添加到數(shù)據(jù)庫表中。
本文實(shí)例講述了PHP實(shí)現(xiàn)的pdo連接數(shù)據(jù)庫并插入數(shù)據(jù)功能。分享給大家供大家參考,具體如下:
創(chuàng)建配置文件
pdo_config.php
?php
$db_Type
=
"mysql";//數(shù)據(jù)庫類型
$host
=
"localhost";//主機(jī)名
$dbName
=
"test";//數(shù)據(jù)庫名
$userName
=
"root";//用戶名
$password
=
"root";//密碼
$dsn
=
"{$db_Type}:host={$host};dbname={$dbName}";
?
pdo插入數(shù)據(jù)庫
pdo_insert.php
?php
header('Content-type:text/html;
charset=utf-8');
require
'pdo_config.php';
try{
$pdo
=
new
PDO
($dsn,$userName,$password);//創(chuàng)建一個連接對象
$pdo-exec('set
names
utf8');//設(shè)置編碼
$sql
=
"INSERT
student
(name,email)
VALUES
('李四','123@qq點(diǎn)抗 ')";
$pdo-exec($sql);
}catch
(PDOException
$e){
die('操作失敗'.$e-getMessage());
}
//關(guān)閉連接
$pdo
=
null;
?
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP基于pdo操作數(shù)據(jù)庫技巧總結(jié)》、《php+mysqli數(shù)據(jù)庫程序設(shè)計(jì)技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:關(guān)于php連接mssql:pdo
odbc
sql
serverPHP5中使用PDO連接數(shù)據(jù)庫的方法PHP中PDO連接數(shù)據(jù)庫中各種DNS設(shè)置方法小結(jié)ThinkPHP框架基于PDO方式連接數(shù)據(jù)庫操作示例PHP使用ODBC連接數(shù)據(jù)庫的方法tp5(thinkPHP5)框架連接數(shù)據(jù)庫的方法示例PHP7使用ODBC連接SQL
Server2008
R2數(shù)據(jù)庫示例【基于thinkPHP5.1框架】tp5(thinkPHP5)操作mongoDB數(shù)據(jù)庫的方法thinkPHP5實(shí)現(xiàn)數(shù)據(jù)庫添加內(nèi)容的方法tp5(thinkPHP5)框架數(shù)據(jù)庫Db增刪改查常見操作總結(jié)PHP利用pdo_odbc實(shí)現(xiàn)連接數(shù)據(jù)庫示例【基于ThinkPHP5.1搭建的項(xiàng)目】