Jump to content

Delete from table troubles


plugnz

Recommended Posts

Hi there,

 

This is one of those really annoying questions that is probably so obvious but I just can t see it. I'm trying to delete some rows from my database which have been selected from a drop down box in my form. Heres the code

echo " <td width = '50%'>";
echo ' <br><select name="category_name" size="1" > ';
echo ' <optgroup label="-----------------------------------------">';
  
$query = "SELECT category_id, category_name " .
             "FROM cms_categories " .
             "ORDER BY category_name ASC";
		   
    $results = mysql_query($query,$conn)
      or die(mysql_error());

    while ($row = mysql_fetch_array($results)) {
      extract($row);  	   		  
  echo ' <option selected value="' . $row['category_name'] .
       '" selected="selected">'. htmlspecialchars($row['category_name']);
  }
  echo " </option></optgroup>\n";
  echo " </select>\n";	
      echo ' <input type="hidden" name="category_id" value="' . $row['category_id'] . "\">\n";
  echo ' <input type="submit" class="submit" name="action" ' .
           "value=\"Delete Category\"></td></tr>\n";

Plus...

case 'Delete Category':
      if (isset($_POST['category_name'])
  and isset($_POST['category_id'])) {
        $sql = "DELETE FROM cms_categories " .
               "WHERE category_name= " . $_POST['category_name'] .
		  "AND category_id=" . $_POST['category_id']; 
  
        mysql_query($sql, $conn)
          or die('Could not DELETE category; ' . mysql_error());

 

And here's the error message:

Could not DELETE category; You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'category_id=' at line 1

have tried $_GET instead of $_POST and get no error but the rows don't get deleted either.

If I take out the category_id part all together i get the following error message

Could not DELETE category; Unknown column 'Schools' in 'where clause'

Which tells me that the category name data is being passed ok so I'm guessing its the category_id hidden field thats upsetting it...

 

I have tried replacing the $_POST[] with the actual data and that works so somehow the database is not receiving the right info to make it work.

any thoughts why this is not working?

 

Thanks in advance

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.