Hyperjase Posted April 27, 2009 Share Posted April 27, 2009 I'm wanting to remove rows from the database, using an auto-populated drop down. I'm having issues tying up which variable I need to pull from the drop down section as to what needs deleting in the database ... here's the drop down code: <?php if (isset($_GET['removecat'])) { ?> <p class="admintxtl">Remove Category</p> <FORM ENCTYPE="multipart/form-data" ACTION="<?php echo($_SERVER['PHP_SELF']); ?>" METHOD='POST'> <p><span class="admintxt">Remove Category</span><br /> <?php $query="SELECT * FROM category ORDER by 'catname'"; $result = mysql_query ($query); echo "<select name='removecat'>"; while($nt=mysql_fetch_array($result)) { echo "<option value=$nt[id]>$nt[catname]</option>"; } echo "</select>"; ?> <input type="submit" name="removecat" value="Remove Category" class="admintxt" /> </form> <br /> <br /> </p><?php } ?> And here's the code for the actual DB DELETE command: <?php if (isset($_POST['removecat'])) { $result = "DELETE from category WHERE id = ('{$_POST['removecat']}')"; mysql_query( $result ) or die (mysql_error()); echo "<html><head><meta http-equiv='refresh' content='5;url=admin.php?main'></head>"; echo "<p class='bigtext'>".$_POST['category']." removed from the database".$proceed; exit; } ?> I've done plenty of searching, much testing but I can't suss it out, I'm almost certain it's something simple I'm missing! Cheers Jason Quote Link to comment https://forums.phpfreaks.com/topic/155893-solved-mysql-delete-using-auto-populated-drop-down/ Share on other sites More sharing options...
ignace Posted April 27, 2009 Share Posted April 27, 2009 Use the multiple="multiple" option on your select element which allows you to select multiple categories at once <?php if (isset($_GET['removecat'])) { ?> <p class="admintxtl">Remove Category</p> <form enctype="application/x-www-form-urlencoded" action="<?php echo($_SERVER['PHP_SELF']); ?>" method="post"> <p><span class="admintxt">Remove Category</span><br /> <?php $query='SELECT * FROM category ORDER BY \'catname\''; $result = mysql_query ($query); echo '<select name="removecats[]" multipe="multiple">'; while($nt=mysql_fetch_array($result)) { echo "<option value=\"{$nt['id']}\">{$nt['catname']}</option>"; } echo '</select>'; ?> <input type="submit" name="removecat" value="Remove Category" class="admintxt" /> </form> <br /> <br /> </p><?php } ?> <?php if (isset($_POST['removecat'])) { $query = 'DELETE from category WHERE id IN ('. implode(', ', $_POST['removecats']) .')'; echo $query;// debug mysql_query( $query ) or die (mysql_error()); echo "<html><head><meta http-equiv='refresh' content='5;url=admin.php?main'></head>"; echo "<p class='bigtext'>".$_POST['category']." removed from the database".$proceed; exit; } ?> Your problem was that you named your submit button the same as your select element tag. I have renamed your select name="removecats" (plural) P.S. Your code is a mess. I strongly advice separating your html from your php as much as possible. Quote Link to comment https://forums.phpfreaks.com/topic/155893-solved-mysql-delete-using-auto-populated-drop-down/#findComment-820587 Share on other sites More sharing options...
Hyperjase Posted April 27, 2009 Author Share Posted April 27, 2009 Thanks for the help, I do understand it's a tad messy, I'm still learning what does what, hence the mess and quite a few questions! One more thing; the final echo (echo "<p class='bigtext'>".$_POST['category']." removed from the database".$proceed;) attempts to pull the name of the category that has just been deleted, how would I go about this? I was going to use the removecats variable, but thats only the category id number. Any ideas? On a side note how would i move the html outside the php in this? I wasn't sure how to do that. Thanks, Jason Quote Link to comment https://forums.phpfreaks.com/topic/155893-solved-mysql-delete-using-auto-populated-drop-down/#findComment-820610 Share on other sites More sharing options...
premiso Posted April 27, 2009 Share Posted April 27, 2009 An extra SQL Statement. <?php if (isset($_POST['removecat'])) { $query = 'SELECT category_name FROM category WHERE id IN ('. implode(', ', $_POST['removecats']) .')'; $result = mysql_query($query); $removed = array(); while ($row = mysql_fetch_assoc($result)) { $removed[] = $row['category_name']; // change this to be the column name } $query = 'DELETE from category WHERE id IN ('. implode(', ', $_POST['removecats']) .')'; echo $query;// debug mysql_query( $query ) or die (mysql_error()); echo "<html><head><meta http-equiv='refresh' content='5;url=admin.php?main'></head>"; echo "<p class='bigtext'>". implode(', ', $removed) ." removed from the database".$proceed; exit; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/155893-solved-mysql-delete-using-auto-populated-drop-down/#findComment-820619 Share on other sites More sharing options...
Hyperjase Posted April 27, 2009 Author Share Posted April 27, 2009 Thanks ... although I get an error when I remove a category now: Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/stea7024/public_html/anthony/admin.php on line 93 Not entirely sure what that means, I can't see anything obvious which is out of place Quote Link to comment https://forums.phpfreaks.com/topic/155893-solved-mysql-delete-using-auto-populated-drop-down/#findComment-820646 Share on other sites More sharing options...
premiso Posted April 27, 2009 Share Posted April 27, 2009 Did you make sure you change the column name in this sql statement? $query = 'SELECT category_name FROM category WHERE id IN ('. implode(', ', $_POST['removecats']) .')'; To be what it actually is? Quote Link to comment https://forums.phpfreaks.com/topic/155893-solved-mysql-delete-using-auto-populated-drop-down/#findComment-820652 Share on other sites More sharing options...
Hyperjase Posted April 27, 2009 Author Share Posted April 27, 2009 Hehe ... I missed that ... I think it's time for bed when I'm missing things so obvious. No doubt will have plenty more questions tomorrow Thanks again, Jason Quote Link to comment https://forums.phpfreaks.com/topic/155893-solved-mysql-delete-using-auto-populated-drop-down/#findComment-820667 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.