How to delete records from MySql with a single click of a button?

Discussion in 'PHP' started by eritrea1, Aug 4, 2012.

  1. #1
    Hi guys

    As the title says it, I would like to have the ability to drop/delete table/s from MySql. Each field will be marked with 'X' And, when the user clicks on it, than it should be deleted from database.

    Any suggestion would be fine.
     
    eritrea1, Aug 4, 2012 IP
  2. webshore88

    webshore88 Well-Known Member

    Messages:
    131
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    123
    #2
    Did you inserted whole html data in DB?
     
    webshore88, Aug 4, 2012 IP
  3. eritrea1

    eritrea1 Active Member

    Messages:
    182
    Likes Received:
    9
    Best Answers:
    2
    Trophy Points:
    70
    #3
    Yes, I already made a query that lists all results from tables so I need to create a script that deletes some of these results on demand
     
    eritrea1, Aug 4, 2012 IP
  4. webshore88

    webshore88 Well-Known Member

    Messages:
    131
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    123
    #4
    Has every html table contains a unique id or something?
     
    webshore88, Aug 4, 2012 IP
  5. eritrea1

    eritrea1 Active Member

    Messages:
    182
    Likes Received:
    9
    Best Answers:
    2
    Trophy Points:
    70
    #5
    Please take a look at this block of code, it lists some 10 records from database in relation to their id

    
    
    <?php
    echo '<span class="left_headers">RECENT ARTICLES<hr/></span>';
    
    
    $query="SELECT * FROM articles ORDER BY date DESC LIMIT 0,10";  
    $rt=mysql_query($query);          
    echo mysql_error();                   
    	while($nt=mysql_fetch_array($rt)){
    echo "  
    
    
    <li ><a href='articles/index2.php?id=$nt[id]'> $nt[title] </a> </li>
    "; 
    
    
    }?> 
    
    
    Code (markup):
     
    eritrea1, Aug 4, 2012 IP
  6. writingwhiz

    writingwhiz Peon

    Messages:
    12
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #6
    You need to create an HTML form that, when submitted, runs a query with the ID of the row and then issues the DELETE query to MySQL.

    I can do this for you very quickly; send me a PM if you're interested.
     
    writingwhiz, Aug 4, 2012 IP
  7. webshore88

    webshore88 Well-Known Member

    Messages:
    131
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    123
    #7
    It is pretty simple to solve it. If you don't mind you can pm me your DB structure I will code for you. :)
     
    webshore88, Aug 4, 2012 IP
  8. webshore88

    webshore88 Well-Known Member

    Messages:
    131
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    123
    #8
    Dear eritrea1,
    As I guess you have a DB table(article) with fields id, title...
    you can use ajax to delete the article based in its id, here is the code


    first print your delete article link like this:
    echo "<li id=\"article".$nt[id]."\"><a onclick=\"remove_article(".$nt[id].")>".$nt[title]."<img src=\"cross.jpg\"></a></li>";
    PHP:
    which you are printing in while loop
    then js in your html header and don't forget to remove mygreatesite.com with your domain or host url
    
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.js"></script>
    <script type="text/javascript">
        var root = "http://mygreatesite.com";
        function remove_article(id){
            $.ajaxSetup({
                    type:"post",
                    url: root+"/remove_article.php"
                });
                $.ajax({
                    data: 'id='+id,
                    success:function(response){
                        if(response == 'true'){
                            alert('article has been removed');
                            $('#article'+id).remove();
                        }
                        else{
                            alert('Some php error: '+response);
                        }
                    }
                });
        }
    </script>
    
    Code (markup):
    now mysql query in remove_article.php
    if($_POST['id']){    $query = "DELETE FROM `article` WHERE `id`='.$_POST['id'].'";    mysql_query($query) or die(mysql_error());    echo 'true';}
    PHP:
     
    webshore88, Aug 5, 2012 IP