把來自表單的數(shù)據(jù)插入數(shù)據(jù)庫
成都創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比永福網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式永福網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋永福地區(qū)。費用合理售后完善,十多年實體公司更值得信賴。
現(xiàn)在,我們創(chuàng)建一個 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
當(dāng)用戶點擊上例中 HTML 表單中的提交按鈕時,表單數(shù)據(jù)被發(fā)送到 "insert.php"。"insert.php" 文件連接數(shù)據(jù)庫,并通過 $_POST 變量從表單取回值。然后,mysql_query() 函數(shù)執(zhí)行 INSERT INTO 語句,一條新的記錄會添加到數(shù)據(jù)庫表中。
下面是 "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)
?
把來自表單的數(shù)據(jù)插入數(shù)據(jù)庫
現(xiàn)在,我們創(chuàng)建一個 HTML 表單,這個表單可把新記錄插入 "Persons" 表。
這是這個 HTML 表單:
1
2
3
4
5
6
7
8
9
10
11
12
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
當(dāng)用戶點擊上例中 HTML 表單中的提交按鈕時,表單數(shù)據(jù)被發(fā)送到 "insert.php"。"insert.php" 文件連接數(shù)據(jù)庫,并通過 $_POST 變量從表單取回值。然后,mysql_query() 函數(shù)執(zhí)行 INSERT INTO 語句,一條新的記錄會添加到數(shù)據(jù)庫表中。
下面是 "insert.php" 頁面的代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
?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)
?
本文實例講述了PHP實現(xiàn)的pdo連接數(shù)據(jù)庫并插入數(shù)據(jù)功能。分享給大家供大家參考,具體如下:
創(chuàng)建配置文件
pdo_config.php
?php
$db_Type
=
"mysql";//數(shù)據(jù)庫類型
$host
=
"localhost";//主機名
$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.com')";
$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è)計技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
您可能感興趣的文章:關(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實現(xiàn)數(shù)據(jù)庫添加內(nèi)容的方法tp5(thinkPHP5)框架數(shù)據(jù)庫Db增刪改查常見操作總結(jié)PHP利用pdo_odbc實現(xiàn)連接數(shù)據(jù)庫示例【基于ThinkPHP5.1搭建的項目】
本文實例講述了php使用mysqli向數(shù)據(jù)庫添加數(shù)據(jù)的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:
$mydb
=
new
mysqli('localhost',
'username',
'password',
'databasename');
$sql
=
"INSERT
INTO
users
(fname,
lname,
comments)
VALUES
('$_POST[fname]',
'$_POST[lname]',
'$_POST[comments]')";
if
($mydb-query($sql)
==
TRUE)
{
echo
"user
entry
saved
successfully.";
}
else
{
echo
"INSERT
attempt
failed"
;
}
$mydb-close();
希望本文所述對大家的php程序設(shè)計有所幫助。
文章題目:php實現(xiàn)增加數(shù)據(jù)庫 php中數(shù)據(jù)庫怎么創(chuàng)建
網(wǎng)頁鏈接:http://vcdvsql.cn/article8/dopiiip.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊、軟件開發(fā)、建站公司、自適應(yīng)網(wǎng)站、品牌網(wǎng)站制作、搜索引擎優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)