?php
創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司一直秉承“誠信做人,踏實(shí)做事”的原則,不欺瞞客戶,是我們最起碼的底線! 以服務(wù)為基礎(chǔ),以質(zhì)量求生存,以技術(shù)求發(fā)展,成交一個(gè)客戶多一個(gè)朋友!專注中小微企業(yè)官網(wǎng)定制,成都做網(wǎng)站、網(wǎng)站制作、成都外貿(mào)網(wǎng)站建設(shè),塑造企業(yè)網(wǎng)絡(luò)形象打造互聯(lián)網(wǎng)企業(yè)效應(yīng)。
//?以?MySQL?為例:
mysql_connect('127.0.0.1',?'root',?'root',?3306);??//?連接數(shù)據(jù)庫
mysql_select_db('test');???????????????????????????//?選擇數(shù)據(jù)庫
mysql_query('set?names?utf8');?????????????????????//?執(zhí)行SQL
//?插入數(shù)據(jù)語句
$sql?=?"INSERT?INTO?table?(username,?password)?VALUES?('Jack@163.com',?'123456')";
$r?=?mysql_query($sql);
if?(mysql_affected_rows())?{
echo?'新增成功';
}?else?{
echo?mysql_error();
}
需要PHP基礎(chǔ)知識(shí)和數(shù)據(jù)庫基礎(chǔ)知識(shí)。
以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()三個(gè)參數(shù)分別是服務(wù)器名,連接賬號(hào),連接密碼。
連接之后,可以使用mysql_select_db()設(shè)置要處理的數(shù)據(jù)庫,后面則是用數(shù)據(jù)庫語句處理數(shù)據(jù)。SQL語法簡介網(wǎng)頁鏈接
1:首先要使用PHP的超全局變量 $_GET 和 $_POST 用于收集表單數(shù)據(jù)(form-data)
2:然后使用INSERT INTO 語句用于向數(shù)據(jù)庫表中插入新記錄。
具體示例:
(1)首先創(chuàng)建了一個(gè)名為 "Persons" 的表,有三個(gè)列:"Firstname", "Lastname" 以及 "Age"。
?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
mysql_query("INSERT INTO Persons (FirstName, LastName, Age)
VALUES ('Peter', 'Griffin', '35')");
mysql_query("INSERT INTO Persons (FirstName, LastName, Age)
VALUES ('Glenn', 'Quagmire', '33')");
mysql_close($con);
?
(2)其次創(chuàng)建一個(gè) HTML 表單,這個(gè)表單可把新記錄插入 "Persons" 表。
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
(3)接著當(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
$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ù)組吧,直接把數(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ù)庫就可以了。
你可以根據(jù)mysql返回的錯(cuò)誤信息分析。
或者把$sql輸出,然后在其他mysql客戶端執(zhí)行,看看錯(cuò)誤提示。
你要插入數(shù)據(jù)到message表,表名不能用單引號(hào)包著,可能你在看別人的代碼是有`,要看清楚,`與'是不同的,'這個(gè)是單引號(hào),`這個(gè)是主鍵盤1左邊那個(gè)。
從你寫的代碼看來,你還有很大的進(jìn)步空間,有興趣就找我交流交流。
form表單demo:task.html
fieldset id="setFiled"
legend發(fā)布任務(wù)/legend
form action="registr.php" method="post" id="steForm"
label任務(wù)類型:/labelbr
input type="text" name="type"? id="taskType" placeholder="請(qǐng)選擇任務(wù)類型"/br
label酬nbsp;nbsp;金:/labelbr
input type="number" name="money" id="forMoney" min="1" max="1000"/label元/labelbr
label截止時(shí)間:/labelbr
input type="datetime" name="time" id="timeSubmit"/span data-year="" data-month="" data-date="" id="showDate"/spanbr
label詳細(xì)描述:/labelbr
textarea maxlength="512" name="textAray" id="msgArea"/textareabr
input type="submit" name="subMit" id="forSub" value="點(diǎn)擊發(fā)布" /
/form
擴(kuò)展資料
php接收POST數(shù)據(jù)的三種方式
1、$_POST 方式接受數(shù)據(jù)
$_POST 方式是由通過HTTP的POST方法傳遞過來的數(shù)據(jù)組成的數(shù)組,是一個(gè)自動(dòng)全局變量。
注:只能接收Content-Type:application/x-www-form-urlencode提交的數(shù)據(jù)。也就是只能接收表單過來的數(shù)據(jù)。
2、GLOBLES[‘HTTP_RAW_POST_DATA’]
如果訪問原始POST數(shù)據(jù)不是php能夠識(shí)別的文檔類型,比如:text/xml 或者soap等等,可以用$GLOBLES[‘HTTP_RAW_POST_DATA’]來接收,$HTTP_RAW_POST_DATA變量包含有原始POST數(shù)據(jù)。此變量僅在碰到未識(shí)別的MIME數(shù)據(jù)時(shí)產(chǎn)生。
注:$HTTP_RAW_POST_DATA對(duì)于enctype=”multipart/form-data”表單數(shù)據(jù)不可用,也就是說使用$HTTP_RAW_POST_DATA無法接受網(wǎng)頁表單post過來的數(shù)據(jù)。
3、file_get_contents(“php://input”);
如果訪問原始POST數(shù)據(jù),更好的方法是使用file_get_content(“php://input”);對(duì)于未指定Content-Type的POST數(shù)據(jù),可以使用該方法讀取POST原始數(shù)據(jù),包括二進(jìn)制流也可以和$HTTP_RAW_POST_DATA比起來。它帶來的生存眼里更小,并且不需要任何特殊的php.ini設(shè)置。
注:php://input不能用于 enctype=”multipart/form-data”
例如:$postStr = file_get_contents("php://input"); //獲取POST數(shù)據(jù)
當(dāng)前題目:php寫入數(shù)據(jù)庫內(nèi)容 php怎么從數(shù)據(jù)庫讀取數(shù)據(jù)
轉(zhuǎn)載來于:http://chinadenli.net/article32/hgcipc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、網(wǎng)站排名、網(wǎng)頁設(shè)計(jì)公司、小程序開發(fā)、網(wǎng)站設(shè)計(jì)、網(wǎng)站導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)