把來自表單的數(shù)據(jù)插入數(shù)據(jù)庫

專注于為中小企業(yè)提供成都做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)盤山免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了1000多家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
現(xiàn)在,我們創(chuàng)建一個(gè) HTML 表單,這個(gè)表單可把新記錄插入 "Persons" 表。
這是這個(gè) 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)用戶點(diǎn)擊上例中 HTML 表單中的提交按鈕時(shí),表單數(shù)據(jù)被發(fā)送到 "insert.php"。"insert.php" 文件連接數(shù)據(jù)庫,并通過 $_POST 變量從表單取回值。然后,mysql_query() 函數(shù)執(zhí)行 INSERT INTO 語句,一條新的記錄會(huì)添加到數(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)
?
$value?=?'';
$query_num?=?5;?//插入數(shù)量
for($i=1;$i=$query_num;$i++){
$value?.=?"('25','1')";
}
//mysql?insert有插入多條語法,拼接sql語句,table_name表名???
$sql?=?"insert?into?table_name?(memid,online)?values?".$value;
//執(zhí)行,插入$query_num條數(shù)據(jù)
mysql_query($sql);
數(shù)組吧,直接把數(shù)組轉(zhuǎn)字符串啊
implode() 函數(shù)返回由數(shù)組元素組合成的字符串。(適合一維數(shù)組)
$arr = array('Hello', 'World', 'I', 'love', 'Shanghai');
1 echo implode(" ",$arr);//加空格
the result : Hello World I love Shanghai
2 echo implode(",",$arr);//加逗號(hào)
the result : Hello,World,I,love,Shanghai
轉(zhuǎn)換數(shù)組為字符串后插入數(shù)據(jù)庫就可以了。
現(xiàn)在,我們創(chuàng)建一個(gè)
HTML
表單,這個(gè)表單可把新記錄插入
"Persons"
表。
這是這個(gè)
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
語句,一條新的記錄會(huì)添加到數(shù)據(jù)庫表中。
PHP向MySQL數(shù)據(jù)庫中寫入數(shù)據(jù)有三個(gè)步驟:
1,PHP和MySQL建立連接關(guān)系
2,打開MySQL數(shù)據(jù)庫
3,接受頁面數(shù)據(jù),PHP錄入到指定的表中
1、2兩步可直接使用一個(gè)數(shù)據(jù)庫鏈接文件即可:conn.php
代碼如下
?php
mysql_connect("localhost","root","");//連接MySQL
mysql_select_db("hello");//選擇數(shù)據(jù)庫
?
當(dāng)然,前提是已經(jīng)安裝WEB服務(wù)器、PHP和MySQL,并且建立MySQL表“cnbruce”
mysql_connect()中三個(gè)參數(shù)分別為MySQL地址、MySQL用戶名和MySQL密碼
然后就是通過WEB頁面?zhèn)鬟f數(shù)據(jù),讓PHP通過SQL語句將數(shù)據(jù)寫入MySQL數(shù)據(jù)庫指定的表中,比如新建文件 post.php
代碼如下
?php
require_once("conn.php");//引用數(shù)據(jù)庫鏈接文件
$uname = $_GET['n'];//GET方法為URL參數(shù)傳遞
$psw = $_GET['p'];
$psw=md5($psw);//直接使用MD5加密
$sql = "insert into members(username,password) values ('$uname','$psw')";
mysql_query($sql);//借SQL語句插入數(shù)據(jù)
mysql_close();//關(guān)閉MySQL連接
echo "成功錄入數(shù)據(jù)";
?
測試頁面: ;p=i0514
即可向MySQL數(shù)據(jù)庫hello的members表中插入新的數(shù)據(jù)“cnbruce”到username字段、“i0514”到password字段
補(bǔ)充:讀取表
讀取表中的內(nèi)容,這里我們用while,可以根據(jù)具體情況,用for 或其他的.
代碼如下
while($row = mysql_fetch_array($result))
{
echo "div style="height:24px; line-height:24px; font-weight:bold;""; //排版代碼
echo $row['Topic'] . "br/";
echo "/div"; //排版代碼
當(dāng)前文章:php數(shù)據(jù)庫追加寫入,php寫入數(shù)據(jù)到數(shù)據(jù)庫
分享地址:http://chinadenli.net/article36/hshpsg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、電子商務(wù)、品牌網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)、網(wǎng)站營銷、企業(yè)網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)