Avatar of APD Toronto
APD Toronto
Flag for Canada

asked on 

PHP Post

Hi Experts,

I am taking a PHP course, but cannot get the PHP post to work.

First, I have the following PHP code:
<?php
    require_once('database.php');

    // Get all categories
    $query = 'SELECT * FROM categories
              ORDER BY categoryID';
    $categories = $db->query($query);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<!-- the head section -->
<head>
    <title>My Guitar Shop</title>
    <link rel="stylesheet" type="text/css" href="main.css" />
</head>

<!-- the body section -->
<body>
    <div id="page">

    <div id="header">
        <h1>Product Manager</h1>
    </div>

    <div id="main">

    <h1>Category List</h1>
    <table>
        <tr>
            <th>Name</th>
            <th>&nbsp;</th>
        </tr>
        
    <!-- add code for the rest of the table here -->
    <?php foreach($categories as $cat) : ?>
        <tr>
            <td><?php echo $cat['categoryName']; ?></td>
                    <td>
                        <form action="delete_category.php" method="post" id="delete_cat_form">
                            <input type="hidden" name="catID" 
                                   value="<?php echo $cat['categoryID']; ?>"/>
                            <input type="submit" value="Delete "
                        </form>
                     </td>
                
        </tr>
    <?php endforeach; ?>
    </table>
    <br />

    <h2><a href="add_category_form.php">Add Category</a></h2>
    
    <!-- add code for the form here -->
    
    <br />
    <p><a href="index.php">List Products</a></p>

    </div> <!-- end main -->

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

    </div><!-- end page -->
</body>
</html>

Open in new window


This produces the following html:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<!-- the head section -->
<head>
    <title>My Guitar Shop</title>
    <link rel="stylesheet" type="text/css" href="main.css" />
</head>

<!-- the body section -->
<body>
    <div id="page">

    <div id="header">
        <h1>Product Manager</h1>
    </div>

    <div id="main">

    <h1>Category List</h1>
    <table>
        <tr>
            <th>Name</th>
            <th>&nbsp;</th>
        </tr>
        
    <!-- add code for the rest of the table here -->
            <tr>
            <td>Guitars</td>
                    <td>
                        <form action="delete_category.php" method="post" id="delete_cat_form">
                            <input type="hidden" name="catID" 
                                   value="1"/>
                            <input type="submit" value="Delete "
                        </form>
                     </td>
                
        </tr>
            <tr>
            <td>Basses</td>
                    <td>
                        <form action="delete_category.php" method="post" id="delete_cat_form">
                            <input type="hidden" name="catID" 
                                   value="2"/>
                            <input type="submit" value="Delete "
                        </form>
                     </td>
                
        </tr>
            <tr>
            <td>Drums</td>
                    <td>
                        <form action="delete_category.php" method="post" id="delete_cat_form">
                            <input type="hidden" name="catID" 
                                   value="3"/>
                            <input type="submit" value="Delete "
                        </form>
                     </td>
                
        </tr>
        </table>
    <br />

    <h2><a href="add_category_form.php">Add Category</a></h2>
    
    <!-- add code for the form here -->
    
    <br />
    <p><a href="index.php">List Products</a></p>

    </div> <!-- end main -->

    <div id="footer">
        <p>
            &copy; 2014 My Guitar Shop, Inc.
        </p>
    </div>

    </div><!-- end page -->
</body>
</html>

Open in new window


However, when I click "Delete" for any category I always get cat= 3 with the following code in my delete_category.php.
<?php

$cat = $_POST['catID'];

$query = "cat = $cat";
echo $query;
        
?>

Open in new window


What am I doing wrong?
PHP

Avatar of undefined
Last Comment
Dave Baldwin

8/22/2022 - Mon