如何在数据库中创建多个复选框搜索值并将其显示在同一页面上

时间:2016-04-18 07:52:12

标签: php html mysql sql html5

我无法显示我的搜索表单,我怀疑问题是由于我的工作无效。我点击搜索,它包含任何错误,但空白表。也许有一些我忽略的php和sql语法错误。 fyi表单显示在同一页面(复选框)index.php

index.php
<?php require_once('Connections/conn.php'); ?>
<!DOCTYPE html>
<html>
<head>
    <title>Casado</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>

<nav class="navbar navbar-inverse">
  <div class="container-fluid">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>                        
      </button>
      <a class="navbar-brand" href="/kahwin/index.php">Casado</a>
    </div>
    <div class="collapse navbar-collapse" id="myNavbar">
      <ul class="nav navbar-nav navbar-right">
        <li><a href="#"><span class="glyphicon glyphicon-log-in"></span> Login</a></li>
      </ul>
    </div>
  </div>
</nav>

<div class="container text-center">  
<div class="page-header">
<div class="container">

  <form method="post">
    <div class="form-group">
      <h3><label for="usr">Carian bajet anda:</label></h3>
<div class= "col-md-12">
<div class=" col-md-4"></div> 

      <div class=" col-md-4">
      <input name="bajet" type="text" class="form-control" id="usr"></div>

    </div>
    <div class=" col-md-4"></div>
    </div><br>

    <h3><label for="sel1">Pilih negeri pilihan anda:</label></h3>

    <div class= "col-md-12">
<div class=" col-md-4"></div> 

      <div class=" col-md-4">
          <select class="form-control" name="sel">
            <option>Kuala Lumpur</option>
            <option>Negeri Sembilan</option>
            <option>Pahang</option>
            <option>Perak</option>
            <option>Terengganu</option>
            <option>Selangor</option>

          </select>
       </div>

       <div class=" col-md-4"></div>
    </div><br>


      <br>
      <h5><label for="check">Senarai Pra-perkahwinan:</label></h5>
   <center> <div class="checkbox">
      <label class="checkbox-inline">  <input type="checkbox"name="check_list[]"  value="Jurufoto"><label>Jurufoto</label></label>
      <label class="checkbox-inline">  <input type="checkbox"name="check_list[]"  value="Butik"><label>Butik</label></label>
      <label class="checkbox-inline">  <input type="checkbox"name="check_list[]"  value="Hiburan"><label>Hiburan</label></label>
      <label class="checkbox-inline">  <input type="checkbox"name="check_list[]"  value="Kad Kahwin"><label>Kad Kahwin</label></label>
      <label class="checkbox-inline">  <input type="checkbox"name="check_list[]"  value="Katering"><label>Katering</label></label>
       <br>


    </center>
            <div class="col-md-4"></div>

            <div class="col-md-4">
                <button class="btn btn-success btn-sm" name="search">Search&nbsp;<span class="glyphicon glyphicon-search"></span></button><br><br>
            </div>

            <div class="col-md-4"></div>
        </div>


</form>
<table class="table table-bordered">
            <thead>
                <tr>

                    <th>Jenis</th>
                    <th>Vendor</th>
                    <th>Negeri</th>
                    <th>No.</th>
                    <th>Pakej</th>
                    <th>Harga</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
            <?php

                $budget = $_POST['bajet'];
                $select = $_POST['sel'];
                $check = $_POST['check_list'];

                if (isset($_POST['search'])) {
                    mysql_select_db($database_conn, $conn);



                $sql = mysql_query(" SELECT * 
                FROM vendor 
                RIGHT JOIN item 
                ON vendor.v_id=item.v_id
                WHERE item.harga <= ' $budget ' 
                AND vendor.state = ' $select '
                AND vendor.type = ' $check ' );
                while($row = mysql_fetch_array($sql)) {

            ?>
            <tr>

                    <td><?php echo $row['type'] ?></td>
                    <td><?php echo $row['companyName'] ?></td>
                    <td><?php echo $row['state'] ?></td>
                    <td><?php echo $row['contact'] ?></td>
                    <td><?php echo $row['harga'] ?></td>
                    <td><?php echo $row['pakej'] ?></td>
                    <td><a href="index.php?v_id=<?php echo $row['v_id']?>">View Package</a></td>


                </tr>
                <?php }
            }
            ?>
            </tbody>
        </table>
</div>

1 个答案:

答案 0 :(得分:0)

使用implode()复选框从所选复选框的数组转换为字符串。

<?php
$budget = $_POST['bajet'];
$select = $_POST['sel'];
$check = implode("','", $_POST['check_list']);
$finalCheck = "'".$check ."'";

if (isset($_POST['search'])) {
    mysql_select_db($database_conn, $conn);

$sql = mysql_query(" SELECT * 
FROM vendor 
RIGHT JOIN item 
ON vendor.v_id=item.v_id
WHERE item.harga <= '%". $bajet . "%' 
AND vendor.state = '%". $select ."%'
AND vendor.type IN (". $finalCheck ." )" );
?>