新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
php怎么向數(shù)據(jù)庫增加數(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;
}
}
?> 當前名稱:php怎么向數(shù)據(jù)庫增加數(shù)據(jù)
文章地址:http://m.fisionsoft.com.cn/article/cdsishs.html


咨詢
建站咨詢
