EDIT_PRODUCT.PHP php // Get the product data $category_id = $_POST['category_id']; $code = $_POST['code']; $name = $_POST['name']; $price = $_POST['price']; $product_id = $_POST['productID'];

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

My update button does not work, when i press edit to update data. i get an unidentified index. Plz help!

 

EDIT_PRODUCT.PHP

 

<?php

// Get the product data

$category_id = $_POST['category_id'];

$code = $_POST['code'];

$name = $_POST['name'];

$price = $_POST['price'];

$product_id = $_POST['productID'];




// Validate inputs

if (empty($code) || empty($name) || empty($price)) {

$error = "Invalid product data. Check all fields are try again.";

include('error.php');

} else {

// If valid, add the product to the database

require_once('database.php');

$query = "UPDATE products

SET categoryID = $category_id,

productCode = $code,

productName = $name,

listPrice = $price

WHERE productID = $product_id";

$db->exec($query);

 

// Display the Product List page

include('index.php');

}

EDIT_PRODUCT_FORM.PHP

 

<?php

 

// get post values

$product_id = $_POST['product_id'];

 

// This is the drop down menu next to 'Category:'

require('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>

 

<!-- the head section -->

 

<head>

<title>My Guitar Shop</title>

<link rel="stylesheet" href="main.css">

</head>

 

<!-- the body section -->

 

<body>

<header>

<h1>Product Manager</h1>

</header>

 

<main>

<h1>Edit Product</h1>

category id: <?php echo $category_id; ?> <br>




<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 type="input" name="code" value="<?php echo $code; ?>"><br>

 

<label>Name:</label>

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

 

<label>List Price:</label>

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

 

<label>&nbsp;</label>

<input type="submit" value="Update Product"><br>

</form>

<p><a href="index.php">View Product List</a></p>

</main>

 

<footer>

<p>&copy; <?php echo date("Y"); ?> My Guitar Shop, Inc.</p>

</footer>

</body>

 

</html>

Expert Solution
Step 1

When working with PHP, you  come across two methods: $_POST and $_GET. These methods are used to get values ​​from the user through the form. Using them may result in a "Notice: Undefined Index" error. This error means that  your code has a variable or constant that has not been assigned a value. However, there are times when you want to use  values ​​obtained through a user form in your PHP code. This error can be avoided by using the isset() function. This function checks if the index variable has been assigned a value  before using it

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Table
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education