如何将多个sql查询链接到html选择表单?

时间:2015-12-10 07:42:07

标签: php html mysqli

我有一个脚本,我一直无法上班,我有一个表单有一个选择选项,当用户选择数据库被查询并检索到请求的数据时,我无法形象如何通过表单调用查询。我已经包含了我的代码,我是否需要将查询放入数组中,然后循环遍历它们?或者我是否需要将查询放入自己的变量中,然后在表单中使用此变量?谢谢你的指导!

<?php
session_start(); 
$page_title = "Pagination";
 require_once ("./includes/variables.inc.php");
$dbc = mysqli_connect(HOST, USER, PWD, NAME)     
    or die ('Cannot connect to database');
 $sql = "SELECT COUNT(productCode) FROM products"
    or die ('Cannot Select database');  
$query = $dbc->query($sql);
$row = $query->fetch_row();
$rows = $row[0];
$page_rows = 10;
$last = ceil($rows/$page_rows);
if($last < 1){
    $last = 1;
    }
 $pagenum = 1;
if(isset($_GET['pn'])){
    $pagenum = preg_replace('#[^0-9]#', '', $_GET['pn']);
    }
if ($pagenum < 1) {
    $pagenum = 1;
    }
    else if ($pagenum > $last) {
    $pagenum = $last;
    }
$limit = 'LIMIT ' .($pagenum - 1) * $page_rows .',' .$page_rows;
// This is the query again, it is for grabbing just one page worth of rows by applying $limit
//queries for form select
$sql = "SELECT productCode FROM products ORDER BY productCode DESC $limit";
$query = $dbc->query($sql);
// Select queries
if(isset($_POST['productCode'])){
    $sql = "SELECT productCode FROM products ORDER BY productCode DESC $limit";
    $query = $dbc->query($sql);
    }
if(isset($_POST['productName'])){
    $sql = "SELECT productName FROM products ORDER BY productCode DESC $limit";
    $query = $dbc->query($sql);
    }
if(isset($_POST['productLine'])){
    $sql = "SELECT productLine FROM products ORDER BY productCode DESC $limit";
    $query = $dbc->query($sql);
    }
if(isset($_POST['productScale'])){
    $sql = "SELECT productScale FROM products ORDER BY productCode DESC $limit";
    $query = $dbc->query($sql);
    }
if(isset($_POST['productVendor'])){
    $sql = "SELECT productVendor FROM products ORDER BY productCode DESC $limit";
    $query = $dbc->query($sql);
    }
if(isset($_POST['productDescription'])){
    $sql = "SELECT productDescription FROM products ORDER BY productCode DESC $limit";
    $query = $dbc->query($sql);
    }
if(isset($_POST['buyPrice'])){
    $sql = "SELECT productLine FROM products ORDER BY productCode DESC $limit";
    $query = $dbc->query($sql);
    }
if(isset($_POST['quantityInStock'])){
    $sql = "SELECT quantityInStock FROM products ORDER BY productCode DESC $limit";
    $query = $dbc->query($sql);
    }
if(isset($_POST['MSRP'])){
    $sql = "SELECT MSRP FROM products ORDER BY productCode DESC $limit";
    $query = $dbc->query($sql);
    }
$textline1 = "users (<b>$rows</b>)";
$textline2 = "Page <b>$pagenum</b> of <b>$last</b>";

$paginationCtrls = '';

if($last != 1){
      if ($pagenum > 1) {
        $previous = $pagenum - 1;
        $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$previous.'">Previous</a> &nbsp; &nbsp;';
              for($i = $pagenum-4; $i < $pagenum; $i++){
            if($i > 0){
                $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> &nbsp; ';
            }
        }
    }
    // Render the target page number, but without it being a link
    $paginationCtrls .= ''.$pagenum.' &nbsp; ';
        for($i = $pagenum+1; $i <= $last; $i++){
        $paginationCtrls .= '<a href="'.$_SERVER['PHP_SELF'].'?pn='.$i.'">'.$i.'</a> &nbsp; ';

        if($i >= $pagenum+4){
            break;
        }
    }

    if ($pagenum != $last) {
        $next = $pagenum + 1; $paginationCtrls .= ' &nbsp; &nbsp; <a href="'.$_SERVER['PHP_SELF'].'?pn='.$next.'">Next</a> ';
    }
}

$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>
            <td>Name</td>
            <td>Product Line</td>
            <td>Scale</td>
            <td>Vendor</td>
            <td>Description</td>
            <td>Price</td>
            <td>Quanity</td>
            <td>MSRP</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>";
    }

// Close your database connection
 mysqli_close($dbc);

我还包括表格

<select name="value">
        <option value="<?php echo $row['productCode']; ?>"> Product ID</option>
        <option value="<?php echo $row['productName']; ?>">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>
 <br>
 <input name="submit" id="submit" type="submit" value="Select"><br>
</form>

        <p><br><?php echo $textline1; ?> Paged
        <?php echo $textline2; ?>
        <?php echo $list; ?>
        <div id="pagination_controls"><br>
        <?php echo $paginationCtrls; ?></div>

1 个答案:

答案 0 :(得分:2)

表单选择菜单名为value,但您正在尝试在$ _POST数组中查找单个值,就好像它们是表单元素的名称一样 - 这不起作用。 / p>

而不是采用这种方法,或许更多这些方法可能会有效。

<?php
    if( $_SERVER['REQUEST_METHOD']=='POST' ){
        if( isset( $_POST['value'] ) ){
            switch( $_POST['value'] ){
                case 'productCode': $sql = "SELECT productCode FROM products ORDER BY productCode DESC $limit";break;
                case 'productName': $sql = "SELECT productName FROM products ORDER BY productCode DESC $limit";break;
                case 'productLine': $sql = "SELECT productLine FROM products ORDER BY productCode DESC $limit";break;
                /* etc */
            }
        }
    }
?>


<form name='userselection' method='post'>
    <select name='value'>
        <option value='productCode'>Code
        <option value='productName'>Name
        <option value='productLine'>Line
        <!-- etc -->
    </select>
</form>