所以我有这样的形式:
<form name="form1" action="passearch.php" method="POST" class="basic-grey"><h1 class="offset3"> Search for a Bandpass Filter
<span>Please complete all the fields.</span></h1>
<label>
<span><b>Start Bandpass(fL):</b></span>
<input type="text" name="Lowfreq" placeholder="Lowest Frequency">
</label>
<label>
<span><b>Stop Bandpass(fH):</b></span>
<input type="text" name ="Highfreq" placeholder="Highest Frequency">
</label>
<label>
<span>
<b>Connector Type:</b> </span>
<select name="connector_type">
<option value=''></option>
<option value="Any">Any</option>
<option value="no_Connector">No Connector</option>
<option value="SMA-F">SMA-F</option>
<option value="SMA-M">SMA-M</option>
</select>
</label>
<label>
<span>
<b>Construction Type:</b> </span>
<select name="construction_type">
<option value="any">Any</option>
<option value="combline">Combline</option>
<option value="lumped">Lumped Element</option>
<option value="mixed">Mixed</option>
<option value="suspended substrate">Suspended Substrate</option>
<option value="waveguide">Waveguide</option>
</select>
</label>
<input type="submit" class="small button" value="Submit">
</form>
从MySQL数据库获取信息。我们的想法是能够搜索与表单数据匹配的过滤器,但是如果没有选择选择(例如,如果没有选择连接器类型,搜索将继续,但不会搜索具有x连接器类型的过滤器。)我让它为'$ highfreq'工作,但似乎无法让它适用于Connector_type和Construction_type。我不确定我是否会采用正确的方式。这是我的PHP选择
<?php
$username = "root";
$password = "Toom13371!";
$lowfreq = $_POST['Lowfreq']; # User-Supplied Data. I'm A
$highfreq = $_POST['Highfreq']; # User-Supplied Data. Placeholder yo.
$construction = $_POST['construction_type']; # User-Supplied Data. Still holding
$connector = $_POST['connector_type']; # User-Supplied Data. that place.
try {
$dbh = new PDO("mysql:host=127.0.0.1;dbname=filters", $username, $password);
} catch(PDOException $e) {
echo $e->getMessage();
}
//print
//echo $construction; #test
// Select some data
$sth = $dbh->prepare("SELECT id, type_code, connector_type, construction_type, start_passband, stop_passband, low_passband, high_passband FROM filter_bandpass WHERE ((start_passband = $lowfreq OR $lowfreq = '') AND (stop_passband = $highfreq OR $highfreq = '')
AND (construction_type = '$construction')
)");
//$sth = $dbh->prepare("SELECT id, type_code FROM filter_bandpass WHERE construction_type = '$construction'");
// Execute the query, replacing the placeholders with their true value
$sth->execute(array(
));
// How many records did we find?
echo "<div class='jumbotron'>";
echo "<h1>Filters Found</h1>";
echo 'We found ' . $sth->rowCount() . ' filters matching your search request.';
echo "</div>";
?>
<hr>
<div class="row-fluid marketing">
<!-- Pwetty little table. -->
<table class="gridtable">
<thead>
<tr>
<th> ID</th>
<th> Type Code</th>
<th> Connector Type</th>
<th>Construction Type</th>
<th> Start Passband </th>
<th> Stop Passband </th>
<th> Low Passband </th>
<th> High Passband </th>
</tr>
</thead>
<tbody>
<?php while ($row = $sth->fetch()): ?>
<tr>
<td><?php echo htmlspecialchars($row['id'])?></td>
<td><?php echo htmlspecialchars($row['type_code']); ?></td>
<th><?php echo htmlspecialchars($row['connector_type']); ?></td>
<th><?php echo htmlspecialchars($row['construction_type']); ?></td>
<th><?php echo htmlspecialchars($row['start_passband']); ?></td>
<th><?php echo htmlspecialchars($row['stop_passband']); ?></td>
<th><?php echo htmlspecialchars($row['low_passband']); ?></td>
<th><?php echo htmlspecialchars($row['high_passband']); ?></td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
先谢谢,汤姆。
答案 0 :(得分:0)
设置空构造类型 -
<select name="construction_type">
<option value="">Any</option>
<option value="combline">Combline</option>
<option value="lumped">Lumped Element</option>
<option value="mixed">Mixed</option>
<option value="suspended substrate">Suspended Substrate</option>
<option value="waveguide">Waveguide</option>
</select>
提交后-
$lowfreq = $_POST['Lowfreq']; # User-Supplied Data. I'm A
$highfreq = $_POST['Highfreq']; # User-Supplied Data. Placeholder yo.
$construction = $_POST['construction_type']; # User-Supplied Data. Still holding
$connector = $_POST['connector_type']; # User-Supplied Data.
$query = "SELECT id, type_code, connector_type, construction_type, start_passband, stop_passband, low_passband, high_passband FROM filter_bandpass WHERE "
$condition = array();
if (!empty($lowfreq)) {
$condition[] = " start_passband = $lowfreq";
}
if (!empty($highfreq)) {
$condition[] = " stop_passband = $highfreq";
}
if (!empty($construction)) {
$condition[] = " construction_type = $construction";
}
if (!empty($construction)) {
$condition[] = " connector_type = $connector";
}
if (!empty($condition)) {
$query .= " ".implode('AND', $condition);
}
您将收到查询。