按照我之前的问题:PHP code won't pull database information
我想通过选择选项导航交易页面。 在每个选择选项中,它将使用来自数据库的client_id并过滤列表(动态地应该更改页面以根据客户端ID过滤交易信息)。
我的sql(里面没有:client变量)显示以下信息:
我使用选择选项的部分PHP代码如下:
<?php
//url /index.php?action=clients
include('header.php'); // create top box
include('sidemenu.php'); // create side menu
//database connection
echo "<div id='content'>";
include('pdo_connect.php');
//Read data type
$type = "";
if (isset($_REQUEST['action']))
$type = $_REQUEST['action'];
//echo 'Action: {$type}';
switch($type) {
case 'transactions' :
$sql = "SELECT products.product_title, products.product_description, products.unit_price,
p_clients.first_name, p_clients.last_name, sales.quantity FROM p_clients INNER JOIN
sales ON p_clients.client_id = sales.client_id INNER JOIN products ON products.product_id = sales.product_id
WHERE client_id = :client_id";
$values = array(':client_id'=>$_REQUEST['client_id']);
$transactions = getAll($sql, $values);
displayTransactionList($transactions);
break;
default:
defaultView();
break;
}
include('footer.php');
function defaultView() {
?>
<!-- add page content -->
<!-- <div id='content'>-->
<h2>Welcome to our movie store</h2>
</div>
<div id = 'image'></div>
<div id = 'box'>
<p id = 'text-box'>
Welcome to International Electronics, the store where technology is endless. Please
feel free to browse our online store items and current customer interactions.
</p>
</div>
<?php
}
function displayTransactionList($transactions) {
//echo "<div id='content'>
echo"<h2>List of Client Transactions</h2>";
echo " <p>
<h3> Select Specific Client </h3>
<form id='myform' method='post' action='index.php'>
<select id = 'category' name='client_id' >
<option selected= 'selected'> --select id--</option>
<option value = '1'>Client ID 1</option>
<option value = '2'>Client ID 2</option>
<option value = '3'>Client ID 3</option>
<option value = '4'>Client ID 4</option>
<option value = '5'>Client ID 5</option>
<option value = '6'>Client ID 6</option>
<option value = '7'>Client ID 7</option>
<option value = '8'>Client ID 8</option>
<option value = '9'>Client ID 9</option>
<option value = '10'>Client ID 10</option>
</select>
</li>
<input type='hidden' name= 'action' value = 'transactions'/>
</form></p>";
echo "<table id = 'long'>";
echo "<tr><td id = 'title'>First Name</td><td id= 'title'>Last Name</td><td id = 'title'>Product Title</td>
<td id = 'title'>Product Description</td><td id = 'title'>Cost</td><td id = 'title'>Quantity</td></tr>";
//display each record
for ($i = 0; $i < count($transactions); $i++){
echo "<tr><td>{$transactions[$i]['first_name']}</td><td> {$transactions[$i]['last_name']}
</td><td> {$transactions[$i]['product_title']} </td><td> {$transactions[$i]['product_description']} </td><td>
{$transactions[$i]['unit_price']} </td><td> {$transactions[$i]['quantity']} </td></tr>";
}
echo "</table>";
echo "</div>";
}
function getAll($sql, $values =null){
global $db;
$statm = $db->prepare($sql);
//Method 4
//assign a value to named parameters using an array
//$values= array(':genre'=>'drama');
$statm->execute($values);
//Fetch all records
$result = $statm->fetchAll();
return $result;
}
Script.js在单击选择选项时更改页面:
document.ready(function(){
$('#category').on('change', function() {
//send form data
$('#myform').submit();
$('#content').html(); //also tried $('#content').html(response);
});
});
sidemenu.php
<div id = 'top'>
<div class = "nav">
<ul id = "nav1" class= "text-left">
<div id = 'text-left'>
<ul id = "nav" class= "text-left">
<li><a href='index.php'>Home</a></li>
<li><a href='index.php?action=products&product_type=tv'>TV Products</a></li>
<li><a href='index.php?action=products&product_type=cell'>Cell Phone Products</a></li>
<li><a href='index.php?action=products&product_type=computer'>Computer Products</a></li>
<li><a href='index.php?action=clients'>List of Customers</a></li>
<li><a href='index.php?action=transactions'>List of Transactions</a></li>
</ul>
</div>
</div>
更新:
如果我尝试: $ values = array(&#39;:client_id&#39; =&gt; $ _ POST [&#39; client_id&#39;]);
没有任何改变。
![在此输入图片说明] [3]