需要PHP基礎知識和數(shù)據(jù)庫基礎知識。
專注于為中小企業(yè)提供成都網(wǎng)站設計、成都做網(wǎng)站、外貿(mào)網(wǎng)站建設服務,電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)海棠免費做網(wǎng)站提供優(yōu)質(zhì)的服務。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了數(shù)千家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設實現(xiàn)規(guī)模擴充和轉(zhuǎn)變。
以SQL為例。使用PHP MySQL 函數(shù)可以編輯數(shù)據(jù)庫。
mysql_connect() 函數(shù)打開MySQL 連接。舉例
?php
$con = mysql_connect("localhost","mysql_user","mysql_pwd");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}// 一些代碼...mysql_close($con);
?
mysql_connect()三個參數(shù)分別是服務器名,連接賬號,連接密碼。
連接之后,可以使用mysql_select_db()設置要處理的數(shù)據(jù)庫,后面則是用數(shù)據(jù)庫語句處理數(shù)據(jù)。SQL語法簡介網(wǎng)頁鏈接
把來自表單的數(shù)據(jù)插入數(shù)據(jù)庫
現(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
當用戶點擊上例中 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)
?
INSERT INTO msg(title,contents,dates) VALUES ($title,$cons,now())
將字段名兩邊的單引號去掉就沒問題了,親測成功!希望能幫到你。
把來自表單的數(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
當用戶點擊上例中 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)
?
現(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
當用戶點擊上例中
HTML
表單中的提交按鈕時,表單數(shù)據(jù)被發(fā)送到
"insert.php"。"insert.php"
文件連接數(shù)據(jù)庫,并通過
$_POST
變量從表單取回值。然后,mysql_query()
函數(shù)執(zhí)行
INSERT
INTO
語句,一條新的記錄會添加到數(shù)據(jù)庫表中。
網(wǎng)站欄目:php向數(shù)據(jù)庫插入 php數(shù)據(jù)庫怎么導入
文章鏈接:http://chinadenli.net/article12/hpejgc.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供微信公眾號、定制網(wǎng)站、品牌網(wǎng)站制作、品牌網(wǎng)站設計、外貿(mào)網(wǎng)站建設、品牌網(wǎng)站建設
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)