Hii, I am able show values in a dropdown list but i am not able to select the value and delete it from DB. Please suggest me what should i do... For eg : if someone selects HISTORY from dropdown,i want entire row to be deleted which contains SUBJECT as HISTORY.
Code for your form Select Option. <form action="php_file_name.php" method="post"> <select name="subject"> <option value="HISTORY">HISTORY</option> <option value="...">... ...</option> :::::::::::::::::::::: :::::::::::::::::::::: <select> <input type="submit" name="delete" value="Delete" /> </form> Code (markup): Code for Your PHP file: <?php if(isset($_POST['delete'])) { mysql_connect("host","user","pass"); mysql_select_db("db_name"); mysql_query("DELETE FROM table_name WHERE SUBJECT='".$_POST['subject']."'"); } ?> Code (markup):
Well you can use your dropdown list elements as links and by GET method send a subject to PHP-script (http://yoursite.com/delete.php?subject=history). $subject=$_GET['subject']; mysql_query("DELETE FROM table WHERE subject='$subject'"); PHP: P.S. for real-time (without refreshing) script use AJAX
The SQL command for that is: DELETE FROM table WHERE subject like '%history%'; Code (markup): So the actual php should look like this: $strSearch = $_POST['subject']; $deleteSQL = "DELETE FROM table WHERE subject like '%".$strSearch."%'"; mysql_query($deleteSQL) or die(mysql_error()); PHP:
You're not right. subject like '%history%' will delete all the tables where it gets an expression matching history. Now if the subject is 'Ancient History', it'll also be deleted. I guess, this is not seeked by the thread starter.
Unless you're looking to use wildcards where the search only has to contain a portion of the patten, I would suggest sticking with the equal (=) operator as tech bongo suggests. You don't want to have erratic behavior with your script and database. It has also been said that it is faster when only equal comparison is required.
I misread the post a bit, i thought the guy wanted to delete a row where the subject contained history, as in history was in there somewhere, not the only actual thing. Oops
I'd like to see the select statement first, but at least be sure to check for quotes $strSearch = str_replace("\"", "\\\"", trim($_POST['subject'])); $deleteSQL = "DELETE FROM table WHERE subject like \"$strSearch\"";