显示搜索值

时间:2013-04-09 19:47:44

标签: php search

我尝试创建一个php页面,您可以在其中搜索多个给定点上的数据库 我写了搜索部分,但我无法弄清楚如何显示找到的项目 到目前为止,我的代码是

<html>
<body>
<link rel="stylesheet" href="css/reset.css" type="text/css" media="all">
<link rel="stylesheet" href="css/grid.css" type="text/css" media="all">
<link rel="stylesheet" href="css/home.css" type="text/css" media="all">
<?php
 $connectie = mysql_connect('localhost','root','usbw');
 if($connectie == false){echo "error";}

 if(mysql_select_db('makelaars',$connectie) == false){ echo "error2";}
 ?>
 <table>
<tr>
    <td width= 100>
        Soort Object: 
    </td>
    <td>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method='post'>
    <select name="soort">
        <?php
            $query_soort = "SELECT DISTINCT `Soort_object` FROM `objecten`";
            $resultaat = mysql_query($query_soort, $connectie);
            $inhoud_soort = $_POST['blog'];
        while ($soort = mysql_fetch_assoc($resultaat)) {
            echo "<option>" . htmlentities($soort['Soort_object']) . "</option>";
        }   
        ?>
    </select>
    </td>
    </tr>
    <tr>
    <td>
    <p>Status:</p>
    </td>
    <td>
    <select name="soort">
        <?php
            $query_status = "SELECT DISTINCT `status` FROM `objecten`";
            $resultaatstatus = mysql_query($query_status, $connectie);
            $inhoud_status = $_POST['blog'];
        while ($status = mysql_fetch_assoc($resultaatstatus)) {
            echo "<option>" . htmlentities($status['status']) . "</option>";
        }   
        ?>
    </select>
    </td>

</tr>
<tr>
    <td>
    <p>Provincie:</p>
    </td>
    <td>
    <select name="provincie">
        <?php
            $query_provincie = "SELECT DISTINCT `Provincie` FROM `objecten`";
            $resultaatprovincie = mysql_query($query_provincie, $connectie);
            $inhoud_provincie = $_POST['blog'];
        while ($provincie = mysql_fetch_assoc($resultaatprovincie)) {
            echo "<option>" . htmlentities($provincie['Provincie']) . "</option>";
        }   
        ?>
    </select>
    </td>
    <td>
        <input type="submit" value="verstuur" name="soortknop">
    </td>

</tr>
<tr>

    <td>

    </td>
    </form>

有人可以解释我如何显示具有与搜索相同的值的数据

1 个答案:

答案 0 :(得分:0)

没有机会正确测试,但它应该或多或少有效。请看一下代码中的注释,你有一个错误('select'标签的名称)如果没有修复就会在运行后显示错误!

连接到数据库后将其放入。你正在做的基本上是检查是否单击了提交按钮(然后设置与提交按钮名称对应的$ _POST属性)。如果未提交,则代码将不会执行,结果将不会显示。

我还建议你不要使用mysql_函数,而是查看像PDO这样的库。它的参数化查询使您的脚本更安全,并防止SQL注入。

<?php

// Submit button was clicked
if(isset($_POST['soortknop'])) {

    // Sanitize data
    $post_soort = mysql_real_escape_string($_POST['soort']);
    $post_status = mysql_real_escape_string($_POST['status']); // changed this, you've two select fields 'soort'?
    $post_provincie = mysql_real_escape_string($_POST['provincie']);

    // Fetch results using all $post_ variables
    $result_query = mysql_query(
        "SELECT *
        FROM 'objecten'
        WHERE Soort_object = '" . $post_soort . "'
            AND status = '" . $post_status . "'
            AND Provincie = '" . $post_provincie . "'");

    // Display results
    while ($row = mysql_fetch_assoc($result_query)) {
        echo htmlentities($row['Soort_object']) . " " . htmlentities($row['status']) . " " . htmlentities($row['post_provincie ']);
    }

}