douju8113 2015-04-19 04:10
浏览 36
已采纳

php验证和mongodb

I'm making a registration form using PHP and mongodb. This form works but the problem is it's not carrying out the validations. Even if I leave all the fields empty, it updates the database with the empty fields. Its as if the whole error = array(); is invisible.

What I need is for it to perform the checks and not update the database until all the requirements are met.

<?php
    session_start();

    if($_POST['submit']){
        $ScName=strip_tags($_POST['ScName']);
        $fname=strip_tags($_POST['fname']);
        $lname=strip_tags($_POST['lname']);
        $email=strip_tags($_POST['email']);
        $password=strip_tags($_POST['password']);
        $password2=strip_tags($_POST['password2']);

        $error = array();

            if(empty($email) or !filter_var($email,FILTER_SANITIZE_EMAIL)){
              $error[] = "Email id is empty or invalid";
            }
            if(empty($password)){
              $error[] = "Please enter password";
            }
            if(empty($password2)){
              $error[] = "Please Confirm password";
            }
            if($password != $password2){
               $error[] = "Password and Confirm password are not matching";
            }
            if(empty($fname)){
                $error[] = "Enter first name";
            }
            if(empty($lname)){
                $error[] = "Enter last name";
            }

            if(count($error == 0)){
                //database configuration
                $host = 'localhost';
                $database_name = 'mongo1';
                $database_user_name = '';  
                $database_password = '';  

                 $connection=new Mongo('localhost');

                 if($connection){

                     //connecting to database
                     $database=$connection->user;

                     //connect to specific collection
                     $collection=$database->user;

                     $query=array('email'=>$email);
                     //check for existing username
                     //$query=array('ScName'=>$ScName);
                     //checking for existing user
                     $count=$collection->findOne($query);

                     if(!count($count)){
                         //Save the New user
                         $user=array('fname'=>$fname,'lname'=>$lname,'ScName'=>$ScName,'email'=>$email,'password'=>md5($password));             
                         $collection->save($user);
                         echo "You are successfully registered.";
                     }else{
                         echo "Email already exists.Please register with another Email";
                     }

                 }else{

                      die("Database is not connected");
                 }

            }else{
                //Displaying the error
                foreach($error as $err){
                    echo $err.'</br>';
                }
            }
            }

    ?>
  • 写回答

1条回答 默认 最新

  • douyan4243 2015-04-19 07:25
    关注

    You have misplaced the bracket at

    if(count($error == 0)){
    

    Since $error == 0 is false, as $error is a populated array, count(false) evaluates as 0 (i.e., false), and the if branch doesn't execute. You should close the bracket after $error:

    if (count($error) == 0) {
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line