最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - if isset not working for undefined index, how to fix

programmeradmin1浏览0评论

this is my code though i am not sure what i am missing to remove the error:

<!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>

this is my code though i am not sure what i am missing to remove the error:

<!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>
Share Improve this question edited May 14, 2020 at 5:41 Arushi Rajput 456 bronze badges asked May 14, 2020 at 4:14 rogerroger 1
Add a comment  | 

1 Answer 1

Reset to default 1

This doesn't seem to have anything to do with WordPress, but the problem is simple: You're still using $_POST['quantity'] outside of isset().

You're checking isset() here:

if(isset($_POST['quantity'])) {
$quantityinstock = $_POST['quantity'];
}

But then you're immediately using it without the check anyway:

$quantityinstock = $dbConn->escape_string($_POST['quantity']);

You should not be using $_POST['quantity'] without a check for isset().

发布评论

评论列表(0)

  1. 暂无评论