复选框不工作(尝试制作搜索过滤器)PHP

时间:2014-09-21 14:52:04

标签: php checkbox

我试图使用复选框创建搜索过滤器。当我显示表中的所有数据时,它显示正常但当我尝试使用复选框时没有任何反应。这是我的代码。

<?php
include "connect.php";
$sql="SELECT * FROM websiteusers ";
if (isset($_POST['submit'])) {

    $searchgender=$_POST['gender'];
    $searchethnicity=$_POST['ethnicity'];

    $sql .= "WHERE gender = '{$searchgender}' ";
    $sql .= " AND ethnicity = '{$searchethnicity}'";

}

$query=mysql_query($sql) or die(mysql_error());

?>

<form name="form" action="search.php" method="post">
<table>
    <tr>
        <td>
            Gender:
        </td>
        <td>
            <input type="checkbox" name="gender" value="male">Male<br>
            <input type="checkbox" name="gender" value="female">Female
        </td>
    </tr>
    <tr>
        <td>
            Ethnicity:
        </td>
        <td>
            <input type="checkbox" name="ethnicity" value="black">Black<br>
            <input type="checkbox" name="ethnicity" value="hispanic">Hispanic 

        </td>
    </tr>
</table>
<p><input type="submit" value="search profiles" name="search"></p>
</form>

3 个答案:

答案 0 :(得分:1)

在上面放置(重命名)if (isset($_POST['search'])) {(参见下面的注释) $sql="SELECT * FROM websiteusers ";

您也没有名为submit的表单元素,但您有一个名为search的提交按钮

<input type="submit" value="search profiles" name="search">

因此,将if (isset($_POST['submit']))更改为if (isset($_POST['search'])),这是最不可能为您效劳的原因。

您的代码执行基于该条件语句。

启用error reporting会发出Undefined index submit...

信号
error_reporting(E_ALL);
ini_set('display_errors', 1);

您还需要使用while循环遍历结果。

类似

while($results = mysql_fetch_array($query)){

    echo $results['gender'] . "<br>" $results['ethnicity'] . "<br>";

}

修改

以下是我测试和使用的mysqli_*方法,而不是mysql_*。还包括一个三元运算符和mysqli_real_escape_string()用于搜索变量。

Nota: 如果未选中其中一个复选框,则三元运算符将避免收到Undefined index...警告。

search.php更改为action="",因为所有内容都在同一页内。

旁注: 您可能希望为此行使用OR代替AND,但已使用AND - OR已在下面注释掉:

$sql .= " AND ethnicity = '{$searchethnicity}'";

<强>代码:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

$DB_HOST = "localhost"; // replace but do not use mysql_ method.
$DB_NAME = "xxx"; // replace
$DB_USER = "xxx"; // replace
$DB_PASS = "xxx"; // replace

$conn = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if($conn->connect_errno > 0) {
  die('Connection failed [' . $conn->connect_error . ']');
}

$sql="SELECT * FROM websiteusers ";
if (isset($_POST['submit'])) {


$searchgender = isset($_POST['gender']) ? mysqli_real_escape_string($conn,$_POST['gender']) : '';

$searchethnicity = isset($_POST['ethnicity']) ? mysqli_real_escape_string($conn,$_POST['ethnicity']) : '';


    $sql .= "WHERE gender = '{$searchgender}' ";

//      $sql .= " OR ethnicity = '{$searchethnicity}'"; // either use this

$sql .= " AND ethnicity = '{$searchethnicity}'"; // or this, not both

}

$query=mysqli_query($conn,$sql) or die(mysqli_error($conn));

?>

<form name="form" action="" method="post">
<table>
    <tr>
        <td>
            Gender:
        </td>
        <td>
            <input type="checkbox" name="gender" value="male">Male<br>
            <input type="checkbox" name="gender" value="female">Female
        </td>
    </tr>
    <tr>
        <td>
            Ethnicity:
        </td>
        <td>
            <input type="checkbox" name="ethnicity" value="black">Black<br>
            <input type="checkbox" name="ethnicity" value="hispanic">Hispanic 

        </td>
    </tr>
</table>
<p><input type="submit" value="search profiles" name="submit"></p>
</form>

<table width="70%" cellspacing="5" cellpadding="5">
    <tr>
        <td><strong>name</strong></td>
        <td><strong>gender</strong></td>
        <td><strong>ethnicity</strong></td>
    </tr>
<?php while ($row=mysqli_fetch_array($query)) { ?>
    <tr>
        <td><?php echo $row['name']; ?></td>
        <td><?php echo $row['gender']; ?></td>
        <td><?php echo $row['ethnicity']; ?></td>
    </tr>
<?php } ?>


</table>

<强> 脚注:

  • 为了让AND正常工作,需要选择其中一个复选框。

  • 如果勾选了所有四个复选框,则它将无法与AND一起使用。您需要使用OR才能使其正常工作或重新构建查询数据库的方式。

您甚至可能希望再次使用ORAND来尝试LIKE条款,具体取决于您希望查询的标准。

$sql .= "WHERE gender LIKE '{$searchgender}' ";
$sql .= " OR ethnicity = '{$searchethnicity}'";

要包含的组合太多。您将需要尝试它们并混合它们。

答案 1 :(得分:0)

<?php
include "connect.php";
$sql="SELECT * FROM websiteusers WHERE 0 OR ";
if (isset($_POST['submit'])) {

    if (isset($_POST['gender'])){
        $searchgender_raw= $_POST['gender'];
        foreach ($sg in $searchgender_raw)
            $searchgender.= " '" . $sg . "',";
    }
    $searchgender = rtrim($searchgender, ",");
    if (isset($_POST['ethnicity'])){
        $searchethnicity_raw= $_POST['ethnicity'];
        foreach ($se in $searchethnicity_raw)
            $searchethnicity.= " '" . $se . "',";
    }
    $searchethnicity = rtrim($searchethnicity, ",");


    $sql .= " WHERE gender IN ({$searchgender}) ";
    $sql .= " AND ethnicity IN ({$searchethnicity})";

}
$query=mysql_query($sql) or die(mysql_error());

?>

现在让我知道你是否理解代码

答案 2 :(得分:0)

编辑: 新代码: 你好,这是我的新代码和男女过滤器的工作,但种族给了我“未定义的索引”。您还会注意到我有两条查询行。这是因为第一位代码中的一个显然无法通过底部的代码到达。好的,我的新代码。

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

$DB_HOST = "localhost"; 
$DB_NAME = "mysite"; 
$DB_USER = "root"; 
$DB_PASS = "***"; // password edited out

$conn = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if($conn->connect_errno > 0) {
  die('Connection failed [' . $conn->connect_error . ']');
}




$sql="SELECT * FROM websiteusers ";
if (isset($_POST['submit'])) {
    $searchgender = isset($_POST['gender'])  ? mysqli_real_escape_string($conn,$_POST['gender']) : '';

    $searchethnicity = isset($_POST['ethnicity'])  ? mysqli_real_escape_string($conn,$_POST['gender']) : '';

    $sql .= "WHERE gender = '{$searchgender}'";
    $sql .= " OR ethnicity = '{$searchethnicity}' ";
}

$query=mysqli_query($conn,$sql) or die(mysqli_error($conn));


?>

<?php
$query=mysqli_query($conn,$sql) or die(mysqli_error($conn));
?>

<form name="form" action="" method="post">
<table>
    <tr>
        <td>
            Gender:
        </td>
        <td>
            <input type="checkbox" name="gender" value="male">Male<br>
            <input type="checkbox" name="gender" value="female">Female
        </td>
    </tr>
    <tr>
        <td>
            Ethnicity:
        </td>
        <td>
            <input type="checkbox" name="ethnicity" value="black">Black<br>
            <input type="checkbox" name="ethnicity" value="hispanic">Hispanic 

        </td>
    </tr>
</table>
<p><input type="submit" value="search profiles" name="submit"></p>
</form>


<table width="70%" cellspacing="5" cellpadding="5">
    <tr>
        <td><strong>name</strong></td>
        <td><strong>gender</strong></td>
        <td><strong>ethnicity</strong></td>
    </tr>
<?php while ($row=mysqli_fetch_array($query)) { ?>
    <tr>
        <td><?php echo $row['name']; ?></td>
        <td><?php echo $row['gender']; ?></td>
        <td><?php echo $row['ethnicity']; ?></td>
    </tr>
<?php } ?>

</table>