新聞中心
PHP在網(wǎng)站上實(shí)現(xiàn)跟數(shù)據(jù)庫添加數(shù)據(jù)
把來自表單的數(shù)據(jù)插入數(shù)據(jù)庫
成都創(chuàng)新互聯(lián)是專業(yè)的阿克陶網(wǎng)站建設(shè)公司,阿克陶接單;提供成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè),網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行阿克陶網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!
現(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 語句,一條新的記錄會添加到數(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)
?
php數(shù)據(jù)庫添加、刪除、修改數(shù)據(jù)(mysql)
一、PHP操作MySql數(shù)據(jù)庫
新增數(shù)據(jù)
?php
$query
=
"INSERT
INTO
grade
(name,email,point,regdate)
VALUE
('
李三','yc60.com@gmail.com',,NOW())"
;
@mysql_query($query)
or
die(
'添加數(shù)據(jù)出錯:'
.mysql_error());
?
修改數(shù)據(jù)
?php
$query
=
"UPDATE
grade
SET
name='小可愛'
WHERE
id=6"
;
@mysql_query($query)
or
die(
'修改出錯:'
.mysql_error());
?
刪除數(shù)據(jù)
?php
$query
=
"DELETE
FROM
grade
WHERE
id=6";
@mysql_query($query)
or
die(
'刪除錯誤:'
.mysql_error());
?
顯示數(shù)據(jù)
?php
$query
=
"SELECT
id,name,email,point
FROM
grade";
$result
=
@mysql_query($query)
or
die(
'查詢語句出錯:'
.mysql_error());
while
(!!
$row
=
mysql_fetch_array($result))
{
echo
$row[
'id'
].
'----'
.$row['name'
].'----'
.$row
['email'
].
'----'
.$row['point'
];
echo
'br
/
';
}
?
二、其他常用函數(shù)
mysql_f
etch_row()
:從結(jié)果集中取得一行作為枚舉數(shù)組
mysql_f
etch_assoc()
:
從結(jié)果集中取得一行作為關(guān)聯(lián)數(shù)組
mysql_f
etch_array()
:
從結(jié)果集中取得一行作為關(guān)聯(lián)數(shù)組,或數(shù)字?jǐn)?shù)組,或二者兼有
mysql_f
etch_lengths
()
:
取得結(jié)果集中每個(gè)輸出的長度
mysql_f
ield_name():
取得結(jié)果中指定字段的字段名
mysql_num_rows():
取得結(jié)果集中行的數(shù)目
mysql_num_f
ields():取得結(jié)果集中字段的數(shù)目
mysql_get_client_inf
o()
:
取得
MySQL
客戶端信息
mysql_get_host_info():
取得
MySQL
主機(jī)信息
mysql_get_proto_info():
取得
MySQL
協(xié)議信息
mysql_get_server_inf
o()
:
取得
MySQL
服務(wù)器信息
PHP加數(shù)據(jù)庫
把來自表單的數(shù)據(jù)插入數(shù)據(jù)庫
現(xiàn)在,我們創(chuàng)建一個(gè) HTML 表單,這個(gè)表單可把新記錄插入 "Persons" 表。
這是這個(gè) 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)用戶點(diǎn)擊上例中 HTML 表單中的提交按鈕時(shí),表單數(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代碼怎么往數(shù)據(jù)庫里自定義插入數(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
語句,一條新的記錄會添加到數(shù)據(jù)庫表中。
新聞標(biāo)題:數(shù)據(jù)庫添加數(shù)據(jù)php,數(shù)據(jù)庫添加數(shù)據(jù)sql語句
當(dāng)前URL:http://www.ef60e0e.cn/article/hsiepc.html