我有这样的问题,这是我的选择选项无法正常工作。我创建的第一个选择选项无法正常运行。只有'jabatan'选项不起作用,而其他人可以。当我尝试搜索'jabatan'并点击'cari'按钮时,它不起作用。
function listAset($PK_lokasiaset,$jbtn,$bhgn,$pnmptn,$naset){
$where = "";
if($PK_lokasiaset == "") $where = "";
else $where = "WHERE PK_lokasiaset = '".$PK_lokasiaset."'";
if($jbtn != ''){
if($jbtn == '99'){}
else if($where == ""){ $where = "WHERE loak.FK_jabatan = '".$jbtn."'";}
else $jbtn .= " AND loak.FK_jabatan = '".$jbtn."' ";
}
if($bhgn != '99'){
if($where == ""){ $where = "WHERE loak.FK_bahagian = '".$bhgn."'";}
else $where .= " AND loak.FK_bahagian = '".$bhgn."' ";
}
if($pnmptn != '99'){
if($where == ""){ $where = "WHERE loak.FK_penempatan = '".$pnmptn."'";}
else $where .= " AND loak.FK_penempatan = '".$pnmptn."' ";
}
if($naset != '99'){
if($where == ""){ $where = "WHERE loak.FK_aset = '".$naset."'";}
else $where .= " AND loak.FK_aset = '".$naset."' ";
}
$sql = "SELECT PK_lokasiaset,loak.nosiri,loak.FK_jabatan,loak.FK_bahagian,loak.FK_penempatan,loak.FK_aset,jab.namajabatan AS jabatan,bah.namabahagian AS bahagian,pen.butiran AS penempatan,kat.namaaset AS kategori,klas.namaaset AS klasifikasi,aset.namaaset AS naset
FROM lokasiaset loak
LEFT JOIN jabatan jab ON jab.PK_jabatan = loak.FK_jabatan
LEFT JOIN bahagian bah ON bah.PK_bahagian = loak.FK_bahagian
LEFT JOIN penempatan pen ON pen.PK_penempatan = loak.FK_penempatan
LEFT JOIN aset ON aset.PK_aset = loak.FK_aset
LEFT JOIN klasifikasiaset klas ON klas.PK_klasifikasiaset = aset.FK_klasifikasiaset
LEFT JOIN kategoriaset kat ON kat.PK_kategoriaset = aset.FK_kategoriaset ".$where." ORDER BY nosiri ASC";
$_SESSION['sqlaset'] = $sql;
//echo $sql;
$query = mysql_query($sql);
if($query) return $query;
else return mysql_error(); }
我的HTML看起来像这段代码
<form class="form-horizontal form-label-left input_mask" action="senaraiaset.php" enctype="multipart/form-data" method="post" >
<div class="form-group">
<div class="row">
<div class="col-md-6 col-sm-6 col-xs-12">
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Jabatan</label>
<div class="col-md-9 col-sm-9 col-xs-12">
<select name="jabatan" class="form-control" onChange="showbhgn(this.value)">
<option value="99">-- Pilih --</option>
<?php
$sjab = "SELECT * FROM jabatan ORDER BY kodjabatan ASC";
$qjab = mysql_query($sjab);
while($rjab = mysql_fetch_array($qjab)){
?>
<option value="<?php echo $rjab['PK_jabatan']; ?>"><?php echo $rjab['namajabatan']; ?></option>
<?php } ?>
</select>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Bahagian </label>
<div class="col-md-9 col-sm-9 col-xs-12" id="bahagian">
<select name="bahagian" class="form-control">
<option value="99">-- Pilih --</option>
<?php
$sjab = "SELECT * FROM bahagian ORDER BY kodbahagian ASC";
$qjab = mysql_query($sjab);
while($rjab = mysql_fetch_array($qjab)){
?>
<option value="<?php echo $rjab['PK_bahagian']; ?>"><?php echo $rjab['namabahagian']; ?></option>
<?php } ?>
</select>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Penempatan </label>
<div class="col-md-9 col-sm-9 col-xs-12">
<select name="penempatan" class="form-control">
<option value="99">-- Pilih --</option>
<?php
$spen = "SELECT * FROM penempatan ORDER BY PK_penempatan ASC";
$qpen = mysql_query($spen);
while($rpen = mysql_fetch_array($qpen)){
?>
<option value="<?php echo $rpen['PK_penempatan']; ?>"><?php echo $rpen['butiran']; ?></option>
<?php } ?>
</select>
</div>
</div>
</div>
<div class="col-md-6 col-sm-6 col-xs-12">
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Kategori Aset</label>
<div class="col-md-9 col-sm-9 col-xs-12">
<select name="kategoriaset" class="form-control" onChange="showkat(this.value)">
<option value="99">-- Pilih --</option>
<?php
$sjab = "SELECT * FROM kategoriaset ORDER BY PK_kategoriaset ASC";
$qjab = mysql_query($sjab);
while($rjab = mysql_fetch_array($qjab)){
?>
<option value="<?php echo $rjab['PK_kategoriaset']; ?>"><?php echo $rjab['namaaset']; ?></option>
<?php } ?>
</select>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Klasifikasi Aset </label>
<div class="col-md-9 col-sm-9 col-xs-12" id="kategoriaset">
<select name="kategoriaset" class="form-control" onChange="showaset(this.value)">
<option value="99">-- Pilih --</option>
<?php
$sjab = "SELECT * FROM klasifikasiaset ORDER BY PK_klasifikasiaset ASC";
$qjab = mysql_query($sjab);
while($rjab = mysql_fetch_array($qjab)){
?>
<option value="<?php echo $rjab['PK_klasifikasiaset']; ?>"><?php echo $rjab['namaaset']; ?></option>
<?php } ?>
</select>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Nama Aset </label>
<div class="col-md-9 col-sm-9 col-xs-12" id="namaasets" >
<select name="namaaset" class="form-control" >
<option value="99">-- Pilih --</option>
<?php
$sjab = "SELECT * FROM aset ORDER BY PK_aset ASC";
$qjab = mysql_query($sjab);
while($rjab = mysql_fetch_array($qjab)){
?>
<option value="<?php echo $rjab['PK_aset']; ?>"><?php echo $rjab['namaaset']; ?></option>
<?php } ?>
</select>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="ln_solid"></div>
<div class="form-group">
<div class="col-md-12 col-sm-12 col-xs-12">
<input type="submit" name="cari" value="Cari" class="btn btn-primary" />
<input type="button" name="reset" value="Semula" class="btn btn-default" />
</div>
</div>
</form>
</div>
<br>
<div class="row">
<table id="datatable-fixed-header" class="table table-striped table-bordered">
<thead>
<tr>
<th>Bil</th>
<th>Kod Aset</th>
<th>Kategori Aset</th>
<th>Klasifikasi Aset</th>
<th>Penempatan</th>
<th>Jabatan</th>
<th>Bahagian</th>
<th>Nama Aset</th>
</tr>
</thead>
<tbody>
<?php
//$jbtn = '';
//$bhgn = '';
//$pnmptn = '';
/*$kat = '';
$klas = '';
$naset = '';*/
if(isset($_POST['cari'])){
$jbtn = $_POST['jabatan'];
$bhgn = $_POST['bahagian'];
$pnmptn = $_POST['penempatan'];
//$kat = $_POST['kategoriaset'];
//$klas = $_POST['klasifikasiaset'];
$naset = $_POST['namaaset'];
}
$query = listAset('',$jbtn,$bhgn,$pnmptn,$naset);//,$klas,$kat
//echo '-'.$query;
$bil = 0;
while($row = mysql_fetch_array($query))
{
$bil++;
?>
<tr>
<td><?php echo $bil; ?></td>
<td><a style="color:#6197d0;" class="" href="asetAct1.php?aset=<?php echo $row['PK_lokasiaset']; ?>"><?php echo $row['nosiri']; ?></a></td>
<td><?php echo $row['kategori']; ?></td>
<td><?php echo $row['klasifikasi']; ?></td>
<td><?php echo $row['penempatan']; ?></td>
<td><?php echo $row['jabatan']; ?></td>
<td><?php echo $row['bahagian']; ?></td>
<td><?php echo $row['naset']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
答案 0 :(得分:0)
我打赌这是你的问题
if($PK_lokasiaset == ""){
$where = "";
}else{
$where = "WHERE PK_lokasiaset = '".$PK_lokasiaset."'";
}
如果它是空的,你只需要
$where = "";
如果没有,
$where = "WHERE PK_lokasiaset = '".$PK_lokasiaset."'"
随后你会得到
$where = "AND ...";
如果没有单词Where
,则它为空,并且您的查询将失败
select * from table AND var = "something"
我会改变这个烂摊子
if($PK_lokasiaset == "") $where = "";
else $where = "WHERE PK_lokasiaset = '".$PK_lokasiaset."'";
if($jbtn != ''){
if($jbtn == '99'){}
else if($where == ""){ $where = "WHERE loak.FK_jabatan = '".$jbtn."'";}
else $jbtn .= " AND loak.FK_jabatan = '".$jbtn."' ";
}
这样的事情
$where = array();
if($PK_lokasiaset != "")
$where[] = "PK_lokasiaset = '{$PK_lokasiaset}'";
...
if($naset != '99')
$where[] = "loak.FK_aset = '{$naset}'";
然后就这样做
'SELECT * FROM table WHERE '.implode(' AND ', $where );
我会认真使用占位符和预处理语句,以避免SQL注入。
可以轻松完成 $params = array();
$where = array();
if($PK_lokasiaset != "") {
$where[] = "PK_lokasiaset = :PK_lokasiaset";
$params[':PK_lokasiaset'] = $PK_lokasiaset;
}
...
if($naset != '99'){
$where[] = "loak.FK_aset = :FK_aset";
$params[':FK_aset'] = $PK_lokasiaset
}
然后用pdo你就可以了
$stmt = $PDO->prepare( 'SELECT * FROM table WHERE '.implode(' AND ', $where ));
$stmt->execute( $params );
使用数组和内爆更加清晰。只有在您的查询中混合使用AND
和OR
时,唯一能够顺利运行的时间。