【问题标题】:I'm trying to build a shopping cart for my web application我正在尝试为我的 Web 应用程序构建购物车
【发布时间】:2019-09-29 02:54:29
【问题描述】:

我正在尝试为我的业务应用程序创建一个购物车,我已经编写了此代码,它没有给我任何错误,但它不会检索数据库中的数据

cart.php 看起来像这样

$last_id = mysqli_insert_id($con);
$_SESSION['last_id'] = $last_id;
$sql = "SELECT * FROM product_order WHERE ord_id = '$last_id'";
$result = mysqli_query($con,$sql);
$row = mysqli_fetch_assoc($result);

我创建了一个表格来显示数据

<tr>
                        <th width="5%">Last Id</th>
                        <th width="5%">Order id</th>
                        <th width="5%">Selected Card</th>
                        <th width="15%">Field 1</th>
                        <th width="15%">Field 2</th>
                        <th width="15%">Field 3</th>
                        <th width="15%">Field 4</th>
                        <th width="15%">Field 5</th>
                        <th width="5%">Quantity</th>
                        <th width="5%">Price</th>
                        <th width="5%">Total</th>
                        <th width="15%">Action</th>
                    </tr>
                    <tr>
                    <td><?php echo $last_id?></td>
                    <td><?php echo $row['ord_id']?></td>
                    <td><?php echo $row['card_id']; ?></td>
                    <td><?php echo $row['field1']; ?></td>
                    <td><?php echo $row['field2']; ?></td>
                    <td><?php echo $row['field3']; ?></td>
                    <td><?php echo $row['field4']; ?></td>
                    <td><?php echo $row['field5']; ?></td>
                    <td><?php echo $row['quantity']; ?></td>
                    <td><?php echo $row['price']; ?></td>
                    <td><?php echo $total ?></td>

【问题讨论】:

  • 您对 SQL 注入持开放态度,应该立即解决
  • 通过以下方式检查您的错误:ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);
  • 你正在获取一个行数组作为 $row -> 你需要一个 foreach 循环或类似的循环来输出你的表行
  • 我在$row = mysqli_fetch_assoc($result);这行之后输入,但没有给我错误@MohammadrezaYektamaram
  • if(mysqli_num_rows($result) &gt; 0) { while ($row = mysqli_fetch_array($result)){ 我试过了,但现在它只是一个白页@GrahamRitchie

标签: php html css mysqli cart


【解决方案1】:

试试这个语法:

$sql = "SELECT * FROM product_order WHERE ord_id =" . $last_id;

  <?php foreach ($row as $r) : ?>
  <td><?php echo $r['ord_id']?></td>
  <td><?php echo $r['card_id']; ?></td>
  <td><?php echo $r['field1']; ?></td>
  <td><?php echo $r['field2']; ?></td>
  <td><?php echo $r['field3']; ?></td>
  <td><?php echo $r['field4']; ?></td>
  <td><?php echo $r['field5']; ?></td>
  <td><?php echo $r['quantity']; ?></td>
  <td><?php echo $r['price']; ?></td>
  <?php endforeach; ?>

【讨论】:

  • 不返回任何仍为空的内容
  • 你教他们吗? @CraftCover
  • if(mysqli_num_rows($result) &gt; 0) { while ($row = mysqli_fetch_array($result)){ 我试过了,但现在它只是一个白页,什么都没有
  • 给我这个警告警告:在第 49 行的 C:\xampp\htdocs\Neo\cart.php 中为 foreach() 提供的参数无效
  • 它不起作用,但是代码看起来像这样$last_id = mysqli_insert_id($con); $_SESSION['last_id'] = $last_id; $sql = "SELECT * FROM product_order WHERE ord_id =" . $last_id; $result = mysqli_query($con,$sql); $row = mysqli_fetch_assoc($result);
猜你喜欢
  • 2019-12-09
  • 1970-01-01
  • 2017-07-24
  • 1970-01-01
  • 2011-05-24
  • 2013-08-30
  • 1970-01-01
  • 1970-01-01
  • 2021-07-12
相关资源
最近更新 更多