/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] PHP Confirm on Delete Confusion

[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][/SIZE]

[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”);
}[/CODE]

[SIZE=3][FONT=Arial]From product_list.php[/FONT][/SIZE]

[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>[/CODE]

[SIZE=3][FONT=Arial]Next is attempted code:
This is a sample of what worked for the successful student.
From index.php[/FONT][/SIZE]

[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”);
}[/CODE]

[SIZE=3][FONT=Arial]From product_list.php[/FONT][/SIZE]

[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>[/CODE]

[SIZE=3][FONT=Arial]From confirm.php[/FONT][/SIZE]

[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>[/CODE]

[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][/SIZE]

to post a comment
PHP

37 Comments(s)

Copy linkTweet thisAlerts:
@KandiLyonsauthorNov 23.2013 — [SIZE=3][FONT=Arial]Real quick! The attempted code does redirect to the confirm.php. I also want to mention some errors I've had other than fetch() . . .

It doesn't redirect back to the product_list.php, it redirects to the folder with no page, doesn't even list from index.php, this error is with this code:[/FONT][/SIZE]


[CODE]// Display the Product List page for the current category
header("Location: .?category_id=$category_id");
[/CODE]


[SIZE=3][FONT=Arial]Of course, the confusion is . . "Why is it suddenly broken?" . . . It can't call the category id.



Other errors include . . . not leaving the confirm.php page and not deleting. Any other errors I can't remember at the moment.



Thanks again.[/FONT][/SIZE]
Copy linkTweet thisAlerts:
@bionoidNov 23.2013 — 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:

header("Location: product_list.php?category_id=$category_id");
Copy linkTweet thisAlerts:
@KandiLyonsauthorNov 23.2013 — [SIZE=3][FONT=Arial]You would like the complete index.php page? . . . or just the code that's involved? The code that's involved is above.



I'll wait for your response before posting the entire page, but [/FONT][/SIZE]


[CODE]header("Location: .?category_id=$category_id");[/CODE]

[SIZE=3][FONT=Arial]is the universal code used throughout the entire sample of files we had to work with and works everywhere BUT the confirm.php page.



I'll try your suggestion and let you know what I get. Thank you.[/FONT][/SIZE]
Copy linkTweet thisAlerts:
@KandiLyonsauthorNov 23.2013 — [SIZE=3][FONT=Arial]Your suggestion provided the following two errors in product_list.php[/FONT][/SIZE]

[CODE]<?php foreach ($categories as $category) : ?>[/CODE]

[SIZE=3][FONT=Arial]and[/FONT][/SIZE]

[CODE]<?php foreach ($products as $product) : ?>[/CODE]

[SIZE=3][FONT=Arial]Both of these are the beginning of my foreach loops, basically it can't run/find the $category(s) or $products(s)[/FONT][/SIZE]

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:

header("Location: product_list.php?category_id=$category_id");[/QUOTE]


[SIZE=3][FONT=Arial]Do you have any other suggestions?[/FONT][/SIZE]
Copy linkTweet thisAlerts:
@bionoidNov 23.2013 — ?

Well, I didn't know you could actually redirect to "." current location ?

I just added a couple things to your index and it seemed to work for me, clearly you have a lot more products to deal with.

[b]index.php[/b]
[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]


The reason why the complete "index.php" would be more insight into the problem.
Copy linkTweet thisAlerts:
@KandiLyonsauthorNov 23.2013 — [SIZE=3][FONT=Arial]OMG! It works for you? What the heck!?! Okay, let me look at this for a few minutes and see if I can spot something. I'll be back to respond.[/FONT][/SIZE]
Copy linkTweet thisAlerts:
@bionoidNov 23.2013 — "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.
Copy linkTweet thisAlerts:
@KandiLyonsauthorNov 23.2013 — [SIZE=3][FONT=Arial]Here is my function list for product_db.php[/FONT][/SIZE]

[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]
Copy linkTweet thisAlerts:
@KandiLyonsauthorNov 23.2013 — "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]


[SIZE=3][FONT=Arial]I can't get to the link you posted. =( [/FONT][/SIZE]
Copy linkTweet thisAlerts:
@KandiLyonsauthorNov 23.2013 — [SIZE=3][FONT=Arial]For whatever reason this only "breaks" when I try to make the confirm page. Otherwise, everything works just fine. I honestly don't see why creating a new page in the same folder as the other pages, that operate as should, would cause the problem.[/FONT][/SIZE]
Copy linkTweet thisAlerts:
@bionoidNov 23.2013 — The link is not public, I only included it to show that the redirection had worked and now I'm looking at the deleted products category.
Copy linkTweet thisAlerts:
@KandiLyonsauthorNov 23.2013 — [SIZE=3][FONT=Arial]Oh. Okay, well, I understood it when I read it. I'm going to give you the page as it currently works. Poke around if you want . . . adding/deleting, etc. There will be no confirmation, but at least you can see what is going on and maybe come up with some ideas. I don't know if I should post it here, so I'm just going to send you a private message with the link, but please post your response here so that others can benefit if we find a solution.[/FONT][/SIZE]
Copy linkTweet thisAlerts:
@bionoidNov 23.2013 — Well I followed the request, the data the form is sending is correct: [FONT=Courier New]action=delete_product&product_id=7&category_id=2[/FONT]

And it did the redirection perfectly afterwards.

For some reason the "YES" block is being defaulted in your code.

As I cannot see any problems through your site I would like to ask for the entire index.php file please.
Copy linkTweet thisAlerts:
@KandiLyonsauthorNov 23.2013 — [SIZE=3][FONT=Arial]Okay, here it is (including all notes).[/FONT][/SIZE]

[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]
Copy linkTweet thisAlerts:
@KandiLyonsauthorNov 23.2013 — [SIZE=3][FONT=Arial]Let me know if you want any other files.[/FONT][/SIZE]
Copy linkTweet thisAlerts:
@bionoidNov 23.2013 — As you know, as it stands that file doesn't include the confirmation page, so I've made that change. I've commented out all the unused variables and includes. Look for //bionoid where I've made changes:

With the $product variable, I'm guessing you normally have a [FONT=Courier New]foreach($products as $product)[/FONT] that should be in there??

[B]index.php[/B]
[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]


Also the confirm page isn't able to see the product information when it's being included, but it can see the ones you created from the POST information:

[B]confirm.php[/B]
[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]
Copy linkTweet thisAlerts:
@KandiLyonsauthorNov 23.2013 — [SIZE=3][FONT=Arial]My foreach loops are in the product_list.php . . . here is the whole page (including all notes)[/FONT][/SIZE]

[code=php]<?php include '../view/header.php'; ?>
<div id="main">
<h1>Product List</h1>

<div id="sidebar">
<!-- display a list of categories -->
<p>&nbsp;</p>
<h2>Categories</h2>
<p>&nbsp;</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>&nbsp;</p>
<h2><?php echo $category_name; ?></h2>
<p>&nbsp;</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>&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>
<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>&nbsp;</p>
<p><a href="?action=show_add_form">Add Product</a></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
</div>

</div>
<?php include '../view/footer.php'; ?>[/code]


[SIZE=3][FONT=Arial]I'll take a look at what you have posted and get back to you.[/SIZE][/FONT]
Copy linkTweet thisAlerts:
@bionoidNov 23.2013 — In light of the full [FONT=Courier New]product_list.php[/FONT] file:

[B]index.php[/B]
[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]


[B]confirm.php[/B]
[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]
Copy linkTweet thisAlerts:
@KandiLyonsauthorNov 23.2013 — [SIZE=3][FONT=Arial]I've tried this before and received an error (along with the same confirm.php that you have provided)[/FONT][/SIZE]


[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]


[SIZE=3][FONT=Arial]I went ahead and copy/pasted your code exactly and uploaded it.[/FONT][/SIZE]

[SIZE=3][FONT=Arial]This is what I get : Fatal error: Cannot redeclare delete_product() in . . . /product_list.php on line 8[/FONT][/SIZE]

[SIZE=3][FONT=Arial]I put both [/FONT][/SIZE]


[code=php]function delete_product() {} //bionoid[/code]

[SIZE=3][FONT=Arial]and [/FONT][/SIZE]

[code=php]function delete_product($product_id) {
global $db;
$query = "DELETE FROM products
WHERE productID = '$product_id'";
$db->exec($query);
}
[/code]


[SIZE=3][FONT=Arial]on that line and received the same results. I might be missing something, or my server doesn't like me. [/FONT][/SIZE]
Copy linkTweet thisAlerts:
@KandiLyonsauthorNov 23.2013 — [SIZE=3][FONT=Arial]Reading your new post, will be back.[/FONT][/SIZE]
Copy linkTweet thisAlerts:
@bionoidNov 23.2013 — If you upload "as-is", then comment out all my dummy functions and re-include your php files (they are only to cater for a lacking database):

[B]index.php[/B]
[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]


[B]confirm.php[/B]
[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]
Copy linkTweet thisAlerts:
@KandiLyonsauthorNov 23.2013 — [SIZE=3][FONT=Arial]I uploaded your new code, I get the same error. I saw your 'Dummy Function' comment. I guess my brain is tired =) . . . I've been over this most every day for a week at several hours a day. I've looked endlessly and just can't seem to find an answer, constantly redefining my web queries. =( Please excuse me if I seem incapable at the moment.[/FONT][/SIZE]
Copy linkTweet thisAlerts:
@KandiLyonsauthorNov 23.2013 — [SIZE=3][FONT=Arial]I want to mention that you've been very helpful dedicating your time and it is most appreciated.[/FONT][/SIZE]
Copy linkTweet thisAlerts:
@bionoidNov 23.2013 — 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???
Copy linkTweet thisAlerts:
@KandiLyonsauthorNov 23.2013 — [SIZE=3][FONT=Arial]I did upload it and then after receiving the error I replaced it with the working pages. I will upload it again, this time I'll leave it up for a few minutes if you want to see the error. Give me two minutes.[/FONT][/SIZE]


[SIZE=3][FONT=Arial]Edit: Done. Go ahead and look.[/FONT][/SIZE]
Copy linkTweet thisAlerts:
@bionoidNov 23.2013 — I see it

Fatal error: Cannot redeclare delete_product() in /home/kandilyo/public_html/kandilyons.com/cit252project/guitar/product_manager/product_list.php on line 8[/QUOTE]

As mentioned before, your product_list.php file doesn't declare that function. -BUT- the modified code I gave you with the dummy functions active does have it on line 8.

The code I supplied should be used in the index.php file, under the product_manager directory. The product_list.php file should not be touched.

I've even reproduced the error you're getting my copying the code into the wrong file ?
Copy linkTweet thisAlerts:
@KandiLyonsauthorNov 23.2013 — [SIZE=3][FONT=Arial]Oh! I just re-read what you said, no, I haven't uploaded anything to index.php. It just occurred to me what I did. Going to go fix that now. Thanks for pointing that out. Told you I was tired. =/[/FONT][/SIZE]

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]
Copy linkTweet thisAlerts:
@KandiLyonsauthorNov 23.2013 — [SIZE=3][FONT=Arial]AH! So much better! No errors! LOL! Helps to put the code in the correct file. Okay, well, it redirects, it's not deleting, but I'm looking again at what you wrote. At least it goes back to the original page. Thanks.. I'll be back.[/FONT][/SIZE]
Copy linkTweet thisAlerts:
@bionoidNov 23.2013 — Yes, please use:

[B]index.php[/B]
[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]
Copy linkTweet thisAlerts:
@KandiLyonsauthorNov 23.2013 — [SIZE=3][FONT=Arial]With your last post . . . I get the same fetch() issue I started with . . . Line 15. Here is the page with the code . . . this is category_db.php[/FONT][/SIZE]

[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]


[SIZE=3][FONT=Arial]

Specifically: [/FONT][/SIZE]


[code=php] $category = $category->fetch();[/code]


[SIZE=3][FONT=Arial]

I left the page up if you wish to see it. [/FONT][/SIZE]
Copy linkTweet thisAlerts:
@bionoidNov 23.2013 — haha, Oops yes ?

On line 138 in [FONT=Courier New]index.php[/FONT]. It should look like this (uncommented):

[code=php]$categories = get_categories();[/code]

Sorry, that was my fault.
Copy linkTweet thisAlerts:
@KandiLyonsauthorNov 23.2013 — [SIZE=3][FONT=Arial]OMG!!!! You are a [COLOR="#008000"]genius[/COLOR]! I'll be staring at this code for some time to see what is different between you and me. =D[/FONT][/SIZE]
Copy linkTweet thisAlerts:
@KandiLyonsauthorNov 23.2013 — [SIZE=3][FONT=Arial]You have NO IDEA how incredibly happy I am right now. I can stop pounding my head![/FONT][/SIZE]
Copy linkTweet thisAlerts:
@KandiLyonsauthorNov 23.2013 — [SIZE=3][FONT=Arial]Now, I have to create a NO button. I hope it's doesn't kill me.[/FONT][/SIZE]
Copy linkTweet thisAlerts:
@bionoidNov 23.2013 — Well, I'll leave that up to you if you don't mind. I've stayed up waaaay past my bedtime (no I'm not 10 years old, just a different timezone) ?

I'll check things out tomorrow if you need help again.

Good luck.
Copy linkTweet thisAlerts:
@KandiLyonsauthorNov 23.2013 — [SIZE=3][FONT=Arial]Of course. I see your country label. I'm sure by now you are very tired. Thank you SO MUCH for all your help. It's been a pleasure.[/FONT][/SIZE]
Copy linkTweet thisAlerts:
@KandiLyonsauthorNov 24.2013 — [SIZE=3][FONT=Arial]For those people who want to know exactly what was the code issue. . . it was the confirm.php page.



The original:[/FONT][/SIZE]


[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]


[SIZE=3][FONT=Arial]The 'fixed' version:[/FONT][/SIZE]

[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]


[SIZE=3][FONT=Arial]Specifically what was changed:



This[/FONT][/SIZE]


[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]


[SIZE=3][FONT=Arial]was changed to[/FONT][/SIZE]

[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]


[SIZE=3][FONT=Arial]Notice that the [ ] and ' ' were both removed. These were acceptable on the product_list.php page where the code required no confirmation, but they were NOT acceptable when redirected to the confirm.php page.[/FONT][/SIZE]

[SIZE=3][FONT=Arial]Thanks again, bionoid.[/FONT][/SIZE]
×

Success!

Help @KandiLyons spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...