我不知道为什么这不起作用。也许我缺乏 PHP 经验,但我尝试尽可能多地遵循可行的方法,但它不起作用。
我希望执行的是,在用户按下按钮后从页面列表中获取 ID,并将变量传递到引导模式页面。我设法弄清楚如何显示 ID 表格。
但是当涉及到日期过滤时,却失败了。
<?php
require_once 'dbconfig.php';
if (isset($_REQUEST['id'],$_POST["From"], $_POST["to"])) {
$result = '';
$id = intval($_REQUEST['id'],$_POST["From"], $_POST["to"]);
$query = "SELECT * FROM history WHERE ID=:id and date BETWEEN '".$_POST["From"]."' AND '".$_POST["to"]."'";
//
$stmt = $DBcon->prepare( $query );
$stmt->execute(array(':id'=>$id));
$row=$stmt->fetch(PDO::FETCH_ASSOC);
extract($row);
$result .='
<table class="table table-bordered">
<tr>
<th width="10%">Order Number</th>
<th width="35%">Customer Number</th>
<th width="40%">Purchased Item</th>
<th width="10%">Purchased Date</th>
</tr>';
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$result .='
<tr>
<td>'.$row["ID"].'</td>
<td>'.$row["location"].'</td>
<td>'.$row["currentStatus"].'</td>
<td>'.$row["date"].'</td>
</tr>';
}
$result .='</table>';
echo $result;
}?>
但它是这样工作的:
<?php
require_once 'dbconfig.php';
if (isset($_REQUEST['id'])) {
$id = intval($_REQUEST['id']);
$query = "SELECT * FROM history WHERE ID=:id";
$stmt = $DBcon->prepare( $query );
$stmt->execute(array(':id'=>$id));
$row=$stmt->fetch(PDO::FETCH_ASSOC);
extract($row);
}
?>
<div id="purchase_order">
<table class="table table-bordered">
<tr>
<th width="10%">ID</th>
<th width="20%">Location</th>
<th width="20%">Status</th>
<th width="10%">Date</th>
</tr>
<?php
// while($row= mysqli_fetch_array($sql))
// {
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
?>
<tr>
<td><?php echo $row["ID"]; ?></td>
<td><?php echo $row["location"]; ?></td>
<td><?php echo $row["currentStatus"]; ?></td>
<td><?php echo $row["date"]; ?></td>
</tr>
<?php
}
?>
最佳答案
我相信问题出在您在 SQL 中使用date
。
<?php
require_once 'dbconfig.php';
// enable error reporting
error_reporting(E_ALL);
ini_set('display_errors', 1);
if (isset($_REQUEST['id'], $_POST['From'], $_POST['to'])) {
$result = '';
// intval() only has 2 arguments
$id = intval($_REQUEST['id']);
$from = $_POST['From'];
$to = $_POST['to'];
// more likely the problem: date is a reserved word in MySQL -- so wrap it in backquotes
// also, use placeholders for From and To
$query = "SELECT * FROM history WHERE ID = ? AND `date` BETWEEN ? AND ?";
$stmt = $DBcon->prepare($query);
$stmt->execute([$id, $from, $to])); // simplified
$row = $stmt->fetch(PDO::FETCH_ASSOC);
// unnecessary and dangerous
//extract($row);
$result .= '
<table class="table table-bordered">
<tr>
<th width="10%">Order Number</th>
<th width="35%">Customer Number</th>
<th width="40%">Purchased Item</th>
<th width="10%">Purchased Date</th>
</tr>';
// you already fetched the row - no need to do it again
if ($row) {
$result .= '
<tr>
<td>'.$row["ID"].'</td>
<td>'.$row["location"].'</td>
<td>'.$row["currentStatus"].'</td>
<td>'.$row["date"].'</td>
</tr>';
}
$result .= '</table>';
echo $result;
}
?>
有用的链接:
关于php - 将 ID 变量传递到模态页面以通过包含此 php 页面来执行日期过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50472404/