做個php插入數(shù)據(jù)庫 php往數(shù)據(jù)庫添加數(shù)據(jù)

PHP加數(shù)據(jù)庫

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

成都創(chuàng)新互聯(lián)公司是專業(yè)的郊區(qū)網(wǎng)站建設(shè)公司,郊區(qū)接單;提供成都網(wǎng)站建設(shè)、做網(wǎng)站,網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行郊區(qū)網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊,希望更多企業(yè)前來合作!

現(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)用戶點(diǎn)擊上例中 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怎么連接mysql數(shù)據(jù)庫并導(dǎo)入數(shù)據(jù)?

方法/步驟

1

登錄到phpMyAdmin

2

新建一個要導(dǎo)入的數(shù)據(jù)庫,點(diǎn)擊“+new”

3

如圖,分別填寫數(shù)據(jù)庫名稱,以及選擇數(shù)據(jù)庫的排序規(guī)則,

4

完成步驟3,點(diǎn)擊“創(chuàng)建”

5

完成步驟4,從右側(cè)就可以看到我們新創(chuàng)建的數(shù)據(jù)庫了,如果沒有立即顯示,刷新即可立馬顯示了。

6

單擊我們新創(chuàng)建的數(shù)據(jù)庫,

7

然后,我們選擇“導(dǎo)入”,

8

完成步驟7,我們點(diǎn)擊“選擇文件”,

9

點(diǎn)擊了“選擇文件”之后,就會出現(xiàn)如圖所示的彈出框,我們選擇要導(dǎo)入的sql就可以了,后綴名可以是.sql,也可以是壓縮文件.zip。如圖,選擇好文件之后,點(diǎn)擊“確定”就可以了

10

完成步驟⑨,你就可以從剛才的頁面中看到自己上傳的文件了,如圖所示,其余選項默認(rèn)就可以了,然后點(diǎn)擊“執(zhí)行”就可以了。

11

這個時候,你就可以看到你的數(shù)據(jù)已經(jīng)導(dǎo)入到你新創(chuàng)建的數(shù)據(jù)庫中了

用php代碼怎么往數(shù)據(jù)庫里自定義插入數(shù)據(jù)

現(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ù)據(jù)被發(fā)送到

"insert.php"。"insert.php"

文件連接數(shù)據(jù)庫,并通過

$_POST

變量從表單取回值。然后,mysql_query()

函數(shù)執(zhí)行

INSERT

INTO

語句,一條新的記錄會添加到數(shù)據(jù)庫表中。

php向數(shù)據(jù)庫插入一條數(shù)據(jù)代碼 急急急?。。?!

$rs

=

mysql_query($sql);

這一段改成:

if(mysql_query($sql)){

echo

"script

language=JavaScriptalert('數(shù)據(jù)庫提交成功!');window.location.href='team.php';/script";

}else{

echo

"插入失敗,錯誤原因是{mysql_error()}";

}

然后根據(jù)錯誤原因解決問題,或者把錯誤原因給大家看看。

如果仍然提示成功,請檢查你的權(quán)限,還有你的mysql數(shù)據(jù)庫Team這個表里的主鍵有沒有重復(fù)?

php如何寫入數(shù)據(jù)庫

數(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);//加逗號

the result : Hello,World,I,love,Shanghai

轉(zhuǎn)換數(shù)組為字符串后插入數(shù)據(jù)庫就可以了。

php怎么把數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫

需要PHP基礎(chǔ)知識和數(shù)據(jù)庫基礎(chǔ)知識。

以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ù)分別是服務(wù)器名,連接賬號,連接密碼。

連接之后,可以使用mysql_select_db()設(shè)置要處理的數(shù)據(jù)庫,后面則是用數(shù)據(jù)庫語句處理數(shù)據(jù)。SQL語法簡介網(wǎng)頁鏈接

本文標(biāo)題:做個php插入數(shù)據(jù)庫 php往數(shù)據(jù)庫添加數(shù)據(jù)
地址分享:http://muchs.cn/article30/doscgso.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、網(wǎng)站制作微信公眾號、定制開發(fā)、全網(wǎng)營銷推廣、軟件開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作