plugnz Posted May 24, 2010 Share Posted May 24, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/202731-delete-from-table-troubles/ Share on other sites More sharing options...
jskywalker Posted May 24, 2010 Share Posted May 24, 2010 i think there's a space in : "WHERE category_name= " . $_POST['category_name'] . so, change it to: "WHERE category_name= '" . $_POST['category_name'] ."'". Quote Link to comment https://forums.phpfreaks.com/topic/202731-delete-from-table-troubles/#findComment-1062573 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.