douchuoliu4422 2014-09-15 09:31
浏览 35
已采纳

IF /从表单中选择PHP语句

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. 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. I'm not sure if i'm going at it the right way. Here is my PHP select

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

  • 写回答

1条回答 默认 最新

  • doujing5150 2014-09-15 09:40
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 哪个tomcat中startup一直一闪而过 找不出问题
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码