Delete from table

Discussion in 'PHP' started by ishandoshi, Jul 31, 2009.

  1. #1
    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.
     
    ishandoshi, Jul 31, 2009 IP
  2. techbongo

    techbongo Active Member

    Messages:
    309
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    80
    #2
    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):
     
    techbongo, Jul 31, 2009 IP
  3. XDMCoder

    XDMCoder Peon

    Messages:
    50
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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 ;)
     
    XDMCoder, Jul 31, 2009 IP
  4. ptalent

    ptalent Peon

    Messages:
    26
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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:
     
    ptalent, Jul 31, 2009 IP
  5. techbongo

    techbongo Active Member

    Messages:
    309
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    80
    #5
    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.
     
    techbongo, Jul 31, 2009 IP
  6. Leron

    Leron Active Member

    Messages:
    38
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    53
    #6
    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.
     
    Leron, Jul 31, 2009 IP
  7. ptalent

    ptalent Peon

    Messages:
    26
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    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
     
    ptalent, Aug 1, 2009 IP
  8. NoamBarz

    NoamBarz Active Member

    Messages:
    242
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    58
    #8
    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\"";
     
    NoamBarz, Aug 1, 2009 IP