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

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、数据库编程与设计 153楼
WEB前端全系列/第五阶段:前后端交互/PHP、数据库编程与设计 154楼

<?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、数据库编程与设计 155楼

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="jquery-3.5.1.js"></script>
</head>
<body>
    <span>用户名:</span><input type="text" class="userName">
    <br>
    <span>密码:</span><input type="text" class="userPsw">
    <br>
    <button>登录</button>
    <script>
       /* $('button').click(function () {
            $.ajax({
                type:'post',
                url:'10jqAjax请求.php',
                dataType:'json',
                data:{
                    myName:$('.userName').val(),
                    myPsw:$('.userPsw').val()
                },
                success:function (res) {
                    console.log(res.infocode);
            };
        })*/
            $('button').click(function () {
                $.ajax({
                    type:'post',
                    url:'10jqAjax请求.php',
                    dataType:'json',
                    data:{
                        myName:$('.userName').val(),
                        myPsw:$('.userPsw').val()
                    },
                    success:function (res) {
                        console.log(res);
                        console.log(res.infocode);
                        if (res.infocode == 0){
                            alert('登录失败');
                        }else{
                            alert('登录成功');
                        }
                    }
                })
            })
    </script>
</body>
</html>
============================================
<?php
    $username = $_POST['myName'];
    $userpsw = $_POST['myPsw'];
    $success = array('msg'=>'ok');
    if($username=='zhangsan' && $userpsw=='123456'){
        //登陆成功1:登录成功  0:登陆失败
        $success['infocode']=1;
    }else{
        //登陆失败
        $success['infocode']=0;
    }
    echo json_encode($success);
?>

老师,请问一下我的代码在前台为什么访问不到后台的infocode,导致点击登录页面的时候不管输入的用户名和密码正不正确都是显示登陆成功,麻烦帮我看一下是哪里的问题?image.png



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

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Ajax案例</title>
    <script src="js/jquery-3.5.1.js"></script>
</head>
<body>
 <span>用户名:</span><input type="text" class="username">
 <span>密码:</span><input type="text" class="password">
 <button class="button">点击</button>
 <script>
     $('button').click(function () {
        $.ajax({
          //请求方式
           type:'post',
          //交互数据格式为JSON
          dataType:'json',
          //指明请求发送到PHP后台的地址
          url:'777.php',
          data:{
               myName:$('.username').val(),
               myPass:$('.password').val()
          },
          success:function (res) {
              if(res.infor==0){
                  alert('登录失败')
              }else {
                  alert('登录成功')
              }
          }
      });

  });
 </script>
</body>
</html>
<?php
  $username=$_post['myName'];
  $password=$_post['myPass'];
  $success=array('msg'=>'ok'),
  if($username=='beixi'&& $password=='123456'){
   $success['infor']=1;
  }else{
   $success['infor']=0;
  }
  echo json_encode($success);
?>

老师麻烦看一下前后端没法交互  谢谢老师

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

课程分类

百战程序员微信公众号

百战程序员微信小程序

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