如果设置不适用于未定义的索引,如何修复

时间:2020-05-14 作者:roger

这是我的代码,但我不确定删除错误时缺少了什么:

<!DOCTYPE html>
<html lang="en">
 <head>
 <meta charset="utf-8">
 <title>Week 8 Exercise 4</title>
 <link rel="stylesheet" href="styles.css">
 </head>
 <body>
 <?php

 require_once("conn.php");
 if(isset($_POST[\'quantity\'])) {
 $quantityinstock = $_POST[\'quantity\'];
 }

 $quantityinstock = $dbConn->escape_string($_POST[\'quantity\']);
 $sql =  "select name, quantityInStock, price from product where quantityinstock = \'$quantityinstock\'";
 $results = $dbConn->query($sql)
 or die (\'Problem with query: \' . $dbConn->error);

 ?>
 <h1>Product with stock > <?php echo "$quantityinstock"; ?> </h1>
 <table>
 <tr>
 <th>Name </th>
 <th>Quantity In Stock</th>
 <th>Price</th>
 </tr>
Autumn 2020
Technologies for Web Applications 300582
Page 3 of 5
 <?php
 while ($row = $results->fetch_assoc()) { ?>
 <tr>
 <td><?php echo $row["name"]?></td>
 <td><?php echo $row["quantityInStock"]?></td>
 <td><?php echo $row["price"]?></td>
 <!-- output the other fields here from the $row array -->
 </tr>
 <?php }
 $dbConn->close(); ?>
 </table>
</body>
</html>

1 个回复
SO网友:Jacob Peattie

这似乎与WordPress没有任何关系,但问题很简单:您仍然在使用$_POST[\'quantity\'] 外部isset().

你在检查isset() 此处:

if(isset($_POST[\'quantity\'])) {
$quantityinstock = $_POST[\'quantity\'];
}
但不管怎么说,您都可以在没有支票的情况下立即使用它:

$quantityinstock = $dbConn->escape_string($_POST[\'quantity\']);
您不应该使用$_POST[\'quantity\'] 无需检查isset().