www.webdeveloper.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 38

Thread: [RESOLVED] PHP Confirm on Delete Confusion

  1. #1
    Join Date
    May 2007
    Location
    Las Vegas, NV
    Posts
    42

    resolved [RESOLVED] PHP Confirm on Delete Confusion

    I'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


    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");
    }
    From product_list.php

    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>
    Next is attempted code:
    This is a sample of what worked for the successful student.
    From index.php


    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");
    }
    From product_list.php

    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>
    From confirm.php

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

  2. #2
    Join Date
    May 2007
    Location
    Las Vegas, NV
    Posts
    42
    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:


    Code:
    // Display the Product List page for the current category
    header("Location: .?category_id=$category_id");
    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.

  3. #3
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    782
    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:

    Code:
    header("Location: product_list.php?category_id=$category_id");
    Last edited by bionoid; 11-23-2013 at 02:17 PM.
    JavaScript: Learn | Validate | Compact

  4. #4
    Join Date
    May 2007
    Location
    Las Vegas, NV
    Posts
    42
    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


    Code:
    header("Location: .?category_id=$category_id");
    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.

  5. #5
    Join Date
    May 2007
    Location
    Las Vegas, NV
    Posts
    42
    Your suggestion provided the following two errors in product_list.php

    Code:
    <?php foreach ($categories as $category) : ?>
    and

    Code:
    <?php foreach ($products as $product) : ?>
    Both of these are the beginning of my foreach loops, basically it can't run/find the $category(s) or $products(s)

    Quote Originally Posted by bionoid View Post
    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:

    Code:
    header("Location: product_list.php?category_id=$category_id");
    Do you have any other suggestions?

  6. #6
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    782


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

    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.

    index.php
    PHP Code:
    <?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");
    }

    ?>
    The reason why the complete "index.php" would be more insight into the problem.
    Last edited by bionoid; 11-23-2013 at 02:44 PM.
    JavaScript: Learn | Validate | Compact

  7. #7
    Join Date
    May 2007
    Location
    Las Vegas, NV
    Posts
    42
    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.

  8. #8
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    782
    "Works" for me means I got a delete button, once pressed I got a button with "Yes" as well.
    Clicking "Yes" runs the delete_product function and does a redirection to http://www/?category_id=99, on my webserver.
    JavaScript: Learn | Validate | Compact

  9. #9
    Join Date
    May 2007
    Location
    Las Vegas, NV
    Posts
    42
    Here is my function list for product_db.php

    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);
    }
    ?>

  10. #10
    Join Date
    May 2007
    Location
    Las Vegas, NV
    Posts
    42
    Quote Originally Posted by bionoid View Post
    "Works" for me means I got a delete button, once pressed I got a button with "Yes" as well.
    Clicking "Yes" runs the delete_product function and does a redirection to http://www/?category_id=99, on my webserver.
    I can't get to the link you posted. =(

  11. #11
    Join Date
    May 2007
    Location
    Las Vegas, NV
    Posts
    42
    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.

  12. #12
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    782
    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.
    JavaScript: Learn | Validate | Compact

  13. #13
    Join Date
    May 2007
    Location
    Las Vegas, NV
    Posts
    42
    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.

  14. #14
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    782
    Well I followed the request, the data the form is sending is correct: action=delete_product&product_id=7&category_id=2
    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.
    JavaScript: Learn | Validate | Compact

  15. #15
    Join Date
    May 2007
    Location
    Las Vegas, NV
    Posts
    42
    Okay, here it is (including all notes).

    PHP Code:
    <?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");
        }    
        
    }
    ?>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles