how to delete selected records from database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mukeshrasm
    Contributor
    • Nov 2007
    • 254

    how to delete selected records from database

    hello every one

    i want to delete the selected records from database. selection is based on check box. so when user will select any check box it will delete the corresponding records from database.

    how can i do that using php. if any one havin code for that please help me

    thanks
  • arunj82
    New Member
    • Mar 2007
    • 15

    #2
    Hi,
    You can delete selected records in php. For example,

    If you are using a file called test.php,

    <input type="checkbox" name="delete" value="<?=$resu lt['id']?>" onClick="return DeleteThisData( this.value);">

    This is the checkbox you are using while retrieving datas from the database.
    I am calling a javascript function called DeleteThisData( );

    function DeleteThisData( value)
    {
    location.href=" test.php?del="+ value
    }

    This is client side Script which passes the selected record to the url.

    In Serverside coding ,

    <?php
    if($_REQUEST['del'])
    {
    $sql="delete from tablename where id=".$_REQUEST['id'];
    mysql_query($sq l) or die(mysql_error ());
    }
    ?>

    In this way you can delete a single record by record.

    Comment

    • mukeshrasm
      Contributor
      • Nov 2007
      • 254

      #3
      Originally posted by arunj82
      Hi,
      You can delete selected records in php. For example,

      If you are using a file called test.php,

      <input type="checkbox" name="delete" value="<?=$resu lt['id']?>" onClick="return DeleteThisData( this.value);">

      This is the checkbox you are using while retrieving datas from the database.
      I am calling a javascript function called DeleteThisData( );

      function DeleteThisData( value)
      {
      location.href=" test.php?del="+ value
      }

      This is client side Script which passes the selected record to the url.

      In Serverside coding ,

      <?php
      if($_REQUEST['del'])
      {
      $sql="delete from tablename where id=".$_REQUEST['id'];
      mysql_query($sq l) or die(mysql_error ());
      }
      ?>

      In this way you can delete a single record by record.


      Thanks Arjun

      Actually I want to delete the multiple record so please how can I do that..

      Give the code for that as well

      Thanks in Advance..

      Comment

      • arunj82
        New Member
        • Mar 2007
        • 15

        #4
        Hi,

        Here i m giving a example for deleting all records and also u can delete a single record. U check it and if u have any doubt u pls message me. If you press all button in the headings, it will delete all records.


        Sample Codings:

        <?php
        $con=mysql_conn ect("localhost" ,"root","simply 123") or die(mysql_error ());
        mysql_select_db ("databasename" ,$con) or die(mysql_error ());

        // Give r connection details for database

        if($_REQUEST['delete'])
        {
        $totalarray=siz eof($del);
        for($i=0;$i<$to talarray;$i++)
        {
        if(!empty($del[$i])){
        mysql_query("de lete from tbl_sample where id=".$del[$i]) or die(mysql_error ());
        }
        }
        $msg="Values Deleted Successfully";
        }
        ?>
        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
        <HTML>
        <HEAD>
        <TITLE> New Document </TITLE>
        <script language="JavaS cript">
        <!--
        function CheckAll()
        {
        for (var i=0;i<document. frmdeleteall.el ements.length;i ++)
        {
        var e = document.frmdel eteall.elements[i];
        if (e.name != "allbox")
        e.checked = document.frmdel eteall.allbox.c hecked;
        }
        }
        //-->
        </script>
        </HEAD>
        <BODY>
        <form name="frmdelete all" method="post" action="deletea ll.php">
        <table border=1 cellpadding=0 cellspacing=2 width="70%" align="center">
        <?if($msg){?>
        <tr>
        <td colspan=5 align="center"> <?=$msg?></td>
        </tr>
        <?}?>
        <tr>
        <td>S.No</td>
        <td>Name</td>
        <td>Address</td>
        <td>City</td>
        <td>All: <input name="allbox" type="checkbox" value="1" onClick="CheckA ll();"></td>
        </tr>
        <?
        $sql="select * from tbl_sample order by id limit 0,3";
        $i=0;
        $query=mysql_qu ery($sql) or die(mysql_error ());
        while($row=mysq l_fetch_array($ query))
        {
        echo "<tr>";
        echo "<td>".$i++ ."</td>";
        echo "<td>".$row['Name']."</td>";
        echo "<td>".$row['Address']."</td>";
        echo "<td>".$row['City']."</td>";
        echo "<td><input type=\"checkbox \" name=\"del[]\" value=".$row['id']."></td>";
        echo "</td>";
        echo "</tr>";
        }
        ?>
        <tr>
        <td colspan=5 align="center"> <input type="submit" name="delete" value="Submit"> </td>
        </tr>
        </table>
        </BODY>
        </HTML>

        Comment

        • kusang
          New Member
          • Dec 2007
          • 1

          #5
          thx dude.... your script very helpful for me.

          Comment

          • mukeshrasm
            Contributor
            • Nov 2007
            • 254

            #6
            Originally posted by arunj82
            Hi,

            Here i m giving a example for deleting all records and also u can delete a single record. U check it and if u have any doubt u pls message me. If you press all button in the headings, it will delete all records.


            Sample Codings:

            <?php
            $con=mysql_conn ect("localhost" ,"root","simply 123") or die(mysql_error ());
            mysql_select_db ("databasename" ,$con) or die(mysql_error ());

            // Give r connection details for database

            if($_REQUEST['delete'])
            {
            $totalarray=siz eof($del);
            for($i=0;$i<$to talarray;$i++)
            {
            if(!empty($del[$i])){
            mysql_query("de lete from tbl_sample where id=".$del[$i]) or die(mysql_error ());
            }
            }
            $msg="Values Deleted Successfully";
            }
            ?>
            <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
            <HTML>
            <HEAD>
            <TITLE> New Document </TITLE>
            <script language="JavaS cript">
            <!--
            function CheckAll()
            {
            for (var i=0;i<document. frmdeleteall.el ements.length;i ++)
            {
            var e = document.frmdel eteall.elements[i];
            if (e.name != "allbox")
            e.checked = document.frmdel eteall.allbox.c hecked;
            }
            }
            //-->
            </script>
            </HEAD>
            <BODY>
            <form name="frmdelete all" method="post" action="deletea ll.php">
            <table border=1 cellpadding=0 cellspacing=2 width="70%" align="center">
            <?if($msg){?>
            <tr>
            <td colspan=5 align="center"> <?=$msg?></td>
            </tr>
            <?}?>
            <tr>
            <td>S.No</td>
            <td>Name</td>
            <td>Address</td>
            <td>City</td>
            <td>All: <input name="allbox" type="checkbox" value="1" onClick="CheckA ll();"></td>
            </tr>
            <?
            $sql="select * from tbl_sample order by id limit 0,3";
            $i=0;
            $query=mysql_qu ery($sql) or die(mysql_error ());
            while($row=mysq l_fetch_array($ query))
            {
            echo "<tr>";
            echo "<td>".$i++ ."</td>";
            echo "<td>".$row['Name']."</td>";
            echo "<td>".$row['Address']."</td>";
            echo "<td>".$row['City']."</td>";
            echo "<td><input type=\"checkbox \" name=\"del[]\" value=".$row['id']."></td>";
            echo "</td>";
            echo "</tr>";
            }
            ?>
            <tr>
            <td colspan=5 align="center"> <input type="submit" name="delete" value="Submit"> </td>
            </tr>
            </table>
            </BODY>
            </HTML>

            Hi

            I am not getting this line

            $totalarray=siz eof($del);

            in the above code where you have assigned $del.

            sorry if I asked something silly.

            Thanks!

            Comment

            • bnashenas1984
              Contributor
              • Sep 2007
              • 257

              #7
              Hi
              I think I know what you mean by deleting multiple records at once by using checkboxes.
              Actualy I'v done it in one of my websites.

              in the form that user is able to check multiple records use this script

              Code:
              <?PHP 
              	$maximum = 10;  //10 is the number of results per page (could be any other number)
              	for ($i=1; $i <= $maximum; $i++) {
              		print "<input type=\"checkbox\" name=\"checkbox-$i\" value=\"$recordid[i]\" />";
              	}
              ?>
              As you can see $recordid is an array containing your record ids. and name of you checkboxes are ("checkbox-"+recordid)

              Then this is what happens when user submits the form

              Selected checkboxes return the value of the record ids that should be deleted

              Now all you have to do is to control checkboxes to see if they have been checked or not
              Like this:

              Code:
              <?PHP 
              	$maximum = 10;  //10 is the number of results per page (could be any other number)
              	for ($i=1; $i <= $maximum; $i++) {
              		if ($_REQUEST["checkbox-$i"]) {
              			$query = "DELETE FROM yourtable WHERE id='$_REQUEST[\"checkbox-$i\"]'";
              			mysql_query($query);
              		}
              	}
              ?>
              Note that there are other options to make your quey variable. Like adding all of them in one query and do it at once instead of deleting them one by one


              Hope this helps you

              Comment

              • bnashenas1984
                Contributor
                • Sep 2007
                • 257

                #8
                Oh by the way
                I forgot to say. The good thing about this kind of deleting form is that there is no client side script in it. It's all server side

                Comment

                Working...