2018-12-05 02:31
浏览 70


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:



$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.";
} else{
  $query = "UPDATE products SET categoryID = '$category_id', productCode = '$code', productName = '$name', listPrice = '$price' WHERE productID = '$product_id'";
  $statement = $db->prepare($query);




    $product_id = $_POST['product_id'];

    //Get the categories for the pull down menu
    $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">
         <meta charset="utf-8">
         <meta http-equiv="X-UA-Compatible" content="IE=edge">
         <meta name="viewport" content="width=device-width, initial-scale=1">
         <link rel="stylesheet" type="text/css" href="main.css" />

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

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

             <input name="code" type="input" value="<?php echo $code; ?>"><br>

             <input name="name" type="input" value="<?php echo $name; ?>"><br>

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

             <input type="submit" value="Edit Product"/><br>

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


And the index.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);
$category = $statement1->fetch();
$category_name = $category['categoryName'];

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

// 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);
$products = $statement3->fetchAll();


 <!DOCTYPE html>

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

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

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

        <!-- display a table of products -->
        <h2><?php echo $category_name; ?></h2>
                <th class="right">Price</th>

            <?php foreach ($products as $product) : ?>
                <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">

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

            <?php endforeach; ?>

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

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!

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

2条回答 默认 最新