简体   繁体   English

从表单中选择/选择PHP语句

[英]IF/Select PHP statements from a form

So i have this form: 所以我有这种形式:

    <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>

Getting information from a MySQL database. 从MySQL数据库获取信息。 The idea is to be able to search for a filter that matches the form data, but if no selection is chosen (eg, if no connector type is chosen, the search continues but just doesn't search for filters with x connector type.) I have it working for '$highfreq' but can't seem to get it working for Connector_type and Construction_type. 这个想法是能够搜索与表单数据匹配的过滤器,但是如果未选择任何选择(例如,如果未选择连接器类型,则搜索将继续,但是不会搜索具有x连接器类型的过滤器。)我有它为'$ highfreq'工作,但似乎无法使它为Connector_type和Construction_type工作。 I'm not sure if i'm going at it the right way. 我不确定我是否会按照正确的方式进行操作。 Here is my PHP select 这是我的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>

Thanks in advance, Tom. 预先感谢,汤姆。

set the empty construction type - 设置空的构造类型-

            <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>

after submission - 提交后-

$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);
}

you will get the query. 您将得到查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM