Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 671
  • Last Modified:

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?
0
APD Toronto
Asked:
APD Toronto
  • 6
  • 6
1 Solution
 
Dave BaldwinFixer of ProblemsCommented:
You are not understanding how PHP views POST (and GET) variables.  And you are not distinguishing between the different <form>s so any submit button submits ALL of them.  You have id="delete_cat_form" on all three forms but 'id' is supposed to unique on a page.  You also don't have a unique 'name' attribute on the forms.

I have some sample code that I'll post in a minute.
0
 
Marco GasiFreelancerCommented:
Your forms are incorrect: the input submit is missing closing bracket />. You must fix this ion your php script this way:

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

Open in new window

0
 
Marco GasiFreelancerCommented:
Fixing that, all works like a charms: http://eetest.delphicoding.com/index.php
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Dave BaldwinFixer of ProblemsCommented:
When you post a value with the same name but multiple values like "catID=1,2,3", PHP reads that as assigning a value to 'catID' three times which leaves you with only the last value.  And you are getting multiple values because the forms do not have different names and ids.
0
 
Marco GasiFreelancerCommented:
@DaveBaldwin: hi Dave. Please, see the link above: first Notice due to quick coding apart, the page works perfectly. After all, each form acts by itself, not depending by other forms in the page: each time you press submit button, one and only one value is submitted, so the result is the expected one.
0
 
Dave BaldwinFixer of ProblemsCommented:
You could simply use 'catID' to add a number to the name of  each form to distinguish it to get it to look like this:
            <tr>
            <td>Guitars</td>
                    <td>
                        <form name="form1" 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 name="form2" 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 name="form3" 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>
 

Open in new window

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

Open in new window

0
 
Dave BaldwinFixer of ProblemsCommented:
@Marco, your method may work but I would never do it that way.
0
 
Marco GasiFreelancerCommented:
Why?
0
 
Marco GasiFreelancerCommented:
Personally, I would have used only one form and several checkboxes to do it, but I don't see any problem doing it that way, but I'm not an eagle :)
0
 
Dave BaldwinFixer of ProblemsCommented:
Because in all the work that I have done, forms get a unique name and id.  That is often so I can use javascript validation and direct it to the proper form.  It becomes more important when there is more than one form on a page.  It has become a standard method for me.

If you use one form with several checkboxes, the checkboxes need to have unique names or a name in an array format like 'catID[]' for PHP to handle them properly when you check more than one.
0
 
Marco GasiFreelancerCommented:
Yes, obviously the checkboxes should have an unique name.
Please, try to understand what I mean: I agree it is alway better to have unique ids and names, and if you use javascript this is mandatory. I always mean that in this specific situation, the problem was the unclosed tags for submit inputs.

Cheers
0
 
Dave BaldwinFixer of ProblemsCommented:
If that is all there is to the problem, that may be all right.  But I stand by my recommendations, that's just the way I have learned to do things.  Simple pages like in this question tend to grow into more complicated ones.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 6
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now