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