Page 1 of 1

MySQL: Select a line from a table, then delete it.

Posted: Sat Sep 06, 2008 8:26 pm
by jfeaz
Hi guys! This is my first post here, and believe me, I did my homework.

My dilemma is that I need to provide a website owner with the ability to delete categories from her online store.
This whole store is written in the procedural style, so the main file (admin.php in this case) is set to load the include file named after whatever value is assigned to the variable "content".

So this operation involves 2 files: killcat.inc which presents the list of entries from the "categories" table, and deletecat.inc, which takes the submitted form and runs the query to delete the category.

The problem is that when the form on killcat.inc is submitted, a blank page is returned. In other words, deletecat.inc returns nothing and the query is never run. I scrutinized this for obvious errors all evening last night and came up empty.

What am I missing?

killcat.inc

Code: Select all

 
<?php
 
   if (!isset($_SESSION['store_admin']))
   {
      echo "<h2>Sorry, you have not logged into the system</h2>\n";
      echo "<a href=\"admin.php\">Please login</a>\n";
   } else 
   {
   
      echo "<h2>Please select a category to remove.</h2>\n";
      echo "<p>Please note that this will also delete all products in the category. If you wish to keep the products, please move them into different categories first.</p>\n";
   
    $query="SELECT catid,name from categories"; 
    $result=mysql_query($query); 
    echo "<form action=\"admin.php\" method=\"post\">\n";
    echo "<input name=\"content\" type=\"hidden\" value=\"deletecat\" />\n";    
    echo "<select name=\"catid\">";
    while($row=mysql_fetch_array($result,MYSQL_ASSOC)) 
        { 
        $catid = $row['catid']; 
        $name = $row['name']; 
        echo "<option value=\"$catid\">$name</option>\n"; 
        }
 
    echo "</select>\n"; 
    echo "<input type=\"submit\" name=\"goButton\" value=\"Remove selected category\" />\n";
    echo "</form>";
    }
?>
 
deletecat.inc

Code: Select all

 
<?php
 
   $delete = $_POST['goButton'];
   $catid = $_POST['catid'];
 
   if (isset($_POST['goButton']))
   {
      $query = "DELETE from categories WHERE catid = $catid";
      $result = mysql_query($query);
      if ($result)
      {
         echo "<h2>Category: $catid deleted</h2>\n";
         exit;
      } else
      {
         echo "<h2>Problem deleting $prodid</h2>\n";
         exit;
      }
 
?>
 
I know you guys are volunteers so I really appreciate your help, and I'll do my best to find and answer other questions on this board!

Re: MySQL: Select a line from a table, then delete it.

Posted: Sat Sep 06, 2008 10:53 pm
by Bill H
The form action is "admin.php" which is "including" the "deletecat.php," but I'm guessing the $_POST might not survive that process. "admin.php" gets the $_POST vars, but "deletecat.php" might not. I've never done it that way, but it seems to be the case in your process. It would be better to call the ""deletecat.php" as the form "action=" process.

Re: MySQL: Select a line from a table, then delete it.

Posted: Sat Sep 06, 2008 11:16 pm
by jfeaz
Hmmm, that's very interesting.

After reading your post, I thought about it for a minute. admin.php will include deletecat.inc on line 45 because $_POST['content'] is submitted as "deletecat". deletecat.inc in turn grabs $_POST['catid'], and since deletecat.inc is actually included in admin.php (meaning the code is inserted into the document), admin.php actually does get $_POST['catid'].

Does that make sense, or am I missing your point completely?

admin.php

Code: Select all

 
<?php
session_start();
?>
<!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">
<head>
<link rel="stylesheet" type="text/css" href="../style.css" />
<link rel="stylesheet" media="print" type="text/css" href="../print.css" />
<title>00000000000</title>
</head>
 
<?php 
 
include("../mylibrary/login.php"); 
include("../mylibrary/getThumb.php");
include("../mylibrary/showproducts.php");
 
login(); 
?>
 
<body> 
<table width="100%" border="0"> 
<tr> 
    <td id="header" height="90" colspan="3"> 
    <?php include("../includes/header.inc"); ?>
    </td> 
</tr> 
<tr> 
    <td id="nav" width="20%" valign="top"> 
    <?php include("adminnav.inc"); ?>
    </td> 
    
    <td id="main" width="50%" valign="top"> 
    <?php 
        if (!isset($_REQUEST['content'])) 
        { 
            if (!isset($_SESSION['store_admin'])) 
            include("adminlogin.html"); 
            else include("adminmain.inc"); 
        } 
        else 
        { 
            $content = $_REQUEST['content']; 
            $nextpage = $content . ".inc"; 
            include($nextpage); 
        } 
    ?>
    </td> 
    
    <td id="status" width="30%" valign="top"> 
    <?php include("adminstatus.inc"); ?>
    </td> 
    
</tr> 
<tr> 
    <td id="footer" colspan="3"> 
    <div align="center"> 
    <?php include("../includes/footer.inc"); ?> 
    </div>
    </td> 
</tr> 
</table> 
 
</body> 
</html>
 

Re: MySQL: Select a line from a table, then delete it.

Posted: Sun Sep 07, 2008 8:44 am
by Bill H
Yeah, it was late. $_POST is a superglobal so it's accessibel throughout. Your methodology is really roundabout, and I'm not really following it, so somebody else will have to see where it's failing.

Re: MySQL: Select a line from a table, then delete it.

Posted: Sun Sep 07, 2008 3:42 pm
by jfeaz
Well, I got it working by first capturing the $_POST var from the submit button in a local variable and THEN testing for its exact value before running the query:

Code: Select all

 
$delete = $_POST['goButton'];
      if ($delete == "Delete") 
 

Thank you, Bill H for taking the time to look at my code.