我想制作一个表单,用户可以根据自己的输入和选择搜索某些信息。在填写完所有表单后,数据将使用表显示。。在我的内爆中出错,它给了我警告内爆():在第111行传递的无效参数是
$check = implode("','", $_POST['check_list']);
之后,我试着在它给我的复选框上勾选两个值mysql_fetch_array()期望参数1为resource,第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 <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>
始终通过打印值来调试代码,并检查其是否为所需的输出
有趣的是,你的check_list的post值是
$_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语句是错误的,你必须像这样修改
AND vendor.type IN (". $finalCheck .")" );
或者,如果你想检查类似,那么你必须看到这个线程
MySQL IN与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 <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连接,因为mysql_*函数从5.5.0开始就被弃用了。使用mysqli或PDO进行数据库连接。
像这样使用
$check = implode(",", $_POST['check_list']);
由于您在创建$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 ." %)" );