[SIZE=3]I[FONT=Arial]’ve read some threads already on this subject and it appears that some clarification is still needed.
Before I continue I first want to say, “Yes, this was part of a school assignment, No, I’m not cheating, I’ve already turned it in and I’m awaiting a grade.”
I just want clarification to what I don’t understand. When I asked my instructor she redirected me to a student who was successful, even though I’ve tried to repeat his
steps, the code doesn’t work for me. The knowledge I expect my instructor to possess doesn’t appear to be within reach and I have no other contact with the successful student,
so, I came here.
First, let me say, a box or a redirection to another page is fine, I was actually attempting a redirection, the successful student also did a redirection.
I will also be providing several codes, samples of working code, sample attempts of non-working code.
Second, I don’t want any JS code, this code should work in PHP only.
I also want to give a shout out to ginerjim and priyankagound for their successful JS Script posts. I’ve tried both and they both function beautifully.
There are three pages involved, index.php, product_list.php, and confirm.php
The code below works, it’ll delete from the database, it does not provide any confirmation of the delete.
From index.php[/FONT]
[CODE]include(‘product_list.php’);
} else if ($action == ‘delete_product’) {
// Get the IDs
$product_id = $_POST[‘product_id’];
$category_id = $_POST[‘category_id’];
// Delete the product
delete_product($product_id);
// Display the Product List page for the current category
header(“Location: .?category_id=$category_id”);
}
[SIZE=3][FONT=Arial]From product_list.php[/FONT]
[CODE]<form action=”.” method=”post”>
<input type=”hidden” name=”action”
value=”delete_product” />
<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>
[SIZE=3][FONT=Arial]Next is attempted code:
This is a sample of what worked for the successful student.
From index.php[/FONT]
[CODE]include(‘product_list.php’);
} else if ($action == ‘delete_product’) {
// Get the IDs
$product_id = $_POST[‘product_id’];
$category_id = $_POST[‘category_id’];
include(‘confirm.php’);
} else if ($action == ‘YES’) {
// Get the IDs
$product_id = $_POST[‘product_id’];
$category_id = $_POST[‘category_id’];
// Delete the product
delete_product($product_id);
// Display the Product List page for the current category
header(“Location: .?category_id=$category_id”);
}
[SIZE=3][FONT=Arial]From product_list.php[/FONT]
[CODE]<form action=”.” method=”post”>
<input type=”hidden” name=”action”
value=”delete_product” />
<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>
[SIZE=3][FONT=Arial]From confirm.php[/FONT]
[CODE]<form action=”.” method=”post”>
<input type=”hidden” name=”action”
value=”YES” />
<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=”YES” />
</form>
[SIZE=3][FONT=Arial]As you can see, he didn’t change much. What happens with this particular version I get a fetch() error.
I’ve tried other variations of this same code and received some other coding error or another with no idea as to why I can’t get this to work.
In theory it all looks correct, but apparently, something is amiss.
If anyone can stare at my code and provide suggestions, it would be greatly appreciated.[/FONT]
[CODE]// Display the Product List page for the current category
header("Location: .?category_id=$category_id");
[/CODE]
header("Location: product_list.php?category_id=$category_id");
[CODE]header("Location: .?category_id=$category_id");[/CODE]
[CODE]<?php foreach ($categories as $category) : ?>[/CODE]
[CODE]<?php foreach ($products as $product) : ?>[/CODE]
I don't mind helping, but from what I see "index.php" is incomplete. Could you post the rest of it please.
For the redirect in the meantime, I would probably do:
[/QUOTE]
header("Location: product_list.php?category_id=$category_id");
[code=php]<?php
$product = array('productID' => 1, 'categoryID' => 99);
$action = @$_POST['action'];
function delete_product($product_id){}
include('product_list.php');
if ($action == 'delete_product') {
// Get the IDs
$product_id = $_POST['product_id'];
$category_id = $_POST['category_id'];
include('confirm.php');
} else
if ($action == 'YES') {
// Get the IDs
$product_id = $_POST['product_id'];
$category_id = $_POST['category_id'];
// Delete the product
delete_product($product_id);
// Display the Product List page for the current category
header("Location: .?category_id=$category_id");
}
?>[/code]
[CODE]<?php
function get_products() {
global $db;
$query = 'SELECT * FROM products
ORDER BY productID';
$products = $db->query($query);
return $products;
}
function get_products_by_category($category_id) {
global $db;
$query = "SELECT * FROM products
WHERE products.categoryID = '$category_id'
ORDER BY productID";
$products = $db->query($query);
return $products;
}
function get_product($product_id) {
global $db;
$query = "SELECT * FROM products
WHERE productID = '$product_id'";
$product = $db->query($query);
$product = $product->fetch();
return $product;
}
function delete_product($product_id) {
global $db;
$query = "DELETE FROM products
WHERE productID = '$product_id'";
$db->exec($query);
}
function add_product($category_id, $code, $name, $price) {
global $db;
$query = "INSERT INTO products
(categoryID, productCode, productName, listPrice)
VALUES
('$category_id', '$code', '$name', '$price')";
$db->exec($query);
}
?>[/CODE]
"Works" for me means I got a delete button, once pressed I got a button with "Yes" as well.
Clicking "Yes" runs the [FONT=Courier New]delete_product[/FONT] function and does a redirection to [FONT=Courier New]http://www/?category_id=99 [/FONT], on my webserver.[/QUOTE]
[code=php]<?php
require('../model/database.php');
require('../model/product_db.php');
require('../model/category_db.php');
if (isset($_POST['action'])) {
$action = $_POST['action'];
} else if (isset($_GET['action'])) {
$action = $_GET['action'];
} else {
$action = 'list_products';
}
if ($action == 'list_products') {
//the below commented area is an error
// Get the current category ID
/*$category_id = $_GET['category_id'];
if (!isset($category_id)) {
$category_id = 1;
}*/
//the below is the corrected version of the above
if ( isset($_GET['category_id']) )
{ $category_id = $_GET['category_id']; }
else
{ $category_id = 1; }
// Get product and category data
$category_name = get_category_name($category_id);
$categories = get_categories();
$products = get_products_by_category($category_id);
/*
//below is the code to confirm a delete or deny a delete
// Display the product list
include('product_list.php');
} else if ($action == 'delete_product') {
// Get the IDs
$product_id = $_POST['product_id'];
$category_id = $_POST['category_id'];
include('confirm.php');
} else if ($action == 'YES') {
// Get the IDs
$product_id = $_POST['product_id'];
$category_id = $_POST['category_id'];
// Delete the product
delete_product($product_id);
// Display the Product List page for the current category
header("Location: .?category_id=$category_id");
} else if ($action == 'NO') {
// Get the IDs
$product_id = $_POST['product_id'];
$category_id = $_POST['category_id'];
// Display the Product List page for the current category
header("Location: .?category_id=$category_id");
*/
/*
// Display the product list
include('product_list.php');
} else if ($action == 'delete_product') {
// Get the IDs
$product_id = $_POST['product_id'];
$category_id = $_POST['category_id'];
include('confirm.php');
} else if ($action == 'YES') {
// Get the IDs
$product_id = $_POST['product_id'];
$category_id = $_POST['category_id'];
// Delete the product
delete_product($product_id);
// Display the Product List page for the current category
header("Location: .?category_id=$category_id");
*/
//Display the Product List page for the current category
//header("Location: product_list.php?category_id=$category_id");
// Display the product list
include('product_list.php');
} else if ($action == 'delete_product') {
// Get the IDs
$product_id = $_POST['product_id'];
$category_id = $_POST['category_id'];
// Delete the product
delete_product($product_id);
// Display the Product List page for the current category
header("Location: .?category_id=$category_id");
//------------
} else if ($action == 'show_add_form') {
$categories = get_categories();
include('product_add.php');
} else if ($action == 'add_product') {
$category_id = $_POST['category_id'];
$code = $_POST['code'];
$name = $_POST['name'];
$price = $_POST['price'];
// Validate the inputs
if (empty($code)) {
$error = "The code field cannot be blank.";
include('../errors/error.php');
} else if (empty($name)) {
$error = "The name field cannot be blank.";
include('../errors/error.php');
} else if (empty($price)) {
$error = "The price field cannot be blank or be a zero.";
include('../errors/error.php');
} else if (!is_numeric($price)) {
$error = 'The price field must be a valid number.';
include('../errors/error.php');
} else if ($price <= 0) {
$error = 'The price field must be greater than zero.';
include('../errors/error.php');
} else {
add_product($category_id, $code, $name, $price);
// Display the Product List page for the current category
header("Location: .?category_id=$category_id");
}
}
?>[/code]
[code=php]<?php
//require('../model/database.php');
//require('../model/product_db.php');
//require('../model/category_db.php');
function delete_product() {} //bionoid
if (isset($_POST['action'])) {
$action = $_POST['action'];
} else if (isset($_GET['action'])) {
$action = $_GET['action'];
} else {
$action = 'list_products';
}
if ($action == 'list_products') {
//the below commented area is an error
// Get the current category ID
/*$category_id = $_GET['category_id'];
if (!isset($category_id)) {
$category_id = 1;
}*/
//the below is the corrected version of the above
if ( isset($_GET['category_id']) )
{ $category_id = $_GET['category_id']; }
else
{ $category_id = 1; }
// Get product and category data
// $category_name = get_category_name($category_id);
// $categories = get_categories();
// $products = get_products_by_category($category_id);
$product = array('productID' => 1, 'categoryID' => 99); //bionoid
/*
//below is the code to confirm a delete or deny a delete
// Display the product list
include('product_list.php');
} else if ($action == 'delete_product') {
// Get the IDs
$product_id = $_POST['product_id'];
$category_id = $_POST['category_id'];
include('confirm.php');
} else if ($action == 'YES') {
// Get the IDs
$product_id = $_POST['product_id'];
$category_id = $_POST['category_id'];
// Delete the product
delete_product($product_id);
// Display the Product List page for the current category
header("Location: .?category_id=$category_id");
} else if ($action == 'NO') {
// Get the IDs
$product_id = $_POST['product_id'];
$category_id = $_POST['category_id'];
// Display the Product List page for the current category
header("Location: .?category_id=$category_id");
*/
/*
// Display the product list
include('product_list.php');
} else if ($action == 'delete_product') {
// Get the IDs
$product_id = $_POST['product_id'];
$category_id = $_POST['category_id'];
include('confirm.php');
} else if ($action == 'YES') {
// Get the IDs
$product_id = $_POST['product_id'];
$category_id = $_POST['category_id'];
// Delete the product
delete_product($product_id);
// Display the Product List page for the current category
header("Location: .?category_id=$category_id");
*/
//Display the Product List page for the current category
//header("Location: product_list.php?category_id=$category_id");
// Display the product list
include('product_list.php');
} else if ($action == 'delete_product') {
// Get the IDs
$product_id = $_POST['product_id'];
$category_id = $_POST['category_id'];
include('confirm.php'); //bionoid
// Delete the product
//delete_product($product_id);
// Display the Product List page for the current category
//header("Location: .?category_id=$category_id");
//------------
//bionoid
} else if ($action == 'YES') {
// Get the IDs
$product_id = $_POST['product_id'];
$category_id = $_POST['category_id'];
// Delete the product
delete_product($product_id);
// Display the Product List page for the current category
header("Location: .?category_id=$category_id");
} else if ($action == 'show_add_form') {
// $categories = get_categories();
include('product_add.php');
} else if ($action == 'add_product') {
$category_id = $_POST['category_id'];
$code = $_POST['code'];
$name = $_POST['name'];
$price = $_POST['price'];
// Validate the inputs
if (empty($code)) {
$error = "The code field cannot be blank.";
include('../errors/error.php');
} else if (empty($name)) {
$error = "The name field cannot be blank.";
include('../errors/error.php');
} else if (empty($price)) {
$error = "The price field cannot be blank or be a zero.";
include('../errors/error.php');
} else if (!is_numeric($price)) {
$error = 'The price field must be a valid number.';
include('../errors/error.php');
} else if ($price <= 0) {
$error = 'The price field must be greater than zero.';
include('../errors/error.php');
} else {
add_product($category_id, $code, $name, $price);
// Display the Product List page for the current category
header("Location: .?category_id=$category_id");
}
}
?>[/code]
[code=php]<form action="." method="post">
<input type="hidden" name="action" value="YES" />
<input type="hidden" name="product_id" value="<?php echo $product_id; ?>" />
<input type="hidden" name="category_id" value="<?php echo $category_id; ?>" />
<input type="submit" value="YES" />
</form>[/code]
[code=php]<?php include '../view/header.php'; ?>
<div id="main">
<h1>Product List</h1>
<div id="sidebar">
<!-- display a list of categories -->
<p> </p>
<h2>Categories</h2>
<p> </p>
<ul>
<?php foreach ($categories as $category) : ?>
<li>
<p class="category"><a href="?category_id=<?php echo $category['categoryID']; ?>">
<?php echo $category['categoryName']; ?>
</a></p>
</li>
<?php endforeach; ?>
</ul>
</div>
<div id="content">
<!-- display a table of products -->
<p> </p>
<h2><?php echo $category_name; ?></h2>
<p> </p>
<p class="category2">
<!--
<script type='text/javascript'>
function confirmDelete()
{
return confirm("Are you sure you want to delete this?");
}
</script> THIS SCRIPT FUNCTIONS!!! WITH THIS CODE IN WITH THE SUBMIT BUTTON onclick='return confirmDelete()' // -->
<!--
<script>
function deleletconfig(){
var del=confirm("Are you sure you want to delete this record?");
if (del==true){
alert ("Record Deleted")
}else{
alert("Record Not Deleted")
}
return del;
}
</script> THIS SCRIPT FUNCTIONS!!! WITH THIS CODE IN WITH THE SUBMIT BUTTON onclick="return deleletconfig()" // -->
<table>
<tr>
<th>Code</th>
<th>Name</th>
<th class="right">Price</th>
<th> </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>
<td>
<form action="." method="post">
<input type="hidden" name="action"
value="delete_product" />
<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>
</tr>
<?php endforeach; ?>
</table>
</p>
<p> </p>
<p><a href="?action=show_add_form">Add Product</a></p>
<p> </p>
<p> </p>
</div>
</div>
<?php include '../view/footer.php'; ?>[/code]
[code=php]<?php
//require('../model/database.php');
//require('../model/product_db.php');
//require('../model/category_db.php');
//bionoid : MY DUMMY FUNCTIONS
function delete_product() {}
function get_category_name($id)
{
return 'CATEGORY_NAME_' . $id;
}
function get_categories()
{
return array(
array('categoryName' => 'Guitars', 'categoryID' => 2)
);
}
function get_products_by_category()
{
return array(
array('productCode' => '123', 'productName' => 'Product 1', 'listPrice' => '$1', 'productID' => '99', 'categoryID' => 2),
array('productCode' => '456', 'productName' => 'Product 2', 'listPrice' => '$2', 'productID' => '45', 'categoryID' => 6)
);
}
if (isset($_POST['action'])) {
$action = $_POST['action'];
} else if (isset($_GET['action'])) {
$action = $_GET['action'];
} else {
$action = 'list_products';
}
if ($action == 'list_products') {
//the below commented area is an error
// Get the current category ID
/*$category_id = $_GET['category_id'];
if (!isset($category_id)) {
$category_id = 1;
}*/
//the below is the corrected version of the above
if ( isset($_GET['category_id']) )
{ $category_id = $_GET['category_id']; }
else
{ $category_id = 1; }
// Get product and category data
$category_name = get_category_name($category_id);
$categories = get_categories();
$products = get_products_by_category($category_id);
/*
//below is the code to confirm a delete or deny a delete
// Display the product list
include('product_list.php');
} else if ($action == 'delete_product') {
// Get the IDs
$product_id = $_POST['product_id'];
$category_id = $_POST['category_id'];
include('confirm.php');
} else if ($action == 'YES') {
// Get the IDs
$product_id = $_POST['product_id'];
$category_id = $_POST['category_id'];
// Delete the product
delete_product($product_id);
// Display the Product List page for the current category
header("Location: .?category_id=$category_id");
} else if ($action == 'NO') {
// Get the IDs
$product_id = $_POST['product_id'];
$category_id = $_POST['category_id'];
// Display the Product List page for the current category
header("Location: .?category_id=$category_id");
*/
/*
// Display the product list
include('product_list.php');
} else if ($action == 'delete_product') {
// Get the IDs
$product_id = $_POST['product_id'];
$category_id = $_POST['category_id'];
include('confirm.php');
} else if ($action == 'YES') {
// Get the IDs
$product_id = $_POST['product_id'];
$category_id = $_POST['category_id'];
// Delete the product
delete_product($product_id);
// Display the Product List page for the current category
header("Location: .?category_id=$category_id");
*/
//Display the Product List page for the current category
//header("Location: product_list.php?category_id=$category_id");
// Display the product list
include('product_list.php');
} else if ($action == 'delete_product') {
// Get the IDs
$product_id = $_POST['product_id'];
$category_id = $_POST['category_id'];
include('confirm.php'); //bionoid
// Delete the product
//delete_product($product_id);
// Display the Product List page for the current category
//header("Location: .?category_id=$category_id");
//------------
//bionoid
} else if ($action == 'YES') {
// Get the IDs
$product_id = $_POST['product_id'];
$category_id = $_POST['category_id'];
// Delete the product
delete_product($product_id);
// Display the Product List page for the current category
header("Location: .?category_id=$category_id");
} else if ($action == 'show_add_form') {
// $categories = get_categories();
include('product_add.php');
} else if ($action == 'add_product') {
$category_id = $_POST['category_id'];
$code = $_POST['code'];
$name = $_POST['name'];
$price = $_POST['price'];
// Validate the inputs
if (empty($code)) {
$error = "The code field cannot be blank.";
include('../errors/error.php');
} else if (empty($name)) {
$error = "The name field cannot be blank.";
include('../errors/error.php');
} else if (empty($price)) {
$error = "The price field cannot be blank or be a zero.";
include('../errors/error.php');
} else if (!is_numeric($price)) {
$error = 'The price field must be a valid number.';
include('../errors/error.php');
} else if ($price <= 0) {
$error = 'The price field must be greater than zero.';
include('../errors/error.php');
} else {
add_product($category_id, $code, $name, $price);
// Display the Product List page for the current category
header("Location: .?category_id=$category_id");
}
}
?>[/code]
[code=php]<form action="." method="post">
<input type="hidden" name="action" value="YES" />
<input type="hidden" name="product_id" value="<?php echo $product_id; ?>" />
<input type="hidden" name="category_id" value="<?php echo $category_id; ?>" />
<input type="submit" value="YES" />
</form>[/code]
[code=php] // Display the product list
include('product_list.php');
} else if ($action == 'delete_product') {
// Get the IDs
$product_id = $_POST['product_id'];
$category_id = $_POST['category_id'];
include('confirm.php'); //bionoid
//------------
//bionoid
} else if ($action == 'YES') {
// Get the IDs
$product_id = $_POST['product_id'];
$category_id = $_POST['category_id'];
// Delete the product
delete_product($product_id);
// Display the Product List page for the current category
header("Location: .?category_id=$category_id");[/code]
[code=php]function delete_product() {} //bionoid[/code]
[code=php]function delete_product($product_id) {
global $db;
$query = "DELETE FROM products
WHERE productID = '$product_id'";
$db->exec($query);
}
[/code]
[code=php]<?php
require('../model/database.php');
require('../model/product_db.php');
require('../model/category_db.php');
/*
//bionoid : MY DUMMY FUNCTIONS
function delete_product() {}
function get_category_name($id)
{
return 'CATEGORY_NAME_' . $id;
}
function get_categories()
{
return array(
array('categoryName' => 'Guitars', 'categoryID' => 2)
);
}
function get_products_by_category()
{
return array(
array('productCode' => '123', 'productName' => 'Product 1', 'listPrice' => '$1', 'productID' => '99', 'categoryID' => 2),
array('productCode' => '456', 'productName' => 'Product 2', 'listPrice' => '$2', 'productID' => '45', 'categoryID' => 6)
);
}
*/
if (isset($_POST['action'])) {
$action = $_POST['action'];
} else if (isset($_GET['action'])) {
$action = $_GET['action'];
} else {
$action = 'list_products';
}
if ($action == 'list_products') {
//the below commented area is an error
// Get the current category ID
/*$category_id = $_GET['category_id'];
if (!isset($category_id)) {
$category_id = 1;
}*/
//the below is the corrected version of the above
if ( isset($_GET['category_id']) )
{ $category_id = $_GET['category_id']; }
else
{ $category_id = 1; }
// Get product and category data
$category_name = get_category_name($category_id);
$categories = get_categories();
$products = get_products_by_category($category_id);
/*
//below is the code to confirm a delete or deny a delete
// Display the product list
include('product_list.php');
} else if ($action == 'delete_product') {
// Get the IDs
$product_id = $_POST['product_id'];
$category_id = $_POST['category_id'];
include('confirm.php');
} else if ($action == 'YES') {
// Get the IDs
$product_id = $_POST['product_id'];
$category_id = $_POST['category_id'];
// Delete the product
delete_product($product_id);
// Display the Product List page for the current category
header("Location: .?category_id=$category_id");
} else if ($action == 'NO') {
// Get the IDs
$product_id = $_POST['product_id'];
$category_id = $_POST['category_id'];
// Display the Product List page for the current category
header("Location: .?category_id=$category_id");
*/
/*
// Display the product list
include('product_list.php');
} else if ($action == 'delete_product') {
// Get the IDs
$product_id = $_POST['product_id'];
$category_id = $_POST['category_id'];
include('confirm.php');
} else if ($action == 'YES') {
// Get the IDs
$product_id = $_POST['product_id'];
$category_id = $_POST['category_id'];
// Delete the product
delete_product($product_id);
// Display the Product List page for the current category
header("Location: .?category_id=$category_id");
*/
//Display the Product List page for the current category
//header("Location: product_list.php?category_id=$category_id");
// Display the product list
include('product_list.php');
} else if ($action == 'delete_product') {
// Get the IDs
$product_id = $_POST['product_id'];
$category_id = $_POST['category_id'];
include('confirm.php'); //bionoid
// Delete the product
//delete_product($product_id);
// Display the Product List page for the current category
//header("Location: .?category_id=$category_id");
//------------
//bionoid
} else if ($action == 'YES') {
// Get the IDs
$product_id = $_POST['product_id'];
$category_id = $_POST['category_id'];
// Delete the product
delete_product($product_id);
// Display the Product List page for the current category
header("Location: .?category_id=$category_id");
} else if ($action == 'show_add_form') {
// $categories = get_categories();
include('product_add.php');
} else if ($action == 'add_product') {
$category_id = $_POST['category_id'];
$code = $_POST['code'];
$name = $_POST['name'];
$price = $_POST['price'];
// Validate the inputs
if (empty($code)) {
$error = "The code field cannot be blank.";
include('../errors/error.php');
} else if (empty($name)) {
$error = "The name field cannot be blank.";
include('../errors/error.php');
} else if (empty($price)) {
$error = "The price field cannot be blank or be a zero.";
include('../errors/error.php');
} else if (!is_numeric($price)) {
$error = 'The price field must be a valid number.';
include('../errors/error.php');
} else if ($price <= 0) {
$error = 'The price field must be greater than zero.';
include('../errors/error.php');
} else {
add_product($category_id, $code, $name, $price);
// Display the Product List page for the current category
header("Location: .?category_id=$category_id");
}
}
?>[/code]
[code=php]<form action="." method="post">
<input type="hidden" name="action" value="YES" />
<input type="hidden" name="product_id" value="<?php echo $product_id; ?>" />
<input type="hidden" name="category_id" value="<?php echo $category_id; ?>" />
<input type="submit" value="YES" />
</form>[/code]
Fatal error: Cannot redeclare delete_product() in /home/kandilyo/public_html/kandilyons.com/cit252project/guitar/product_manager/product_list.php on line 8[/QUOTE]
Are you sure you've uploaded the code to the [FONT=Courier New]index.php[/FONT] file? the [FONT=Courier New]product_list.php[/FONT] file doesn't even declare that function???[/QUOTE]
[code=php]<?php
require('../model/database.php');
require('../model/product_db.php');
require('../model/category_db.php');
/*
//bionoid : MY DUMMY FUNCTIONS
function delete_product() {}
function get_category_name($id)
{
return 'CATEGORY_NAME_' . $id;
}
function get_categories()
{
return array(
array('categoryName' => 'Guitars', 'categoryID' => 2)
);
}
function get_products_by_category()
{
return array(
array('productCode' => '123', 'productName' => 'Product 1', 'listPrice' => '$1', 'productID' => '99', 'categoryID' => 2),
array('productCode' => '456', 'productName' => 'Product 2', 'listPrice' => '$2', 'productID' => '45', 'categoryID' => 6)
);
}
*/
if (isset($_POST['action'])) {
$action = $_POST['action'];
} else if (isset($_GET['action'])) {
$action = $_GET['action'];
} else {
$action = 'list_products';
}
if ($action == 'list_products') {
//the below commented area is an error
// Get the current category ID
/*$category_id = $_GET['category_id'];
if (!isset($category_id)) {
$category_id = 1;
}*/
//the below is the corrected version of the above
if ( isset($_GET['category_id']) )
{ $category_id = $_GET['category_id']; }
else
{ $category_id = 1; }
// Get product and category data
$category_name = get_category_name($category_id);
$categories = get_categories();
$products = get_products_by_category($category_id);
/*
//below is the code to confirm a delete or deny a delete
// Display the product list
include('product_list.php');
} else if ($action == 'delete_product') {
// Get the IDs
$product_id = $_POST['product_id'];
$category_id = $_POST['category_id'];
include('confirm.php');
} else if ($action == 'YES') {
// Get the IDs
$product_id = $_POST['product_id'];
$category_id = $_POST['category_id'];
// Delete the product
delete_product($product_id);
// Display the Product List page for the current category
header("Location: .?category_id=$category_id");
} else if ($action == 'NO') {
// Get the IDs
$product_id = $_POST['product_id'];
$category_id = $_POST['category_id'];
// Display the Product List page for the current category
header("Location: .?category_id=$category_id");
*/
/*
// Display the product list
include('product_list.php');
} else if ($action == 'delete_product') {
// Get the IDs
$product_id = $_POST['product_id'];
$category_id = $_POST['category_id'];
include('confirm.php');
} else if ($action == 'YES') {
// Get the IDs
$product_id = $_POST['product_id'];
$category_id = $_POST['category_id'];
// Delete the product
delete_product($product_id);
// Display the Product List page for the current category
header("Location: .?category_id=$category_id");
*/
//Display the Product List page for the current category
//header("Location: product_list.php?category_id=$category_id");
// Display the product list
include('product_list.php');
} else if ($action == 'delete_product') {
// Get the IDs
$product_id = $_POST['product_id'];
$category_id = $_POST['category_id'];
include('confirm.php'); //bionoid
// Delete the product
//delete_product($product_id);
// Display the Product List page for the current category
//header("Location: .?category_id=$category_id");
//------------
//bionoid
} else if ($action == 'YES') {
// Get the IDs
$product_id = $_POST['product_id'];
$category_id = $_POST['category_id'];
// Delete the product
delete_product($product_id);
// Display the Product List page for the current category
header("Location: .?category_id=$category_id");
} else if ($action == 'show_add_form') {
// $categories = get_categories();
include('product_add.php');
} else if ($action == 'add_product') {
$category_id = $_POST['category_id'];
$code = $_POST['code'];
$name = $_POST['name'];
$price = $_POST['price'];
// Validate the inputs
if (empty($code)) {
$error = "The code field cannot be blank.";
include('../errors/error.php');
} else if (empty($name)) {
$error = "The name field cannot be blank.";
include('../errors/error.php');
} else if (empty($price)) {
$error = "The price field cannot be blank or be a zero.";
include('../errors/error.php');
} else if (!is_numeric($price)) {
$error = 'The price field must be a valid number.';
include('../errors/error.php');
} else if ($price <= 0) {
$error = 'The price field must be greater than zero.';
include('../errors/error.php');
} else {
add_product($category_id, $code, $name, $price);
// Display the Product List page for the current category
header("Location: .?category_id=$category_id");
}
}
?>[/code]
[code=php]<?php
function get_categories() {
global $db;
$query = 'SELECT * FROM categories
ORDER BY categoryID';
$result = $db->query($query);
return $result;
}
function get_category_name($category_id) {
global $db;
$query = "SELECT * FROM categories
WHERE categoryID = $category_id";
$category = $db->query($query);
$category = $category->fetch();
$category_name = $category['categoryName'];
return $category_name;
}
?>[/code]
[code=php] $category = $category->fetch();[/code]
[code=php]$categories = get_categories();[/code]
[code=php]
<form action="." method="post">
<input type="hidden" name="action"
value="YES" />
<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="YES" />
</form>
[/code]
[code=php]
<form action="." method="post">
<input type="hidden" name="action"
value="YES" />
<input type="hidden" name="product_id"
value="<?php echo $product_id; ?>" />
<input type="hidden" name="category_id"
value="<?php echo $category_id; ?>" />
<input type="submit" value="YES" />
</form>
[/code]
[code=php]
<input type="hidden" name="product_id"
value="<?php echo $product['productID']; ?>" />
<input type="hidden" name="category_id"
value="<?php echo $product['categoryID']; ?>" />
[/code]
[code=php]
<input type="hidden" name="product_id"
value="<?php echo $product_id; ?>" />
<input type="hidden" name="category_id"
value="<?php echo $category_id; ?>" />
[/code]
0.1.9 — BETA 5.19