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

时间:2018-12-05 02:31:49

标签: php mysql

我刚刚开始学习php,这是我的第一个项目,该项目是创建一个包含各种表的数据库。我在这里遇到的问题是,随着页面打印出来,我似乎无法编辑现有产品

注意:未定义的变量:第15行的C:\ xampp \ htdocs \ goodsdept_manager \ edit_product.php中的product_id

这是我的代码:

edit_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');
}

 ?>

edit_product_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方面的专家可以为我提供帮助。抱歉,如果我犯了愚蠢的错误,因为我仍在学习并且一直在寻求改进。谢谢!

2 个答案:

答案 0 :(得分:0)

根据您的错误消息,$product_id语句的范围内未定义$query。因此,在第15行准备查询时未定义。

要解决此问题,只需将注释的// $product_id = $_POST['productID'];替换为$product_id = "";即可对其进行初始化。然后它将被if语句之后的POST请求中的值替换。否则,默认情况下它将是一个空字符串。因此,即使POST请求中的product_id字段无效,也始终将$ product_id的值定义为空字符串。

另一种选择是将$product_id初始化为NULL($product_id = NULL;

答案 1 :(得分:0)

仅当您将$product_id设置为edit_product.php变量时,才在POST上设置if(isset($_POST['productID'])){ $product_id = $_POST['productID']; }

name

...但是您没有将productID的{​​{1}}格式传递给此页面。

您通过在edit_product_form.php中传递name中的productID来正确地将产品ID传递给index.php,尽管您似乎没有通过新的{{1 }}在$product_idedit_product_form.php中声明。 要更正此问题,您需要添加:

edit_product.php

转到<input type="hidden" name="productID" value="<?php echo $product_id; ?>"> 上的表单:

edit_product_form.php