dougu7546
2018-12-05 02:31
浏览 70
已采纳

未定义的变量,php代码问题[重复]

I just started learning php and this is one my first projects which is to create a database with various tables in it. The problem I encounter here is that I cant seem to edit the existing product as the page prints out

Notice: Undefined variable: product_id in C:\xampp\htdocs\goodsdept_manager\edit_product.php on line 15

Here are my codes:

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>

And the 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>

I've been struggling to solve the problem myself so I figured out the experts in stackoverflow can help me. Sorry if silly mistakes were made as I am still learning and always looking to improve. Thanks!

</div>
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新