什么是我的内心问题?

时间:2016-04-19 05:02:41

标签: php html mysql sql html5

我想制作一个表格,用户可以根据他们的输入和选择搜索某些信息。填写数据的所有表格将显示使用table..got error with my implode,它给了我警告implode():无效第111行传递的参数是

$check = implode("','", $_POST['check_list']);

然后我尝试在复选框上勾选两个值,它给了我      mysql_fetch_array()期望参数1是资源,第125行中给出的布尔值为

while($row = mysql_fetch_array($sql)) {

。      
                 

      <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
                $check = array();
             $budget = $_POST['bajet'];
             $select = $_POST['sel'];
               $check = implode("','", $_POST['check_list']);
                 $finalCheck = "'".$check."'";
                 $check = array();

           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 IN ('%". $finalCheck ." %')" );
                        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 }
                } 
                print_r($_POST['check_list'] );
                ?>

                </tbody>
            </table>
    </div>

3 个答案:

答案 0 :(得分:2)

始终通过打印值调试代码并检查所需的输出

请注意,check_list的帖子值为

$_POST['check_list'] = ['Jurufoto','Hiburan'];

所以根据你的代码 在像这样的爆炸之后

$check = implode("','", $_POST['check_list']);

$finalCheck = "'".$check."'";

你的$ finalCheck字符串就像这个“'Jurufoto','Hiburan'”

当你用这样的查询结合它时

AND vendor.type IN ('%". $finalCheck ." %')" );

它变得像这样

 AND vendor.type IN ('%'Jurufoto','Hiburan' %')" );

这是错误的sql语句eather你必须像这样改变

 AND vendor.type IN (". $finalCheck .")" );

或者,如果您想要查看,那么您必须看到这个帖子

MySQL IN with LIKE

确保在获取数据之前打印$sql并将其与所需的输出匹配

现在您的代码将是这样的

<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>
        </div>
    </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>

</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
    if (isset($_POST['search'])) {
        $check = array();
        $budget = $_POST['bajet'];
        $select = $_POST['sel'];
        $check = implode("','", $_POST['check_list']);
        $finalCheck = "'" . $check . "'";
        $check = array();

        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 IN (" . $finalCheck . ")");
        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>

不要使用mysql连接,因为从5.5.0开始不推荐使用mysql_ *函数。使用 mysqli PDO 进行数据库连接。

答案 1 :(得分:0)

像这样使用

$check = implode(",", $_POST['check_list']);

答案 2 :(得分:0)

由于您在创建$ finalCheck时已经添加了单引号,因此无需在子句中再次添加单引号。

$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 IN (%". $finalCheck ." %)" );