2015-12-10 85 views
4

我有一個腳本,我一直有麻煩去工作,我有一個窗體,有一個選擇選項,當用戶選擇數據庫被查詢和請求的數據被檢索,我不知道如何通過表單調用查詢。我已經包含了我的代碼,我是否需要將查詢放入一個數組中,然後通過它們循環?或者我需要將查詢放入它們自己的變量中,然後在表單中使用這個變量?感謝您的任何指導!如何將多個sql查詢鏈接到html選擇表單?

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

回答

2

形式選擇菜單被稱爲value但你想,通過事物的外表,找到$ _POST陣列彷彿在各個值他們是表單元素的名稱 - 這是行不通的。

取而代之的是這種方法或許更符合這些方向。

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