I'm trying to figure out the appropriate way of writing a query that would display results based on whether one or two variables are present. I'm still quite new to PHP, though I am learning, albeit slowly.
My current code, which accepts only one variable for a search criteria is below. As you can see I have only one _POST variable I am receiving $reg
What I'd like to do is receive a second variable
$toest=$_POST['toestelID'];
If only $reg contains a value {and $toest is Null/Empty) I would like it to query the WHERE statement as such
WHERE vg.luchtvaartmaatschappijID= '$reg'
If $reg and $toest both contain values then it would be WHERE vg.luchtvaartmaatschappijID= '$reg' AND vg.toestelID='$toest'
I know this can be done with a IF / Else type statement, I just don't know how to program this as such.
Original Code:
<?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');
}
}
?>