php - what is my implode problems? -
i want make form user can search info based on input , choices.after form filling data display using table..got error implode gave me warning implode(): invalid arguments passed in line 111 is
$check = implode("','", $_post['check_list']);
and after tried tick 2 values on checkbox gave me mysql_fetch_array() expects parameter 1 resource, boolean given in line 125 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 <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 * vendor right join item on vendor.v_id=item.v_id item.harga <= '%". $budget . "%' , vendor.state = '%". $select ."%' , 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>
always debug code printing value , check desired output
let amuse post value check_list
$_post['check_list'] = ['jurufoto','hiburan'];
so according code after imploding this
$check = implode("','", $_post['check_list']); $finalcheck = "'".$check."'";
your $finalcheck string "'jurufoto','hiburan'"
and when concatinate query this
and vendor.type in ('%". $finalcheck ." %')" );
it become this
, vendor.type in ('%'jurufoto','hiburan' %')" );
which wrong sql statement eather have change this
, vendor.type in (". $finalcheck .")" );
or if want check have see thread
and make sure print $sql
before fetching data , match desired output
now code
<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 <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 * vendor right join item on vendor.v_id=item.v_id item.harga <= '%" . $budget . "%' , vendor.state = '%" . $select . "%' , 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 mysql_* functions has been deprecated of 5.5.0. use mysqli or pdo database connectivity.
Comments
Post a Comment