将id变量传递给modal页,以便通过包含此php页来执行日期筛选

anauzrmj  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(422)

我不知道为什么这样不行。也许我缺乏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
                                                }
                                                ?>
bakd9h0s

bakd9h0s1#

我相信问题出在你使用 date 在sql中。

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

有用的链接:
pdo和mysql之间
“date”在mysql中是关键字吗?
转换为布尔值

相关问题