当前位置:Gxlcms > 数据库问题 > phpStudy3——往数据库中添加数据

phpStudy3——往数据库中添加数据

时间:2021-07-01 10:21:17 帮助过:2人阅读

function submit_result() { 2 $.post( 3 "Controllers/ajaxController.php", 4 { 5 "name": $("#name").val(), 6 "mobile": $("#mobile").val(), 7 "score": $("#sp_score").html() 8 }, 9 function(msg) { 10 if (msg == "0") { 11 layer.open({ 12 content: ‘您已参与过该活动,下次再参与哦!‘, 13 btn: [‘知道了‘] 14 }); 15 } else { 16 layer.open({ 17 content: ‘恭喜您,获得了,‘+msg, 18 btn: [‘知道了‘], 19 end:function(){ 20 location.href="index.html"; 21 } 22 }); 23 } 24 } 25 );

 

后端php页面:

 1 <?
 2 //1. 声明字符编码
 3 header("Content-Type:text/html;charset=utf8"); 
 4 
 5 //2. 连接数据库
 6 $link=mysql_connect("localhost","root","root");//连接数据库
 7 if(!$link) echo "系统异常,请稍后再试";//如果连接数据库失败
 8 mysql_select_db("test", $link); //选择数据库
 9 mysql_query("set names ‘utf8‘");  // 解决中文乱码
10 
11 //3. 获取数据
12 $name = $_POST["name"];
13 $phone = $_POST["mobile"];
14 $score=$_POST["score"];
15 
16 //4. 查询手机号码是否存
17 $strsql = "select phoneNumber from user_info where phoneNumber=‘$phone‘";
18 mysql_query("SET NAMES utf8");
19 $result=@mysql_query($strsql);//执行查询
20 $row=mysql_fetch_array($result);//获取数据行
21 
22 //5. 根据是否返回数据行,如果数据行为空,即已参与活动,否则返回获得奖励等级
23 if(!empty($row)){
24     //5.1 数据行不为空,返回0
25     echo 0;
26 }else{
27     //5.2 数据行为空,手机号码没有参与活动,插入数据到数据库
28     $strsql = "insert into user_info(userId,userName,phoneNumber,userScore,dataTime) values(null,‘$name‘,‘$phone‘,‘$score‘,now())";
29     $result = @mysql_query($strsql);
30     
31     //5.3. 成功添加
32     if($result)
33     {
34         if($score>=100) echo "一等奖";
35         if($score<100&&$score>=60) echo "二等奖";
36         if($score<60) echo "三等奖";
37         exit;
38     }
39 }
40   
41 
42 ?>

 

phpStudy3——往数据库中添加数据

标签:

人气教程排行