deleting multiple rows from database in php

manojsinghrana

manojsinghrana

@manojsinghrana-NciZJa Oct 18, 2024
this is the code that i have wrote for deleting multiple rows from database. but on pressing delete button instead of deleting a selected row it comes back to the same page again.. so i want someone to correct my code or give me the rite code for deletion. thank you



<?php
// Check if delete button active, start this
if($delete)
{
for($i=0;$i<$count;$i++)
{
$del_id = $checkbox[$i];
$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
$result = mysql_query($sql);
}

// if successful redirect to delete_multiple.php
if($result)
{
echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete_multiple.php\">";
}
}
mysql_close();
?>

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • Manish Goyal

    Manish Goyal

    @manish-r2Hoep Sep 10, 2010

    can you post complete file here?
  • manojsinghrana

    manojsinghrana

    @manojsinghrana-NciZJa Sep 11, 2010

    here is the complete file.


    <?php
    $host="localhost"; // Host name
    $username="root"; // Mysql username
    $password=""; // Mysql password
    $db_name="test"; // Database name
    $tbl_name="test_mysql"; // Table name

    // Connect to server and select databse.
    mysql_connect("localhost", "root", "")or die("cannot connect");
    mysql_select_db("test")or die("cannot select DB");

    $sql="SELECT * FROM $tbl_name";
    $result=mysql_query($sql);

    $count=mysql_num_rows($result);

    ?>
    <table width="400" border="0" cellspacing="1" cellpadding="0">
    <tr>
    <td><form name="form1" method="post" action="">
    <table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
    <tr>
    <td bgcolor="#FFFFFF">&nbsp;</td>
    <td colspan="4" bgcolor="#FFFFFF">Delete multiple rows in mysql </td>
    </tr>
    <tr>
    <td align="center" bgcolor="#FFFFFF">#</td>
    <td align="center" bgcolor="#FFFFFF">Id</td>
    <td align="center" bgcolor="#FFFFFF">Name</td>
    <td align="center" bgcolor="#FFFFFF">Lastname</td>
    <td align="center" bgcolor="#FFFFFF">Email</td>
    </tr>
    <?php
    while($rows=mysql_fetch_array($result)){
    ?>
    <tr>
    <td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $rows['id']; ?>"></td>
    <td bgcolor="#FFFFFF"><?php echo $rows['id']; ?></td>
    <td bgcolor="#FFFFFF"><?php echo $rows['name']; ?></td>
    <td bgcolor="#FFFFFF"><?php echo $rows['lastname']; ?></td>
    <td bgcolor="#FFFFFF"><?php echo $rows['email']; ?></td>
    </tr>
    <?php
    }
    ?>
    <tr>
    <td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
    </tr>
    <?php
    // Check if delete button active, start this
    if($_POST['delete']){
    for($i=0;$i<$count;$i++){
    $del_id = $checkbox[$i];
    $sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
    $result = mysql_query($sql);
    }

    // if successful redirect to delete_multiple.php
    if($result){
    echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete_multiple.php\">";
    }
    }
    mysql_close();
    ?>
    </table>
    </form>
    </td>
    </tr>
    </table>
  • Manish Goyal

    Manish Goyal

    @manish-r2Hoep Sep 11, 2010

    hey check this code ,it works

    Modify it according to your requirement

    Please post your code in code tags next time thanks

    <?php
        mysql_connect("localhost", "root", "")or die("cannot connect");    
        mysql_select_db("area")or die("cannot select DB");
        $sql="SELECT * FROM chandigarh";
        $result=mysql_query($sql);
        $count=mysql_num_rows($result);
    ?>
    <table width="400" border="0" cellspacing="1" cellpadding="0">
        <tr>
            <td>
                <form name="form1" method="post">
                    <table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
                        <tr>
                            <td bgcolor="#FFFFFF">&nbsp;</td>
                            <td colspan="4" bgcolor="#FFFFFF">[b]Delete multiple rows in mysql[/b] </td>
                        </tr>
                        <tr>
                            <td align="center" bgcolor="#FFFFFF">#</td>
                            <td align="center" bgcolor="#FFFFFF">[b]Id[/b]</td>
                            <td align="center" bgcolor="#FFFFFF">[b]Area ID[/b]</td>
                            <td align="center" bgcolor="#FFFFFF">[b]Name[/b]</td>    
                        </tr>
    <?php
        while($rows=mysql_fetch_array($result)){
    ?>
                        <tr>
                            <td align="center" bgcolor="#FFFFFF"><input type="checkbox" name=check[]  value="<?php echo $rows['id']; ?>"></td>
                            <td bgcolor="#FFFFFF"><?php echo $rows['id']; ?></td>
                            <td bgcolor="#FFFFFF"><?php echo $rows['areaid']; ?></td>
                            <td bgcolor="#FFFFFF"><?php echo $rows['Name']; ?></td>
                        </tr>
                                            
    
    <?php
        }
    ?>
                        <tr>
                            <td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
                        </tr>
                        <?php
                        
                        
                                
                                $check=$_POST['check'];
                                
                            if($_REQUEST['delete']=='Delete'){
     {
                                $sql="Delete from chandigarh where id='$val'";
                                
                                foreach($check as $key=>$value)
                                {
                                $sql="Delete from chandigarh where id='$value'";
                                $final=mysql_query($sql);
                                if($final)
                                {
                                echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete.php\">";
                                }                                            } 
                                    }
                                    }
                        // Check if delete button active, start this
                                
                                
    
    // if successful redirect to delete_multiple.php
                                
    mysql_close();
    ?>
    </table>
    </form>
    </td>
    </tr>
    </table>
  • Manish Goyal

    Manish Goyal

    @manish-r2Hoep Sep 11, 2010

    hey check this code ,it works

    Modify it according to your requirement

    Please post your code in code tags next time thanks

    <?php
        mysql_connect("localhost", "root", "")or die("cannot connect");    
        mysql_select_db("area")or die("cannot select DB");
        $sql="SELECT * FROM chandigarh";
        $result=mysql_query($sql);
        $count=mysql_num_rows($result);
    ?>
    <table width="400" border="0" cellspacing="1" cellpadding="0">
        <tr>
            <td>
                <form name="form1" method="post">
                    <table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
                        <tr>
                            <td bgcolor="#FFFFFF">&nbsp;</td>
                            <td colspan="4" bgcolor="#FFFFFF">[b]Delete multiple rows in mysql[/b] </td>
                        </tr>
                        <tr>
                            <td align="center" bgcolor="#FFFFFF">#</td>
                            <td align="center" bgcolor="#FFFFFF">[b]Id[/b]</td>
                            <td align="center" bgcolor="#FFFFFF">[b]Area ID[/b]</td>
                            <td align="center" bgcolor="#FFFFFF">[b]Name[/b]</td>    
                        </tr>
    <?php
        while($rows=mysql_fetch_array($result)){
    ?>
                        <tr>
                            <td align="center" bgcolor="#FFFFFF"><input type="checkbox" name=check[]  value="<?php echo $rows['id']; ?>"></td>
                            <td bgcolor="#FFFFFF"><?php echo $rows['id']; ?></td>
                            <td bgcolor="#FFFFFF"><?php echo $rows['areaid']; ?></td>
                            <td bgcolor="#FFFFFF"><?php echo $rows['Name']; ?></td>
                        </tr>
                                            
    
    <?php
        }
    ?>
                        <tr>
                            <td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
                        </tr>
                        <?php
                        
                        
                                
                                $check=$_POST['check'];
                                
                            if($_REQUEST['delete']=='Delete'){
     {
                                $sql="Delete from chandigarh where id='$val'";
                                
                                foreach($check as $key=>$value)
                                {
                                $sql="Delete from chandigarh where id='$value'";
                                $final=mysql_query($sql);
                                if($final)
                                {
                                echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete.php\">";
                                }                                            } 
                                    }
                                    }
                        // Check if delete button active, start this
                                
                                
    
    // if successful redirect to delete_multiple.php
                                
    mysql_close();
    ?>
    </table>
    </form>
    </td>
    </tr>
    </table>
  • Saandeep Sreerambatla

    Saandeep Sreerambatla

    @saandeep-sreerambatla-hWHU1M Jun 20, 2011

    I have checked the link and its not an advertisement.
    But please refrain from posting links , answer in your own words. If not explain what you learn. It doesnt help pasting links from google.

    If CE is accessible so is google for everyone.