-3

我试图创建一个 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>

有人可以解释一下我如何显示与搜索结果相同的数据吗

4

1 回答 1

0

没有机会对此进行正确测试,但它应该或多或少地工作。请看一下代码中的注释,您有一个错误(“选择”标签的名称),如果未修复,运行此代码后将显示错误!

在连接到数据库后放置它。您所做的基本上是检查是否单击了提交按钮(然后设置了与提交按钮名称相对应的 $_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 ']);
    }

}
于 2013-04-09T20:12:27.227 回答