PHP在显示搜索结果时不回显HTML

时间:2015-10-10 16:05:28

标签: php html mysql

提交搜索时,我正在尝试回显页面正文中的某些HTML,但我得到的是没有html回声的页面,我无法看到问题。

function search($lname,$postcode){

    $stmt->prepare("SELECT * FROM clients WHERE lname=:lname AND postcode=:postcode");
    $stmt->execute(array(':lname'=>$lname, ':postcode'=>$postcode));
    $userRow=$stmt->fetch(PDO::FETCH_ASSOC);
} 

if(isset($_POST['search'])){
    $lname = $_POST['lname'];
    $postcode = $_POST['postcode'];
    $result = search($lname, $postcode);
    if ($result['rating'] == 'good') {
        $user->redirect('./results.php?good');
    }
}

由于

2 个答案:

答案 0 :(得分:1)

我很难说你的search函数用于什么,但错误是:

  1. 您没有在提供的代码中调用search函数。 我想它应该被称为

    if(isset($_POST['search'])){
        $lname = $_POST['lname'];
        $postcode = $_POST['postcode'];
        $result = search($lname, $postcode);   // or maybe this->search
    
  2. 接下来你要检查某事物的rating键。我无法理解什么。什么是$_GET['rating'];?当然它是一些字符串变量,如果是 - 它没有键rating。那么我想你要查看rating函数返回的结果的search键? 可以这样做:

    $lname = $_POST['lname'];
    $postcode = $_POST['postcode'];
    $result = search($lname, $postcode);   // or maybe this->search
    if ($result['rating'] == 'good') { /* do something */ }
    
  3. 但并非全部。正如您当前的实现search返回bool一样,rating中的$result键显然没有。所以,我想你应该返回$userRow并检查其中的rating键:

    function search($lname,$postcode){
        $stmt = $this->db->prepare("SELECT * FROM clients WHERE lname=:lname AND postcode=:postcode");
        $stmt->execute(array(':lname'=>$lname, ':postcode'=>$postcode));
        $userRow=$stmt->fetch(PDO::FETCH_ASSOC);
        var_dump($userRow);  // for test purposes
        return $userRow;
    } 
    

答案 1 :(得分:0)

if(isset($_POST['search'])){
   $lname = trim($_POST['lname']);
   $postcode = trim($_POST['postcode']);  

   if($lname==""){
      $error[] = "Provide Last Name."; 
   }else if($postcode==""){
      $error[] = "Provide Postcode.";
   }else{
       try{
           $stmt = $conn->prepare("SELECT * FROM clients WHERE lname=:lname AND postcode=:postcode");
           $stmt->execute(array(":lname"=>$lname, ":postcode"=>$postcode));
           $userRow=$stmt->fetch(PDO::FETCH_ASSOC);

           if($userRow['rating'] == ''){
               $user->redirect('./results.php?notfound');
           }else{
               if($userRow['rating'] == 'good'){
                   $user->redirect('./results.php?good');
               }
               if($userRow['rating'] == 'caution'){
                   $user->redirect('./results.php?caution');
               }
               if($userRow['rating'] == 'badnopay'){
                   $user->redirect('./results.php?badnopay');
               }
               if($userRow['rating'] == 'badabusive'){
                   $user->redirect('./results.php?badabusive');
               }
               if($userRow['rating'] == 'badlate'){
                   $user->redirect('./results.php?badlate');
               }
           }
        }
        catch(PDOException $e){
             echo $e->getMessage();
        }
    } 
}