這篇文章主要介紹“php如何向數(shù)據(jù)庫增加數(shù)據(jù)”,在日常操作中,相信很多人在php如何向數(shù)據(jù)庫增加數(shù)據(jù)問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”php如何向數(shù)據(jù)庫增加數(shù)據(jù)”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
成都創(chuàng)新互聯(lián)公司成立于2013年,我們提供高端成都網(wǎng)站建設(shè)、成都網(wǎng)站制作公司、網(wǎng)站設(shè)計、網(wǎng)站定制、全網(wǎng)整合營銷推廣、微信平臺小程序開發(fā)、微信公眾號開發(fā)、seo優(yōu)化排名服務(wù),提供專業(yè)營銷思路、內(nèi)容策劃、視覺設(shè)計、程序開發(fā)來完成項目落地,為軟裝設(shè)計企業(yè)提供源源不斷的流量和訂單咨詢。
php向數(shù)據(jù)庫增加數(shù)據(jù)的方法:1、聲明字符編碼;2、連接數(shù)據(jù)庫;3、獲取數(shù)據(jù);4、通過“insert into user_info(userId,userName,phoneNumber,userScore)...”插入數(shù)據(jù)即可。

本文操作環(huán)境:windows7系統(tǒng)、PHP7.1版、DELL G3電腦
php怎么向數(shù)據(jù)庫增加數(shù)據(jù)?
php往數(shù)據(jù)庫添加數(shù)據(jù)
前端代碼:
function submit_result() {
$.post(
"Controllers/ajaxController.php",
{
"name": $("#name").val(),
"mobile": $("#mobile").val(),
"score": $("#sp_score").html()
},
function(msg) {
if (msg == "0") {
layer.open({
content: '您已參與過該活動,下次再參與哦!',
btn: ['知道了']
});
} else {
layer.open({
content: '恭喜您,獲得了,'+msg,
btn: ['知道了'],
end:function(){
location.href="index.html";
}
});
}
}
);PHP代碼
<?
//1. 聲明字符編碼
header("Content-Type:text/html;charset=utf8");
//2. 連接數(shù)據(jù)庫
$link=MySQL_connect("localhost","root","root");//連接數(shù)據(jù)庫
if(!$link) echo "系統(tǒng)異常,請稍后再試";//如果連接數(shù)據(jù)庫失敗
mysql_select_db("test", $link); //選擇數(shù)據(jù)庫
mysql_query("set names 'utf8'"); // 解決中文亂碼
//3. 獲取數(shù)據(jù)
$name = $_POST["name"];
$phone = $_POST["mobile"];
$score=$_POST["score"];
//4. 查詢手機號碼是否存
$strsql = "select phoneNumber from user_info where phoneNumber='$phone'";
mysql_query("SET NAMES utf8");
$result=@mysql_query($strsql);//執(zhí)行查詢
$row=mysql_fetch_array($result);//獲取數(shù)據(jù)行
//5. 根據(jù)是否返回數(shù)據(jù)行,如果數(shù)據(jù)行為空,即已參與活動,否則返回獲得獎勵等級
if(!empty($row)){
//5.1 數(shù)據(jù)行不為空,返回0
echo 0;
}else{
//5.2 數(shù)據(jù)行為空,手機號碼沒有參與活動,插入數(shù)據(jù)到數(shù)據(jù)庫
$strsql = "insert into user_info(userId,userName,phoneNumber,userScore,dataTime) values(null,'$name','$phone','$score',now())";
$result = @mysql_query($strsql);
//5.3. 成功添加
if($result)
{
if($score>=100) echo "一等獎";
if($score<100&&$score>=60) echo "二等獎";
if($score<60) echo "三等獎";
exit;
}
}
?>到此,關(guān)于“php如何向數(shù)據(jù)庫增加數(shù)據(jù)”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
網(wǎng)頁題目:php如何向數(shù)據(jù)庫增加數(shù)據(jù)
本文URL:http://chinadenli.net/article44/ipdhhe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、域名注冊、網(wǎng)站改版、靜態(tài)網(wǎng)站、App設(shè)計、全網(wǎng)營銷推廣
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)