【问题标题】:i'am have trouble to display my data. I can display data with a specific id. but i don't know how to display data with different id on the same page我无法显示我的数据。我可以显示具有特定 ID 的数据。但我不知道如何在同一页面上显示具有不同 id 的数据
【发布时间】:2022-01-04 23:08:15
【问题描述】:

我用来从数据库中获取数据的 SQL

<?php 
require 'functions.php';

$pesanan = query("SELECT order_name, order_quantity,table_id FROM `pesanan` WHERE transaction_id  = 20;");
?>

循环我可以显示数据,但它仅适用于 transaction_id = 20 的数据

<?php include("temp_sidebar.php");?>
      <div class="header">
          <h3 class="text-center">Order List</h3>
      </div>
    <div class="container-two border border-white border-3">
    
        <div class="order_card">
          <h4 style="text-align: center" >
          <span class="border-bottom border-white border-3">Order 1</span>
          </h4>
          
            <div class="ex1">
            <?php foreach($order as $row): ?>
              <p><?= $row ["order_name"] ?> (<?= $row ["order_quantity"] ?>)</p>
    
              <?php endforeach;?>
            </div>  
            <h6>Table <?= $row ["table_id"] ?></h6>
            <div class="d-grid gap-2">
            <button class="btn btn-success" type="button">Finish</button>
            </div>
        </div>
        
        </div>
    </div>

【问题讨论】:

  • 您正在从数据库中获取查询中具有 20 个 id 的数据。你想如何显示不同的数据可以解释一下吗?

标签: php html sql


【解决方案1】:

替换这个

$pesanan = query("SELECT order_name, order_quantity,table_id FROM `pesanan` WHERE transaction_id  = 20;");

到这个 // 对 $_GET['id'] 进行验证,例如 is_number 和最大长度

$pesanan = query("SELECT order_name, order_quantity,table_id FROM `pesanan` WHERE transaction_id  = ".$_GET['id'].";");

然后尝试使用参数 '?id=20' 访问您的网站,例如:

localhost/myproject/index.php?id=20

【讨论】:

  • 在某种意义上是正确的想法,但请不要鼓励人们使用不安全的代码。您的示例很容易受到 SQL 注入攻击。
猜你喜欢
  • 1970-01-01
  • 2012-11-17
  • 2021-03-26
  • 2020-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-04
相关资源
最近更新 更多