溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

php如何向數(shù)據(jù)庫增加數(shù)據(jù)

發(fā)布時間:2021-12-31 09:35:35 來源:億速云 閱讀:367 作者:iii 欄目:編程語言

這篇文章主要介紹“php如何向數(shù)據(jù)庫增加數(shù)據(jù)”,在日常操作中,相信很多人在php如何向數(shù)據(jù)庫增加數(shù)據(jù)問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”php如何向數(shù)據(jù)庫增加數(shù)據(jù)”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!

php向數(shù)據(jù)庫增加數(shù)據(jù)的方法:1、聲明字符編碼;2、連接數(shù)據(jù)庫;3、獲取數(shù)據(jù);4、通過“insert into user_info(userId,userName,phoneNumber,userScore)...”插入數(shù)據(jù)即可。

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;
    }
}
  
 
?>

到此,關(guān)于“php如何向數(shù)據(jù)庫增加數(shù)據(jù)”的學習就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI