html表单选择选项,为select上的每个选项分配SQL查询

时间:2015-12-04 05:01:15

标签: php mysql html5 forms

我有一个自引用的选择表单,如何为每个选择选项分配SQL查询?因此,当用户选择时,该选择查询数据库以查找该特定表并输出到页面。我为数据库中的每个表都有一个select选项,并创建了相关的查询。我是否需要将查询放入数组中?我被困在如何进一步前进。我已经包含了下面的一些代码。我是否需要为每个用户选项创建唯一的SQL查询var?任何方向都会有所帮助。感谢

    //this is the first query it gets the count for pagination
        $sql = "SELECT COUNT(productCode) FROM products"
        or die ('Cannot Select database');  


    // This  query is just to get the total count of rows
    //$sql = "SELECT COUNT(productCode) FROM products";
    $query = $dbc->query($sql);

$row = $query->fetch_row();
//this is were I am stuck
    if($_POST['productCode'] == ('productCode')) {
    // query to get code records
    $sql2 = "SELECT productCode FROM products";
}
elseif($_POST['productName'] == 'productName') {
    // query to get product name  records
    $sql3 = "SELECT productName FROM products";
} else {
    // query to get all records
    $sql4 = "SELECT * FROM products";
}
    $list = '';

    while ($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
        $id = $row['productCode'];
        $name = $row['productName'];
        $line = $row['productLine'];
        $scale = $row['productScale'];
        $vendor = $row['productVendor'];
        $description = $row['productDescription'];
        $buy_price = $row['buyPrice'];
        $quanity = $row['quantityInStock'];
        $msrp = $row['MSRP'];
        $list .= "<table>
        <tr> 
            <td class='center'><strong>Product Id</strong></td>

        </tr>
        <tr>
            <td class='center'>$id</td> 
            <td>$name</td>
            <td>$product_line</td>
            <td>$scale</td>
            <td>$vendor</td>
            <td>$description</td>
            <td>$buy_price</td>
            <td>$quanity</td>
            <td>$msrp</td>
        </tr>
        </table>";
    }
    <select name="value">
            <option value="<?=$id; ?>">Product ID</option>
            <option value="<?=$name; ?>">Product Name</option>
            <option value="<?=$line; ?>">Product Line</option>
            <option value="<?=$scale; ?>">Product Scale</option>
            <option value="<?=$vendor; ?>">Product Vendor</option>
            <option value="<?=$description; ?>">Product Description</option>
            <option value="<?=$quanity; ?>">Quanity In Stock</option>
            <option value="<?=$buy_price; ?>">Buy Price</option>
            <option value="<?=$msrp; ?>">MSRP</option>
     </select>

1 个答案:

答案 0 :(得分:0)

<?php
//SQL table rows system names => 'human names for select'
$rows4Search = array(
    'productCode' => 'Product Code',
    'productName' => 'Product Name',
    'productLine' => 'Product Line',
    'productScale' => 'Product Scale',
);

//build html select options
foreach ( $rows4Search as $key=>&$val ){
    $opinonsString .= '<option value="'.$key.'" '.( $_POST['wattafind']==$key ? 'selected' : '' ).' >'.$val.'</option>';

}

//form
echo '
    <form method=POST>
        <select name="wattafind">
            <option>All fields</option>
            '.$opinonsString.'
        </select>
        <input type="submit">
    </form>
';

//catch selected option & put it to your query
if ( $_POST['wattafind'] && array_key_exists( $_POST['wattafind'], $rows4Search) )
    $sql = 'SELECT `'.$_POST['wattafind'].'` FROM products';
else
    $sql = 'SELECT * FROM products';

echo "<pre>".print_r($sql,true)."</pre>";