【问题标题】:php data from database is not shown after search搜索后不显示数据库中的php数据
【发布时间】:2015-10-02 05:38:55
【问题描述】:

我是 php 新手,想在名称和日期上创建搜索功能,但我遇到了问题。

我的问题是搜索功能从搜索中看到所有受影响的行,但没有显示它们。我将数据库中的所有数据作为默认值加载到我的页面上,我希望如果来自搜索集的$_GET['go'] 不再显示所有默认数据,并且只显示搜索结果。现在他总是显示默认数据。除了$_GET['by'],没有显示任何数据。

我也尝试使用 PDO,但我对此了解不多....所以我也有可能做错了什么。

我的 PHP 代码:

<?php


            ini_set('display_errors',1);
            ini_set('display_startup_errors',1);
            error_reporting(-1);

            //connect  to the database
            $db_host = "localhost";
            $db_username = "**";
            $db_password = "**";
            $db_name = "**";

            $db = new PDO('mysql:host=' .$db_host . ';dbname='. $db_name . '',$db_username,$db_password);


            if(isset($_POST['submit'])){
                if(isset($_GET['go'])){
                    if(preg_match("/^[a-zA-Z]+/", $_POST['search'])){
                        $zoek=$_POST['search'];

                        var_dump($zoek); //this will display on the screen the content of the variable 

                        // query  the database table
                        $sql="SELECT v.vacatureID, v.werkgeverID, v.functie, v.omschrijvingKort, v.datum, w.werkgeverID, w.naamBedrijf, w.plaats, w.image FROM vacature AS v JOIN werkgever AS w ON v.werkgeverID = w.werkgeverID WHERE w.naamBedrijf LIKE  '%" . $zoek .  "%' ORDER BY datum DESC";
                        // run  the query against the mysql query function
                        $result = $db->prepare($sql); 
                        $result->execute(); 

                        // count number of rows that are effected
                        $rows = $result->fetchAll();
                        $numrows = count($rows);
                        echo  "<p>" .$numrows . " results found for '" . $zoek . "'</p>"; 

                        // create  while loop and loop through result set
                        while($row = $result->fetch()){
                                $Functie=$row['functie'];
                                $OmschrijvingKort=$row['omschrijvingKort'];
                                $Datum=$row['datum'];
                                $Bedrijf=$row['naamBedrijf'];
                                $Plaats=$row['plaats'];
                                $Image=$row['image'];
                                $ID=$row['vacatureID'];

                            // truncate
                            if (strlen($Functie) > 20) {
                                // truncate string
                                $stringCut = substr($Functie, 0, 20);
                                // make sure it ends in a word
                                $Functie = substr($stringCut, 0, strrpos($stringCut, ' ')).'...'; 
                            }
                            if (strlen($Bedrijf) > 25) {
                                // truncate string
                                $stringCut2 = substr($Bedrijf, 0, 14);
                                // make sure it ends in a word
                                $Bedrijf = substr($stringCut2, 0, strrpos($stringCut2, ' ')).'...'; 
                            }
                            if (strlen($OmschrijvingKort) > 63) {
                                // truncate string
                                $stringCut2 = substr($OmschrijvingKort, 0, 63);
                                // make sure it ends in a word
                                $OmschrijvingKort = substr($stringCut2, 0, strrpos($stringCut2, ' ')).'...'; 
                            }
                            // display the result of the array
                            echo 
                            "
                                <div class='vacatureinfo2'>
                                    <img class='userimg2' src='../../uploads/userimage/". $Image ."' id='imge'/>
                                    <p class='func-br-pl pull-left'>". $Functie ." - ". $Bedrijf ." - ". $Plaats ."</p><p class='dtm pull-right'>". $Datum ."</p>
                                    <div class='omschrijvingkort2'>
                                        ". $OmschrijvingKort ."
                                    </div>
                                    <a href='#' class='pull-right'>Meer informatie</a>
                                </div>
                                <hr>
                            ";
                        }
                    }
                    elseif(preg_match("/^[0-9]{4}-[0-9]{2}-[0-9]{2}/", $_POST['searchdate'])){
                        $zoek=$_POST['searchdate'];

                        var_dump($zoek); //this will display on the screen the content of the variable 

                        //-query  the database table
                        $sql="SELECT v.vacatureID, v.werkgeverID, v.functie, v.omschrijvingKort, v.datum, w.werkgeverID, w.naamBedrijf, w.plaats, w.image FROM vacature AS v JOIN werkgever AS w ON v.werkgeverID = w.werkgeverID WHERE v.datum LIKE '%" . $zoek ."%' ORDER BY datum DESC";
                        //-run  the query against the mysql query function
                        $result = $db->prepare($sql); 
                        $result->execute(); 
                        $rows = $result->fetchAll();
                        $numrows = count($rows);
                        echo  "<p>" .$numrows . " results found for '" . $zoek . "'</p>"; 

                        //-create  while loop and loop through result set
                        while($row = $result->fetch()){
                                $Functie=$row['functie'];
                                $OmschrijvingKort=$row['omschrijvingKort'];
                                $Datum=$row['datum'];
                                $Bedrijf=$row['naamBedrijf'];
                                $Plaats=$row['plaats'];
                                $Image=$row['image'];
                                $ID=$row['vacatureID'];

                            //-truncate
                            if (strlen($Functie) > 20) {
                                // truncate string
                                $stringCut = substr($Functie, 0, 20);
                                // make sure it ends in a word
                                $Functie = substr($stringCut, 0, strrpos($stringCut, ' ')).'...'; 
                            }
                            if (strlen($Bedrijf) > 25) {
                                // truncate string
                                $stringCut2 = substr($Bedrijf, 0, 14);
                                // make sure it ends in a word
                                $Bedrijf = substr($stringCut2, 0, strrpos($stringCut2, ' ')).'...'; 
                            }
                            if (strlen($OmschrijvingKort) > 63) {
                                // truncate string
                                $stringCut2 = substr($OmschrijvingKort, 0, 63);
                                // make sure it ends in a word
                                $OmschrijvingKort = substr($stringCut2, 0, strrpos($stringCut2, ' ')).'...'; 
                            }
                            //-display the result of the array
                            echo 
                            "
                                <div class='vacatureinfo2'>
                                    <img class='userimg2' src='../../uploads/userimage/". $Image ."' id='imge'/>
                                    <p class='func-br-pl pull-left'>". $Functie ." - ". $Bedrijf ." - ". $Plaats ."</p><p class='dtm pull-right'>". $Datum ."</p>
                                    <div class='omschrijvingkort2'>
                                        ". $OmschrijvingKort ."
                                    </div>
                                    <a href='#' class='pull-right'>Meer informatie</a>
                                </div>
                                <hr>
                            ";
                        }
                    }
                    else{
                        echo  "<p>Please enter a search query</p>";
                    }
                }
            }//end of search form script                
            if(isset($_GET['by'])){
                $letter=$_GET['by'];

                $sql="SELECT v.vacatureID, v.werkgeverID, v.functie, v.omschrijvingKort, v.datum, w.werkgeverID, w.naamBedrijf, w.plaats, w.image FROM vacature AS v JOIN werkgever AS w ON v.werkgeverID = w.werkgeverID WHERE SUBSTRING(w.naamBedrijf,1,1) LIKE  '%" . $letter .  "%' ORDER BY datum DESC";

                $result = $db->prepare($sql); 
                $result->execute(); 

                $rows = $result->fetchAll();
                $numrows = count($rows);
                echo  "<p>" .$numrows . " results found for '" . $letter . "'</p>"; 

                //-create  while loop and loop through result set
                while($row = $result->fetch()){
                    $Functie=$row['functie'];
                    $OmschrijvingKort=$row['omschrijvingKort'];
                    $Datum=$row['datum'];
                    $Bedrijf=$row['naamBedrijf'];
                    $Plaats=$row['plaats'];
                    $Image=$row['image'];
                    $ID=$row['vacatureID'];

                    //-truncate
                    if (strlen($Functie) > 20) {
                        // truncate string
                        $stringCut = substr($Functie, 0, 20);
                        // make sure it ends in a word
                        $Functie = substr($stringCut, 0, strrpos($stringCut, ' ')).'...'; 
                    }
                    if (strlen($Bedrijf) > 25) {
                        // truncate string
                        $stringCut2 = substr($Bedrijf, 0, 14);
                        // make sure it ends in a word
                        $Bedrijf = substr($stringCut2, 0, strrpos($stringCut2, ' ')).'...'; 
                    }
                    if (strlen($OmschrijvingKort) > 63) {
                        // truncate string
                        $stringCut2 = substr($OmschrijvingKort, 0, 63);
                        // make sure it ends in a word
                        $OmschrijvingKort = substr($stringCut2, 0, strrpos($stringCut2, ' ')).'...'; 
                    }

                    //-display  the result of the array
                    echo 
                    "
                        <div class='vacatureinfo2'>
                            <img class='userimg2' src='../../uploads/userimage/". $Image ."' id='imge'/>
                            <p class='func-br-pl pull-left'>". $Functie ." - ". $Bedrijf ." - ". $Plaats ."</p><p class='dtm pull-right'>". $Datum ."</p>
                            <div class='omschrijvingkort2'>
                                ". $OmschrijvingKort ."
                            </div>
                            <a href='#' class='pull-right'>Meer informatie</a>
                        </div>
                        <hr>
                    ";
                }
            }//end of our letter search script
            else{
                include('php/vacatureoverzichtphp.php');
            }
        ?>

include('php/vacatureoverzichtphp.php'); 我有页面上显示的默认数据的代码。

HTML 代码:

<form class="form-inline pull-right" method="post" action="vacatureoverzicht.php?go"  id="searchform">
            <div class="form-group">
                <label>Zoeken op: </label>
            </div>
            <div class="form-group">
                <label class="sr-only" for="zoekenBedrijf">Bedrijfsnaam of Datum</label>
                <input type="text" class="form-control" name="search" id="zoekenBedrijf" placeholder="Bedrijfsnaam">
                <label> of </label>
                <input type="text" class="form-control" name="searchdate" id="zoekenDatum" placeholder="Datum">
            </div>
            <button type="submit" name="submit" value="Search" class="btn btn-default">zoeken</button>
        </form>
        <br><br><br>
        <p><a  href="vacatureoverzicht.php">Alles</a> | <a  href="?by=A">A</a> | <a  href="?by=B">B</a> | <a  href="?by=C">C</a> | <a  href="?by=D">D</a> | <a  href="?by=E">E</a> | <a  href="?by=F">F</a>
        | <a  href="?by=G">G</a> | <a  href="?by=H">H</a> | <a  href="?by=I">I</a> | <a  href="?by=J">J</a> | <a  href="?by=K">K</a> | <a  href="?by=L">L</a> 
        | <a  href="?by=M">M</a> | <a  href="?by=N">N</a> | <a  href="?by=O">O</a> | <a  href="?by=P">P</a> | <a  href="?by=Q">Q</a> | <a  href="?by=R">R</a>
        | <a  href="?by=S">S</a> | <a  href="?by=T">T</a> | <a  href="?by=U">U</a> | <a  href="?by=V">V</a> | <a  href="?by=W">W</a> | <a  href="?by=X">X</a> 
        | <a  href="?by=Y">Y</a> | <a  href="?by=Z">Z</a></p>

【问题讨论】:

  • 你看到你的第一个var_dump($zoek); 输出了吗?
  • @Alex 是的,它说string(2) "wo",根据我的计数,我还得到了正确的行数6 results found for 'wo',如果我在我的数据库中尝试查询,如果返回所有正确的结果。
  • 你容易受到sql injection attacks的攻击
  • @MarcB 你能告诉我我必须做些什么来使我的代码对 sql 注入攻击安全吗?因为如前所述,我是 php 和 PDO 的新手,这是我认为我的代码安全的唯一方法!

标签: php mysql pdo search-engine


【解决方案1】:

如果我的代码正确,问题就在这里:

$rows = $result->fetchAll();
$numrows = count($rows);
echo  "<p>" .$numrows . " results found for '" . $zoek . "'</p>"; 

// create  while loop and loop through result set
while($row = $result->fetch()){

所以你先做了fetchAll(),然后你尝试while($row = $result-&gt;fetch()){。但您无法再次从相同的结果中获取。

所以你应该把你的循环头改成:

 foreach($rows as $row){

所以完整的片段会是这样的:

$rows = $result->fetchAll();
$numrows = count($rows);
echo  "<p>" .$numrows . " results found for '" . $zoek . "'</p>"; 

// create  while loop and loop through result set
foreach ($rows as $row ){

希望能有所帮助:-)

【讨论】:

  • 或者,$numrows = $result-&gt;num_rows; while($row = $result-&gt;fetch())
  • @Alex 感谢您的帮助!但在搜索结果之后仍会显示包含的默认数据。你知道我也可以解决这个问题吗?因为如果我运行 $_GET['by'] 代码,则不会显示包含...
  • 没关系...我的代码现在工作正常!! :-D 我只需要将 else{include('php/vacatureoverzichtphp.php');} 更改为 if(!isset($_GET['go']) &amp;&amp; !isset($_GET['by'])){ include('php/vacatureoverzichtphp.php'); }
猜你喜欢
  • 2014-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-10
相关资源
最近更新 更多