未定义变量,php代码有问题

rsl1atfo  于 2021-06-15  发布在  Mysql
关注(0)|答案(2)|浏览(347)

这个问题在这里已经有答案了

使用php的“notice:未定义变量”、“notice:未定义索引”和“notice:未定义偏移量”(28个答案)
两年前关门了。
我刚开始学习php,这是我的第一个项目,它是创建一个包含各种表的数据库。我在这里遇到的问题是,当页面打印出来时,我似乎无法编辑现有的产品
注意:未定义的变量:c:\xampp\htdocs\goodsdept\u manager\edit\u product.php第15行中的product\u id
这是我的密码:
编辑\u product.php

<?php

$category_id = $_POST['category_id'];
$code = $_POST['code'];
$name = $_POST['name'];
$price = $_POST['price'];
// $product_id = $_POST['productID'];
if(isset($_POST['productID'])){ $product_id = $_POST['productID']; }

if(empty($code) || empty($name) || empty($price)){
  $error = "Invalid product data.";
  include('error.php');
} else{
  require_once('database.php');
  $query = "UPDATE products SET categoryID = '$category_id', productCode = '$code', productName = '$name', listPrice = '$price' WHERE productID = '$product_id'";
  $statement = $db->prepare($query);
  $statement->execute();
  $statement->closeCursor();

  include('index.php');
}

 ?>

编辑\u product\u form.php

<?php
    $product_id = $_POST['product_id'];

    //Get the categories for the pull down menu
    require_once('database.php');
    $query = "SELECT*FROM categories ORDER BY categoryID";
    $categories = $db->query($query);

    $query = "SELECT*FROM products WHERE productID = $product_id";
    $edit_product = $db->query($query);
    $edit_product = $edit_product->fetch();

    //Define the VALUES
    $code = $edit_product['productCode'];
    $name = $edit_product['productName'];
    $price = $edit_product['listPrice'];
    $category_id = $edit_product['categoryID'];
    ?>

     <!DOCTYPE html>
     <html lang="en">
       <head>
         <meta charset="utf-8">
         <meta http-equiv="X-UA-Compatible" content="IE=edge">
         <meta name="viewport" content="width=device-width, initial-scale=1">
         <title></title>
         <link rel="stylesheet" type="text/css" href="main.css" />
       </head>
       <body>

             <h1>Product Manager</h1>

             <h1>Edit Product</h1>
             Product ID: <?php echo $product_id; ?><br />
             code: <?php echo $code; ?>

             <form action="edit_product.php" method="post"
               id="edit_product_form">

               <label>Category:</label>
               <select name="category_id">
               <?php foreach ($categories as $category) : ?>
                 <option value="<?php echo $category['categoryID']; ?>">
                   <?php echo $category['categoryName']; ?>
                 </option>
               <?php endforeach; ?>
               </select><br>

             <label>Code:</label>
             <input name="code" type="input" value="<?php echo $code; ?>"><br>

             <label>Name:</label>
             <input name="name" type="input" value="<?php echo $name; ?>"><br>

             <label>List Price:</label>
             <input name="price" type="input" value="<?php echo $price; ?>"><br>

             <label>&nbsp;</label>
             <input type="submit" value="Edit Product"/><br>
     </form>

         <footer>
             <p>&copy; <?php echo date("Y"); ?> The Goods Dept, Inc.</p>
         </footer>

       </body>
     </html>

以及index.php

<?php
require_once('database.php'); //calls the database.php file to validate the user

//Get Category ID
if (!isset($category_id)) {
    $category_id = filter_input(INPUT_GET, 'category_id', FILTER_VALIDATE_INT);
    if ($category_id == NULL || $category_id == FALSE) {
        $category_id = 1;
    }
}

//Get name for selected category
$queryCategory = 'SELECT * FROM categories
                  WHERE categoryID = :category_id';
$statement1 = $db->prepare($queryCategory);
$statement1->bindValue(':category_id', $category_id);
$statement1->execute();
$category = $statement1->fetch();
$category_name = $category['categoryName'];
$statement1->closeCursor();

// Get all categories
$query = 'SELECT * FROM categories
                       ORDER BY categoryID';
$statement = $db->prepare($query);
$statement->execute();
$categories = $statement->fetchAll();
$statement->closeCursor();

// Get products for selected category
$queryProducts = 'SELECT * FROM products
                  WHERE categoryID = :category_id
                  ORDER BY productID';
$statement3 = $db->prepare($queryProducts);
$statement3->bindValue(':category_id', $category_id);
$statement3->execute();
$products = $statement3->fetchAll();
$statement3->closeCursor();

 ?>

 <!DOCTYPE html>
<html>

<!-- the head section -->
<head>
    <title>The Goods Dept</title>
    <link rel="stylesheet" type="text/css" href="main.css" />
</head>

<!-- the body section -->
<body>
<header><h1>Product Manager</h1></header>
<main>
    <h1>Product List</h1>

    <aside>
        <!-- display a list of categories -->
        <h2>Categories</h2>
        <nav>
        <ul>
            <?php foreach ($categories as $category) : ?>
            <li><a href=".?category_id=<?php echo $category['categoryID']; ?>">
                    <?php echo $category['categoryName']; ?>
                </a>
            </li>
            <?php endforeach; ?>
        </ul>
        </nav>
    </aside>

    <section>
        <!-- display a table of products -->
        <h2><?php echo $category_name; ?></h2>
        <table>
            <tr>
                <th>Code</th>
                <th>Name</th>
                <th class="right">Price</th>
                <th>&nbsp;</th>
            </tr>

            <?php foreach ($products as $product) : ?>
            <tr>
                <td><?php echo $product['productCode']; ?></td>
                <td><?php echo $product['productName']; ?></td>
                <td class="right"><?php echo $product['listPrice']; ?></td>

                <!-- Delete product -->
                <td><form action="delete_product.php" method="post">
                    <input type="hidden" name="product_id"
                           value="<?php echo $product['productID']; ?>">
                    <input type="hidden" name="category_id"
                           value="<?php echo $product['categoryID']; ?>">
                    <input type="submit" value="Delete">
                </form></td>

                <!-- Update product -->
                <td><form action="edit_product_form.php" method="post" id="edit_product_form">
                    <input type="hidden" name="product_id"
                           value="<?php echo $product['productID']; ?>">
                    <input type="hidden" name="category_id"
                           value="<?php echo $product['categoryID']; ?>">
                    <input type="submit" value="Edit">
                </form></td>

            </tr>
            <?php endforeach; ?>
        </table>

        <p><a href="add_product_form.php">Add Product</a></p>
        <p><a href="category_list.php">List Categories</a></p>
    </section>
</main>
<footer>
    <p>&copy; <?php echo date("Y"); ?> The Goods Dept</p>
</footer>
</body>
</html>

我自己一直在努力解决这个问题,所以我发现stackoverflow的Maven可以帮我。抱歉,如果愚蠢的错误,因为我仍然在学习,并一直期待提高。谢谢!

rta7y2nd

rta7y2nd1#

根据你的错误信息, $product_id 未在您的范围内定义 $query 声明。因此,当在第15行中准备查询时,它是未定义的。
要解决这个问题,只需替换 // $product_id = $_POST['productID'];$product_id = ""; 初始化它。然后它将替换为 if 声明。否则,它将默认为空字符串。因此,$product\ id的值总是定义为空字符串,即使 product_id post请求中的字段无效。
另一个选择是初始化 $product_id 带null( $product_id = NULL; )

8i9zcol2

8i9zcol22#

你只是在设置 $product_idedit_product.php 如果你把它当作 POST 变量:

if(isset($_POST['productID'])){ $product_id = $_POST['productID']; }

…但你不是在传递表格 nameproductID 转到本页。
您正确地将产品id传递给 edit_product_form.php 通过一个 nameproductIDindex.php ,虽然你没有通过新的 $product_id 声明于 edit_product_form.phpedit_product.php . 要更正此问题,您需要添加:

<input type="hidden" name="productID" value="<?php echo $product_id; ?>">

到你的表格上 edit_product_form.php :

<form action="edit_product.php" method="post" id="edit_product_form">
   ...
   <input type="hidden" name="productID" value="<?php echo $product_id; ?>">
</form>

相关问题