我试图找出编写查询的适当方法,该查询将根据是否存在一个或两个变量来显示结果。虽然我正在学习,但我对PHP仍然很陌生,虽然很慢。
我的当前代码仅接受一个搜索条件的变量,如下所示。如你所见,我只有一个_POST变量,我收到$ reg
我想做的是接收第二个变量
$toest=$_POST['toestelID'];
如果只有$reg
包含值{且$toest
为Null / Empty),我希望它能够查询WHERE语句
WHERE vg.luchtvaartmaatschappijID= '$reg'
如果$ reg和$ toest都包含值,那么它将是 在哪里vg.luchtvaartmaatschappijID =' $ reg' AND vg.toestelID =' $ toest'
我知道这可以通过IF / Else类型声明来完成,我只是不知道如何对此进行编程。
原始代码:
<?php
$reg=$_POST['luchtvaartmaatschappijID'];
if(isset($_POST['submit'])){
if(isset($_GET['go'])){
$reg=$_POST['luchtvaartmaatschappijID'];
$color1 = "#C2DFFF";
$color2 = "#FFFFFF";
$row_count = 0;
//connect to the database
$db=mysql_connect ("localhost", "someusername", "somepassword") or die ('I cannot connect to
the database because: ' . mysql_error());
//-select the database to use
$mydb=mysql_select_db("somedatabase");
//-query the database table
$sql="SELECT vg.*, lvm.luchtvaartmaatschappij AS lvmnaam, lvm.luchtvaartmaatschappijID as lvmid,
t.toestel
FROM tbl_vliegtuiggegevens vg
INNER JOIN tbl_luchtvaartmaatschappij lvm
ON vg.lvmID = lvm.luchtvaartmaatschappijID
INNER JOIN tbl_toestel t
ON vg.toestelID = t.toestelID
WHERE vg.luchtvaartmaatschappijID= '$reg'
ORDER BY lvm.luchtvaartmaatschappij ASC, t.toestel ASC, vg.inschrijvingnmr ASC";
//-run the query against the mysql query function
$result=mysql_query($sql);
echo "zoekresultaten:<br>
<table border='0' cellspacing='0'>
<tr>
<th width='100' valign='top'><div align='left'>cn</div></th>
<th width='150' valign='top'><div align='left'>reg</div></th>
<th width='300' valign='top'><div align='left'>Luchtvaartmaatschappij</div></th>
<th width='330' valign='top'><div align='left'>Toestel</div></th>
</tr>
<tr>
<th height='1' colspan='4' valign='top' bgcolor='#333333'></th>
</tr>";
//-create while loop and loop through result set
while($row=mysql_fetch_array($result)){
$row_color = ($row_count % 2) ? $color1 : $color2;
$reg=$row['inschrijvingnmr'];
$toestel=$row['toestel'];
$cnid=$row['cn'];
$lvmid=$row['lvmid'];
$lvmnaam=$row['lvmnaam'];
$ID=$row['vliegtuiggegevenID'];
//-display the result of the array
echo "<tr>";
echo "<td bgcolor='$row_color'>".$cnid."</td>";
echo "<td bgcolor='$row_color'><a target='_blank'
href=\"../vliegtuiggegevens/vliegtuiggegevens_form.php?id=$reg&cid=$cnid&lvid=$lvmid\">" .$reg
. "</a></td>";
echo "<td bgcolor='$row_color'>" . $lvmnaam . "</td>";
echo "<td bgcolor='$row_color'>" . $toestel . "</td>";
echo "</tr>";
// Add 1 to the row count
$row_count++;
}
echo "</table>";
}
if( mysql_num_rows($result) == 0 )
{
include('../vliegtuiggegevens/voegvliegtuiggegevenstoe_form.php');
}
}
?>
答案 0 :(得分:1)
首先我强烈建议使用PDO,不仅更安全(没有sql注入)。当你掌握它时,它可以更容易使用,并提供更多关于如何处理结果数据的方法。不推荐使用mysql函数库。所以你应该使用PDO或mysqli。
要回答你的问题,我想建立一个这种情况的条件数组。例如,
$PDO = new Pdo('mysql:host=localhost;database', 'user', 'password');
//enable pdo error reporting, now you don't need all those die( 'bla' ) stuff.
$PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
//create a stub of your query
$sql = 'SELECT * FROM some_table WHERE';
$where = array();
$params = array();
if( isset($_POST['something'] ) ){
$where[] = 'something = :something'; // using pdo placeholder
$params[':something'] = $_POST['something'];
}
if( isset($_POST['otherthing'] ) ){
$where[] = 'otherthing = :otherthing'; // using pdo placeholder
$params[':otherthing '] = $_POST['otherthing'];
}
//if you need a default value simply count your wheres
// if not just use the part in the else chunk
if( empty( $where ) ){
$sql .= ' id = 0'; //no results on empty conditions etc...
}else{
//now that you have some stuff
$sql .= ' '.implode(' AND ', $where); //collapse it
}
// pdo can be trickier to see what your query actually is
// so you can do something like this to check it out. but you'll have to do the quotes etc..
//echo str_replace( array_keys( $params ), $params, $sql );
//prepare the query
$stmt = $PDO->prepare( $sql );
//execute it
$stmt->execute( $params );
//fetch the results
$res = $stmt->fetchAll(PDO::FETCH_ASSOC);
这应该会给你一些你需要的东西。
供参考
http://php.net/manual/en/book.pdo.php
(参见以下链接中的示例#2)
答案 1 :(得分:0)
是的,你做对了 - 你需要的是一些if和else语句。这是一个应该有用的代码片段:
$toest=$_POST['toestelID'];
$reg=$_POST['reg'];
$where_stmt="";
if((isset($reg) && !empty($reg)) && (!isset($toest) || empty($toest)))
{
$where_stmt="WHERE vg.luchtvaartmaatschappijID='".$reg."'";
}
elseif((isset($toest) && !empty($toest)) && (!isset($reg) || empty($reg)))
{
$where_stmt="WHERE vg.toestelID='".$toest."'";
}
elseif(isset($reg) && !empty($reg) && isset($toest) && !empty($toest))
{
$where_stmt="WHERE vg.luchtvaartmaatschappijID='".$reg."' and vg.toestelID='".$toest."'";
}
else
{
//where statement should be excluded as no filters are available
}
$sql="SELECT vg.*, lvm.luchtvaartmaatschappij AS lvmnaam, lvm.luchtvaartmaatschappijID as lvmid,
t.toestel FROM tbl_vliegtuiggegevens vg INNER JOIN tbl_luchtvaartmaatschappij lvm ON vg.lvmID = lvm.luchtvaartmaatschappijID INNER JOIN tbl_toestel t ON vg.toestelID = t.toestelID $where_stmt ORDER BY lvm.luchtvaartmaatschappij ASC, t.toestel ASC, vg.inschrijvingnmr ASC";