2

I want to make form where user can search certain info based on their input and choices.after all the form filling the data will be display using table..got error with my implode where it gave me warning implode(): Invalid arguments passed in line 111 which is

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

and after that i tried tick two values on checkbox it gave me mysql_fetch_array() expects parameter 1 to be resource, boolean given in line 125 which is

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 Answers3

2

always debug your code by printing the value and check it for the desired output

Let amuse that your post value for check_list is

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

So according to your code after imploding like this

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

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

your $finalCheck string will be like this "'Jurufoto','Hiburan'"

and when you concatinate it with the query like this

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

it become like this

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

which is wrong sql statement eather you have to change like this

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

OR if you want to check with like then you have to see this thread

MySQL IN with LIKE

And make sure to print the $sql before fetching data and match it with your desired output

Now your code will be like this

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

Don't use mysql connection because the mysql_* functions has been deprecated as of 5.5.0. Use mysqli or PDO for the database connectivity.

Community
  • 1
  • 1
Aman Rawat
  • 2,625
  • 1
  • 25
  • 40
0

use like this

$check = implode(",", $_POST['check_list']);
JYoThI
  • 11,977
  • 1
  • 11
  • 26
0

As you have already added single quote at the time of creation $finalCheck, so no need to add single quote again in clause.

$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 ." %)" );
Dipanwita Kundu
  • 1,637
  • 1
  • 9
  • 14
  • implode(): Invalid arguments passed – Tamimi Brewster Apr 19 '16 at 05:27
  • There is no error in this line : $check = implode("','", $_POST['check_list']); But put it after check it's value. like : if(!empty($_POST['check_list'])) { $check = implode("','", $_POST['check_list']);} – Dipanwita Kundu Apr 19 '16 at 05:30
  • after i replacemy with yours ,now my problem is mysql_fetch_array() expects parameter 1 to be resource, boolean given in line 126 which is while ($row = mysql_fetch_array($sql)) { after i filled all the form and click search. – Tamimi Brewster Apr 19 '16 at 07:24
  • please echo $sql or just update that line: $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 . ")") or die(mysql_error()); – Dipanwita Kundu Apr 19 '16 at 07:32