0

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!

rwd
  • 149
  • 1
  • 11

2 Answers2

0

Based on your error message, $product_id is not defined in the scope of your $query statement. Therefore, it is undefined when the query is being prepared in line 15.

To fix that, simply replace the commented // $product_id = $_POST['productID']; with $product_id = ""; to initialize it. Then it will be replaced with the value from the POST request after the if statement. Otherwise, it will be an empty string by default. Hence, the value of $product_id is always defined to an empty string, even though the product_id field in the POST request is invalid.

Another option is to initialized $product_id with NULL ($product_id = NULL;)

Andreas
  • 2,455
  • 10
  • 21
  • 24
0

You're only setting $product_id on edit_product.php if you receiving it as a POST variable:

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

...but you're not passing a form name of productID to this page.

You correctly pass ithe product ID through to edit_product_form.php by passing a name of productID in index.php, though you don't appear pass through the new $product_id declared in edit_product_form.php to edit_product.php. To correct this, you'll want to add:

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

To your form on 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>
Obsidian Age
  • 41,205
  • 10
  • 48
  • 71
  • Alright now the error message is gone, but somehow I cant see a change in the price of the product when I edit it – rwd Dec 05 '18 at 02:49
  • Nevermind silly me.. Thanks alot for the help!! – rwd Dec 05 '18 at 02:57