会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 133936个问题
WEB前端全系列/第五阶段:前后端交互/PHP、数据库编程与设计 152楼
WEB前端全系列/第五阶段:前后端交互/PHP、数据库编程与设计 153楼

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="js/jquery-1.12.3.min.js"></script>
    <style>
        *{
            margin: 0;
            padding: 0;
            user-select: none;
        }
        .box{
            width: 400px;
            height: 300px;
            margin: 100px auto;
            border: 1px solid;
            border-radius: 15%;
        }


        .box .innerbox{
            width: 240px;
            margin-left:100px ;
            position: relative;
            top: 80px;
        }


        .box .textOne{
            display: inline-block;
            width: 70px;
            height: 26px;
            text-align: right;
        }

        .box input{
            display: inline-block;
            width: 100px;
            height: 20px;
            margin-left: 2px;
            cursor: pointer;
        }


        .box .yanzhengma{
            display: inline-block;
            margin-bottom: 2px;
        }


        .box button{
            text-align: center;
            position: relative;
            margin-top: 10px;
            margin-left: 40px;
            cursor: pointer;
        }
    </style>
</head>
<body>
<div class="box">
    <div class="innerbox">
        <span class="textOne">账号:</span><input type="text"><br>
        <span class="textOne">密码:</span><input type="text"><br>
        <span class="textOne">验证码:</span><input type="text"><div class="yanzhengma"></div><br>
        <button>登录</button>
        <button>注册</button>
    </div>
</div>
<script>
    var yanzhengmaText=document.querySelectorAll('input')[2];
    var $buts=$('button');
    //验证码判断
    //创建验证码
    function getRandomNumber(min,max) {
        return Math.floor(Math.random()*(max-min)+min);
    }
    function yanzhengma(length){
        var yanzhengmaInner='';
        var characterDic =
            'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
        for (i=0;i<length;i++){
            var index=getRandomNumber(0,62);
            var zifu=characterDic[index];
            yanzhengmaInner+=zifu;
        }
        return yanzhengmaInner;
        //必须用return来使生成的验证码返回到调用者
    }
    var yanZ= yanzhengma(4);
    console.log(yanZ);
    var yanzheng=document.querySelector('.yanzhengma');
    yanzheng.innerHTML=yanZ;
    yanzheng.style.cssText='background-color:gray;width:60px;line-height:22px; text-align: center;margin-left:1px;vertical-align: bottom;';
    //验证码判断
    var yanzhengmaSpan=document.querySelector('.yanzhengmatiishi');
    // console.log(yanzhengmaText);
    // console.log(yanzheng);


    /*yanzhengmaText.onblur=function () {
        var yanzhengmaTextneirong=yanzhengmaText.value;
        if (yanzhengmaTextneirong==yanZ){
            yanzhengmaSpan.innerHTML='验证码正确';
            yanzhengmaSpan.style.cssText='color:green;font-size:10px;';
            yanzhengmaText.setAttribute('class','borderGreen');
        }
        else {
            yanzhengmaSpan.innerHTML='验证码不正确,请区分大小写';
            yanzhengmaSpan.style.cssText='color:red;font-size:10px;';
            yanzhengmaText.setAttribute('class','borderRed');
        }
    }*/
    //验证码判断
    //创建验证码

//点击登录验证验证码
    $buts.eq(0).click(function () {
        /*var yanzhengmaTextneirong=yanzhengmaText.value;
        if (yanzhengmaTextneirong==yanZ){
            alert('验证码正确');
/!*            yanzhengmaSpan.innerHTML='验证码正确';
            yanzhengmaSpan.style.cssText='color:green;font-size:10px;';
            yanzhengmaText.setAttribute('class','borderGreen');*!/
        }
        else {
            alert('验证码不正确,请区分大小写');
            /!*yanzhengmaSpan.innerHTML='验证码不正确,请区分大小写';
            yanzhengmaSpan.style.cssText='color:red;font-size:10px;';
            yanzhengmaText.setAttribute('class','borderRed');*!/
        }*/



        // 传递前端数据到php文件
        $.ajax({
            type:'post',
            url:'4.1phplogin.php',
            data:'json',
            data:{
                loginName:$('input').eq(0).val(),
                loginPassword:$('input').eq(1).val()
            },
            success:function (res) {
                // console.log(res);

                if(res.return1==0){
                    alert('登录成功!');
                }else  if(res.return1==1){
                    alert('登陆失败,账号或密码错误');
                }else{
                    alert('网络连接失败');
                }
            }
        })
    })


</script>
</body>
</html>

老师,这个是php文件

<?php
/*$username=$_POST['loginName'];
$paswword=$_POST['loginPassword'];
$success=array('msg'=>'ok');*/root
$con=mysqli_connect('localhost','root','','beixidb');
//var_dump($con);
if($con){
     mysqli_query($con,'set names uft8');
     mysqli_query($con,'set character_set_client uft8');
     mysqli_query($con,'set character_set_results uft8');

     $sql="select * from stu where 1";
     $result=$con->query($sql);
//     echo($result->num_rows);//有行数显示
     if($result->num_rows>0){
     $info=[];
        for($i=0;$row=$result->fetch_assoc();$i++){
           $info[$i]=$row;
          }
        $flag=0;
         for($j=0;$j<count($info);$j++){
          if($info[$j]['name']=='$username'){
             if($info[$j]['password']=='$paswword'){
                 $success['return1']=0;
                 $flag=1;
                 break;
             }

          }


         }

     if($flag==0){
        $success['return1']=1;

        }
        else{
           $success['return1']=1;
         }


     }
   }else{
    $success['return1']=2;
   }

//返回给前台
    echo json_encode($success);

?>


麻烦您了,老师

WEB前端全系列/第五阶段:前后端交互/PHP、数据库编程与设计 157楼
WEB前端全系列/第五阶段:前后端交互/PHP、数据库编程与设计 160楼
WEB前端全系列/第五阶段:前后端交互/PHP、数据库编程与设计 162楼

index.php

<?php
//获取数据库中的数据
try{
    $con=new PDO("mysql:host=localhost;dbname=beixidb;charset=utf8;port=3306",'root','');
    $con->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
    $res=$con->query('select * from news');
    $data=$res->fetchAll(PDO::FETCH_ASSOC);
    // print_r($data);
    //字符串的拼接
    $str='';
    for($i=0;$i<count($data);$i++){
        $str.="<tr><td>{$data[$i]['id']}</td><td>{$data[$i]['title']}</td><td>{$data[$i]['author']}</td><td>{$data[$i]['des']}</td>
        <td>{$data[$i]['content']}</td><td>{$data[$i]['time']}</td><td><a href='#'>删除</a></br><a href='#'>修改</a></td></tr>";
    }
}catch(PDOException $e){
    echo $e->getMessage();
}

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .container{
            width:800px;
            height:600px;
            margin:20px auto;
            border:1px solid #ccc;
            overflow:hidden;
        }
        .left,.right{
            float:left;
        }
        .left{
            width:120px;
        }
        table{
            width:620px;
        }
        .left a{
            display:block;
            text-decoration:none;
            color:#000;
            padding:10px 0;
            border-bottom:1px solid #efefef;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="left">
            <a href="">所有文档列表</a>
            <a href="添加文档.html">添加列表</a>
        </div>
        <div class="right">
        <table border="1" cellpadding="0" cellspacing="0" width="600">
        <tr>
            <th>ID</th>
            <th>标题</th>
            <th>作者</th>
            <th>简介</th>
            <th>时间</th>
            <th>操作</th>
        </tr>
        <?php echo $str?>
    </table>
        </div>
    </div>
</body>
</html>

添加文档.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .container{
            width:800px;
            margin:20px auto;
            border:1px solid #ccc;
            overflow:hidden;
        }
        .left,.right{
            float:left;
        }
        .left{
            width:120px;
        }
        input{
            width: 500px;
        }
        .left a{
            display:block;
            text-decoration:none;
            color:#000;
            padding:10px 0;
            border-bottom:1px solid #efefef;
        }
        textarea{
            vertical-align: top;
            margin-top: 5px;
            width: 500px;
            resize: none;
        }
        .btn{
            display: block;
            width: 80px;
            height: 50px;
            border: none;
            background: orange;
            border-radius: 5px;
            color: #fff;
            margin: 10px auto;
            cursor: pointer;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="left">
            <a href="#">添加文档页面</a>
        </div>
        <div class="right">
            <form action="addnews.php" method="get">
                标题:<input type="text" name="title"><br/>
                作者:<input type="text" name="author"><br/>
                简介:<input type="text" name="des"><br/>
                内容:<textarea type="text" name="content" cols="30" rows="10"></textarea><br/>
                <input type="submit" value="保存" class="btn">
            </form>
        </div>
    </div>
</body>
</html>

addnews.php

<?php
    echo '<pre>';
//接收用户传来的数据
    $title=isset($_GET['.title'])?$_GET['title']:'';
    $author=isset($_GET['.author'])?$_GET['author']:'';
    $des=isset($_GET['.des'])?$_GET['des']:'';
    $content=isset($_GET['.content'])?$_GET['content']:'';
    //生成一个时间戳
    $time=time();
    //写入数据库


    $con=new PDO("mysql:host=localhost;dbname=beixidb;charset=utf8;port=3306",'root','');
    $res=$con->query("insert into news (title,author,des,content,time) values('{$title}','$author','$des','$content','$time')");
    if($res){
        echo '<script> alert("添加成功");window.location.href="index.php"</script>';
    }
    else{
        echo '添加失败';
    }
?>

老师,为什么这个案例添加成功后 标题 作者等信息不显示,只显示时间戳,是没添加进去吗?具体代码如上:

效果图附下:

图片.png

WEB前端全系列/第五阶段:前后端交互/PHP、数据库编程与设计 163楼
WEB前端全系列/第五阶段:前后端交互/PHP、数据库编程与设计 164楼

<?php
/**
  * wechat php test
  */

//define your token
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
//$wechatObj->valid();
$wechatObj->responseMsg();

class wechatCallbackapiTest
{
   public function valid()
    {
        $echoStr = $_GET["echostr"];

        //valid signature , option
        if($this->checkSignature()){
           echo $echoStr;
           exit;
        }
    }

    public function responseMsg()
    {
      //get post data, May be due to the different environments
      $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

       //extract post data
      if (!empty($postStr)){
                /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,
                   the best way is to check the validity of xml by yourself */
                libxml_disable_entity_loader(true);
               $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
                $fromUsername = $postObj->FromUserName;
                $toUsername = $postObj->ToUserName;
                $keyword = trim($postObj->Content);
                $time = time();
                $textTpl = "<xml>
                     <ToUserName><![CDATA[%s]]></ToUserName>
                     <FromUserName><![CDATA[%s]]></FromUserName>
                     <CreateTime>%s</CreateTime>
                     <MsgType><![CDATA[%s]]></MsgType>
                     <Content><![CDATA[%s]]></Content>
                     <FuncFlag>0</FuncFlag>
                     </xml>";             
            if(!empty( $keyword ))
                {
                  $msgType = "text";
                   $contentStr = "我是你爸爸";
                   $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                   echo $resultStr;
                }else{
                   echo "Input something...";
                }

        }else {
           echo "";
           exit;
        }
    }
      
   private function checkSignature()
   {
        // you must define TOKEN by yourself
        if (!defined("TOKEN")) {
            throw new Exception('TOKEN is not defined!');
        }
        
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];
              
      $token = TOKEN;
      $tmpArr = array($token, $timestamp, $nonce);
        // use SORT_STRING rule
      sort($tmpArr, SORT_STRING);
      $tmpStr = implode( $tmpArr );
      $tmpStr = sha1( $tmpStr );
      
      if( $tmpStr == $signature ){
         return true;
      }else{
         return false;
      }
   }
}

?>


老师,我在用阿里云服务器对接我的微信订阅号,服务器是lamp环境,域名是我用来练习的域名charles666.xyz,已备案,可以正常访问。

在微信公众号后台-基本配置-服务器配置时,将服务器和微信公众号对接,注释掉上面代码中的“

$wechatObj->responseMsg()

”,解除下面这个方法调用的注释,

$wechatObj->valid()

提示“提交成功”,已经将服务器配置好了。接下来解除

$wechatObj->responseMsg()

的注释,并注释掉下面这个方法的调用,

$wechatObj->valid()

这时候我在公众号中输入任意文字时,公众号应该弹出“我是你爸爸”的回复消息,但实际上提示的是“该公众号提供的服务出现故障,请稍后再试”。

想问下老师,这个故障怎么解决?我哪一步写错了?

WEB前端全系列/第五阶段:前后端交互/PHP、数据库编程与设计 165楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637