【问题标题】:Don't Apply This Condition PHP(Mysql)不要应用此条件 PHP(Mysql)
【发布时间】:2021-08-13 11:06:49
【问题描述】:

致命错误:未捕获的 PDOException:SQLSTATE[42S22]:未找到列:1054 'where 子句'中的未知列 'Ja_ay'

此条件不适用:-

               if($count == 1 ){

                $theMsg = '<div class="alert alert-danger">Sory The This User Is Exist</div>';
                redirectHome($theMsg,'back');
            }

这是我的代码

此变量用于错误

            $formErrors = array();

            if(strlen($user) < 4 ){
                $formErrors[] = 'Username Cant Be Less Than <strong>4 Characters</strong>';
                $theMsg = '';
                redirectHome($theMsg,'back');
            }
            if(strlen($user) > 20 ){
                $formErrors[] = 'Username Cant Be More Than <strong>4 Characters</strong>';
            }
            if(empty($user)){
                $formErrors[] = 'Username Cant Be <strong>Empty</strong>';
            }
            if(empty($name)){
                $formErrors[] = 'Name Cant Be <strong>Empty</strong>';
            }
            if(empty($email)){
                $formErrors[] = 'Email Cant Be <strong>Empty</strong>';
            }
            foreach($formErrors as $error){
                echo '<div class="alert alert-danger">' . $error .'</div>' ;
            }

在这一步我检查是否有任何错误

            if (empty($formErrors)){
                $stmt = $con->prepare("SELECT Username From users WHERE $user = ?");
                $stmt-> execute(array($user));
                $count = $stmt->rowCount();

在此步骤中不应用条件

                if($count == 1 ){
                    $theMsg = '<div class="alert alert-danger">Sory The This User Is Exist</div>';
                    redirectHome($theMsg,'back');
                }else{
                    $stmt = $con->prepare("INSERT INTO 
                    users(Username, Password, Email, Fullname, Date)VALUES(:user, :pass, :mail, :name,now())");
                    $stmt->execute(array(
                            'user'=>$user,
                            'pass'=>$shapass,
                            'mail'=>$email,
                            'name'=>$name
                        ));
                    $theMsg = "<div class='alert alert-success'>" . $stmt->rowCount() . 'Record Update' .'</div>';
                    redirectHome($theMsg,'back');
                }
                    }
                } else{
                    $theMsg = '<div class="alert alert-danger"> Sorry You Cant Browse This Page Directly</div' ;
                    redirectHome($theMsg,'back');
                }

【问题讨论】:

  • 查询中的$user 应该是username 吗?目前,您插入 $user 的方式被 SQL 引擎视为列名。当然,没有像用户名这样的列(至少对于大多数用户名)。我猜算错字。
  • 该错误与显示的代码无关,该错误很清楚,您在表上查询不存在的字段。 1054 'where 子句'中的未知列'Ja_ay'

标签: php mysql


【解决方案1】:

您的代码不正确:

if (empty($formErrors)){
                // replace $user variable with column name in your table
                // here I have just replaced it with 'username' but it depends 
                $stmt = $con->prepare("SELECT Username From users WHERE username = ?");
                $stmt-> execute(array($users));
                $count = $stmt->rowCount();
}

【讨论】:

  • @Grumpy,这与错误有什么关系?我提到问题出在这个 sn-p 代码中。他在查询中放了一个变量而不是列名。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-27
  • 2012-10-30
相关资源
最近更新 更多