毕设(微信商城)遇到的问题及解决方法

2018/3/15
1.Call to undefined function curl_init :
【解决办法】:意思是没有定义curl_init这个方法。找到php.ini,修改extension=php_curl.dll 把前面的分号去掉
2.获取,access_token,返NULL问题,而且写了curl_errno($ch)还不报错。
出现问题的代码段:

 public function getWxAccessToken(){
    
    
         //将access_token存在session中
     if($_SESSION['access_token']&&$_SESSION['expire_time']>time()){
                //如果access_token在session中没有过期
                return $_SESSION['access_token'];
              }
     else{  
                $appid="wxcexxxxxxxxxxxx";
                $appsecret="55xxxxxxxxxxxxxxxx72";
                $url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
                //1.初始化
                $ch = curl_init();
                //2.设置参数
                curl_setopt($ch,CURLOPT_URL,$url);
                curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
                //3.调用接口
                $res = curl_exec($ch);
                //4.关闭
                curl_close($ch);
                //错误判断
                if(curl_errno($ch)){
                    var_dump(curl_errno($ch));
                }
                $arr = json_decode($res,true);
                $access_token=$arr['access_token'];
                $this->http_curl($url,'get','json'); 
                //将获取到的access_token存到session
                $_SESSION['access_token']=$access_token;
                $_SESSION['expire_time']=time()+7000;
                return $access_token;
              }
        }

两个问题
①:不报错的原因:错误判断写在了curl_close下面:
【解决办法】:将curl_close写在方法的最后。
②:access_token为NULL
【解决办法】:跳过SSL证书检查。

$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//需要把这一句加在定义$ch的下一行

2018/3/16
3:

 public function defineItem()
    {
    
    
        header('content-type:text/html;charset=utf-8');
        $access_token = $this->getWxAccessToken();
        $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" . $access_token;
        $postArr = array(
            'button' => array(
                array('name' => urlencode('菜单一'),
                    'type' => 'click',
                    'key' => 'item1'),//第一个一级菜单
            ),
        );

        $postJson = urldecode(json_encode($postArr));//转化文字格式
        $res = $this->http_curl($url, 'post', 'json', $postJson);
        var_dump($res);
    }

这段代码中$res打印出来是int(60)

【解决办法】:不知道为什么打印出来是60。因为$res有http_curl方法得来,检查该方法,因为关于curl的代码有点多,且与getWxAccess_token方法中也有,因此将getWxAccess_token方法中的代码覆盖了http_curl里的那段代码,获取到了正确结果:array(2) { [“errcode”]=> int(0) [“errmsg”]=> string(2) “ok” } 。发现原来的代码中少了curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false),还是因为没有跳过SSL证书的检查而出错。
【心得】:
* 注意,一定要跳过SSL证书,不然没办法采集到curl

2018/4/1
后台管理系统
1.【问题】这里写图片描述

 <div class="col-sm-2">
     <select class="form-control m-b" name="account" id="third" id="tr-third" >
     <option value="-1">请选择三级分类</option>
     </select>
  </div>

js源代码如下

var sortId = $("#third option:selected").val();//第三个下拉框
 if(0==countStandard){
 //countStandard用来记录有多少个规格
            alert("规格尚未填写");return false;
        }else if(sortId==-1){
            alert("分类尚未填写");return false;
        }else if(proName==''){
            alert("商品名称尚未填写");return false;
        }else{
          /*
            。。。。
            添加商品的操作
          */
}

这里原来必须要选到三级分类,假如某商品只有到二级分类,没有三级分类的时候,点击添加商品时,会提示“分类尚未选择”。因为sortId取的是第三个下拉框的值。
为了做到当选到某级分类时,他没有下级分类的情况下,也可以添加商品,而不是像上面这种情况,没有三级分类的时候,没得选又不能添加,修改代码如下

//PHP
 /*
     * 选择分类
     */
    public function chooseSort(){
    
    
        $sortId1 = I('sortId1');
        $sortId2 = I('sortId2');
        $sortId3 = I('sortId3');
        //判断sortId1是否为空或者是否有下级
        $where1['parent_id'] = $sortId1;
        $where2['parent_id'] = $sortId2;
        if($sortId1!='-1')
        $count1 = M('sort')->where($where1)->count();//如果选了一级,判断该一级有没有二级分类
        if($sortId2!='-1')
        $count2 = M('sort')->where($where2)->count();//如果选了二级,判断该二级有没有三级分类
        $data = array(
            'count1'=>$count1,
            'count2'=>$count2
        );
        $this->ajaxReturn(json_encode($data),'json');
    }
//js
/*
 *本来下面想用递归的,但是因为第二个下拉框要判断有没有下级分类,还要看选择了哪个,有变化,所以没想到如何简化。。。
 */
  var sortId1 = $("#tr-first option:selected").val();
        var sortId2 = $("#second option:selected").val();
        var sortId3 = $("#third option:selected").val();
        var sortId = -1;
        $.ajax({
            type: "post",
            url: "chooseSort",
            data:{sortId1: sortId1, sortId2: sortId2, sortId3: sortId3},
            cache:false,
            async:false,//同步,要十分注意这个,必须是同步,不然出了这个function(data),所赋值的sortId还是-1
            dataType:'json',
            success:function(data){
    
    
                var obj = eval("(" + data + ")");
                if (sortId1 != -1) {
                    //选了一级
                    if (obj.count1 == 0) {
                        //没有2级分类
                        //sort = true;
                        sortId = sortId1;
                    } else {
                        if (sortId2 != -1) {
                            //选了2级分类
                            if (obj.count2 == 0) {
                                //没有3级分类
                                // sort = true;
                                sortId = sortId2;
                            } else {
                                //有3级,但没选
                                if (sortId3 == -1) {
                                    sortId = -1;
                                    //  sort = true;
                                }
                                else {
                                    sortId = sortId3;
                                }
                            }
                        } else {
                            //有2级分类,但没选
                            sortId = -1;
                        }
                    }
                }
            }
        });

2.【问题】
解决了上一个问题后,又来了新的问题,某个商品当只有到二级分类的时候,后台控制器查不到结果
这里写图片描述

 $where1['product.id'] = 7;//id为7的商品只有到二级分类
 $proInfo = M('Product')
            ->where($where1)
            ->alias('product')
            ->join('sort s1 On s1.id=product.sort_id')
            ->join('sort s2 On s2.id=s1.parent_id')
            ->join('sort s3 On s3.id=s2.parent_id')
            ->field('
             product.id as pro_id,
             name as pro_name,
             put_focus as focus,
             s1.sort_name as sort_name1,
             s2.sort_name as sort_name2,
             s3.sort_name as sort_name3')
            ->find();
        var_dump($proInfo);

【解析】
肯定没有结果,因为SQL语句是

SELECT product.id as pro_id,name as pro_name,put_focus as focus,s1.sort_name as sort_name1,s2.sort_name as sort_name2,s3.sort_name as sort_name3 
FROM product 
product INNER JOIN sort s1 On s1.id=product.sort_id 
INNER JOIN sort s2 On s2.id=s1.parent_id 
INNER JOIN sort s3 On s3.id=s2.parent_id 
WHERE product.id = 7 LIMIT 1 

又join了一次sort分类表,共join了三次sort表,该商品只有二级分类,join了三次sort表肯定没有结果,所以,得找这个商品的sort_id的父级id的父级id是否为0(因为表设置parent_id为0的是一级分类)。
表结构
这里写图片描述
如果某商品的父级id的父级id是0,证明它是二级分类,那么就join两次sort表就行了。否则就是三级分类。
修改后的MYSQL语句为

from product
product join sort1 s1 on s1.id = product.sort_id
inner join sort1 s2 on s2.id = s1.parent_id 
where product.id = 7
//这个mysql语句还可以这样表示
select product.id,s.child,s.father,s.grandfather 
from product
join (
      select s1.id child ,s2.id father,s2.parent_id grandfather 
      from sort1 s2 
      join sort1 s1 on s2.id = s1.parent_id) s
on product.sort_id = s.child
where product.id  = 7

//还可以这样写
select parent_id from sort1
where id=(SELECT parent_id from sort1,product
          where product.id  = 7
          and product.sort_id=sort1.id)

查询结果均如下
这里写图片描述
所以我们可以做一个判断。

$where1['product.id'] = 6;
         //$sort = M('product')->where($where0)->select('sort_id');
        $info =  M('Product')
            ->where($where1)
            ->join('sort s1 on s1.id = product.sort_id')
            ->join('sort s2 on s2.id = s1.parent_id ')
            ->field('s2.parent_id grandfather')
            ->find();
        //var_dump( $have_father);
        $have_father  = $info['grandfather'];
      if($have_father==0){

            $proInfo = M('Product')
                ->where($where1)
                ->alias('product')
                ->join('sort s1 On s1.id=product.sort_id')
                ->join('sort s2 On s2.id=s1.parent_id')
                ->field('
            product.id as pro_id,
            name as pro_name,
            put_focus as focus,
            s1.sort_name as sort_name1,
            s2.sort_name as sort_name2
                    ')
                ->find();
        }else{

            $proInfo = M('Product')
            ->where($where1)
            ->alias('product')
            ->join('sort s1 On s1.id=product.sort_id')
            ->join('sort s2 On s2.id=s1.parent_id')
            ->join('sort s3 On s3.id=s2.parent_id')
            ->field('
            product.id as pro_id,
            name as pro_name,
            put_focus as focus,
            s1.sort_name as sort_name1,
                    s2.sort_name as sort_name2,
                    s3.sort_name as sort_name3')
            ->find();
        }

3.另外,如果想看Thinkphp执行的Mysql语句,可以在Common下的conf下的config.php里加上这句

 SHOW_PAGE_TRACE'=>true

2018/4/2
1.【问题】
这里写图片描述

//出现问题的代码段
  $return_url = "/Public".explode("./Public", $destination)[1];

【解析】就是这个[1]报错了,因为我的PHP版本是5.3.3的 。可以参考这篇博客PHP 5.3以下版本 无法用下标直接取得函数返回的数组
所以有两种解决办法,一种是升级php版本,另一种是改写代码

$tmp = "/Public".explode("./Public", $destination);
$return_url = $tmp[1];

2.【问题】如何修改mysql密码
我为了升级PHP版本,在服务器上不用wamp(因为我照网上的方法升级不成功。。。),改用PHPstudy,各种折腾之下mysql的密码不知道怎么给改了。所以我只能修改回原来的密码。。。
修改方法参考如下MySQL修改root密码的多种方法(推荐)

试了第一种方法,第三种方法貌似没有用,因为查看了mysql数据库的user表,里面并没有password字段,会报错
第三种方法
这里写图片描述
第一种方法
这里写图片描述

猜你喜欢

转载自blog.csdn.net/sinat_35803474/article/details/79587132
今日推荐