I have the following Code:
I also tried also to use foreach Loop but it doesn't work. Any one can help me please:

############### Code

<?php
include("config.php");
$sql="SELECT * FROM news";
$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"><strong>Delete multiple rows in mysql</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Lastname</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Email</strong></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['title']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['author']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['date']; ?></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($delete){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM news WHERE id='$del_id'";
$result = mysql_query($sql);
if(!resutl){
echo "Error" . mysql_error();
}
}
}
?>
</table>
</form>
</td>
</tr>
</table>

I have the following Code:
I also tried also to use foreach Loop but it doesn't work. Any one can help me please:

############### Code

<?php
include("config.php");
$sql="SELECT * FROM news";
$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"><strong>Delete multiple rows in mysql</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Lastname</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Email</strong></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['title']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['author']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['date']; ?></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($delete){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM news WHERE id='$del_id'";
$result = mysql_query($sql);
if(!resutl){
echo "Error" . mysql_error();
}
}
}
?>
</table>
</form>
</td>
</tr>
</table>

Try something like this at the top of your file:

if (isset($_POST['delete']))
{
	if (isset($_POST['checkbox']))
	{
		$checkbox = $_POST['checkbox'];
		if (is_array($checkbox)) {
		
			foreach ($checkbox as $key => $your_entry_id)
			{
				$mysqli->query("DELETE FROM your_table WHERE your_entry_id=".$your_entry_id); 
				}
			}
		
		}
	}

NOTES: checkbox isn't any kind of "keyword" here, it's just a name of your array (if i noticed well), also $your_entry_id and your_table need to be replaced with appropriate values

short explanation: this should check if delete button was pressed (if you named it delete, otherwise change this)

if it was, then it checks if any checkbox was checked (i must stop using word "check" right now:))

and then, it goes through the array and deletes any entries that was checked.

Hope it works for you.

Thank you so much for your this wonderful Solution. It worked. But I didn't get this foreach Loop, could you explain it, I know foreach loop that it goes through Array Elements, but the question is with $your_entry_id variable.

Thanks,

Try something like this at the top of your file:

if (isset($_POST['delete']))
{
	if (isset($_POST['checkbox']))
	{
		$checkbox = $_POST['checkbox'];
		if (is_array($checkbox)) {
		
			foreach ($checkbox as $key => $your_entry_id)
			{
				$mysqli->query("DELETE FROM your_table WHERE your_entry_id=".$your_entry_id); 
				}
			}
		
		}
	}

NOTES: checkbox isn't any kind of "keyword" here, it's just a name of your array (if i noticed well), also $your_entry_id and your_table need to be replaced with appropriate values

short explanation: this should check if delete button was pressed (if you named it delete, otherwise change this)

if it was, then it checks if any checkbox was checked (i must stop using word "check" right now:))

and then, it goes through the array and deletes any entries that was checked.

Hope it works for you.

<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!--
Design by Free CSS Templates
http://www.freecsstemplates.org
Released for free under a Creative Commons Attribution 2.5 License

Name       : Sparkling   
Description: A two-column, fixed-width design with dark color scheme.
Version    : 1.0
Released   : 20100704

-->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="keywords" content="" />
<meta name="description" content="" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>ojtWTAR-Company Homepage</title>
<link href="style.css" rel="stylesheet" type="text/css" media="screen" />
<script language='JavaScript'>
    checked = false;
    function checkedAll () {
        if (checked == false){
            checked = true
        }
        else{
            checked = false
        }
        for (var i = 0; i < document.getElementById('insert').elements.length; i++) {
            document.getElementById('insert').elements[i].checked = checked;
        }
    }
</script>
</script>
</head>
<body>
<div id="wrapper">
    <div id="header-wrapper">
      <div id="header">
            <div id="logo">
                <h1><span>OJT</span>WTAR<br />
                <p><span>W</span>eekly <span>T</span>ime and <span>A</span>ctivities <span>R</span>ecords</p></h1>
            </div>
            <div id="menu">
                <ul>
                    <li><a href="company_home.php">Home</a></li>
                    <li><a href="logout.php">Logout</a></li>
                </ul>
            </div>
      </div>
    </div>
    <!-- end #header -->
    <div id="page">
        <div id="page-bgtop">
            <div id="page-bgbtm">
                <div id="content">
                    <div class="post">
                        <h2 class="title" align="center"></h2>
                        <!--<p class="meta"><span class="date">May 10, 2010</span><span class="posted">Posted by <a href="#">Someone</a></span></p>-->
                        <div style="clear: both;"> </div>
                        <div class="entry">
                            <?php
                                include "dbconnection.php";

                                echo "<form name='search' method='get' action='search_ojt.php'>";
                                echo "<input type='text' name='search' id='search-text' value='Search Student ID' />
                                <input type='submit' value='GO' />"."<br />"."<br />";
                                echo "</form>";
                            ?>
                            <?php
                                echo "<form name='form1' id='insert' method='post' action=''>";
                                echo "<table border='1' cellpadding='5' align='center'>";
                                echo "<tr>";
                                echo "<td>#</td><td>Student Id</td><td>Last Name</td><td>First Name</td><td>Middle Initial</td><td>Course</td><td>Gender</td><td>Specialization</td>";
                                echo "</tr>";
                                $query="Select * from select_ojt ORDER BY lastname;";
                                $result=mysql_query($query,$link) or die("Error". mysql_error());

                                $count=mysql_num_rows($result);

                                while($rows=mysql_fetch_array($result,MYSQL_BOTH))
                                {
                                    echo "<tr>";
                                    echo "<td>"."<input name='checkbox[]' type='checkbox' id='checkbox[]' value='$rows[studId]'>"."</td>";
                                    echo "<td>".$rows['studId']."</td>";
                                    echo "<td>".$rows['lastname']."</td>";
                                    echo "<td>".$rows['firstname']."</td>";
                                    echo "<td>".$rows['mi']."</td>";
                                    echo "<td>".$rows['curs']."</td>";
                                    echo "<td>".$rows['gender']."</td>";
                                    echo "<td>".$rows['specialize']."</td>";
                                    echo "</tr>";
                                }
                                echo "</table>";
                                echo "<br />"."<input type='button' name='checkall' value='Select All' onclick='checkedAll();'>";
                                echo "<input name='delete' type='submit' id='delete' value='Delete'>";
                                echo "</form>";
                            ?>
                            <?php
                                $delete = $_POST['delete'];
                                $checkbox = $_POST['checkbox'];
                                // Check if delete button active, start this 
                                if(isset($delete)){
                                    for($i=0;$i<$count;$i++){
                                        $del_id = $checkbox[$i];
                                        $sql = "DELETE FROM select_ojt WHERE studId='$del_id'";
                                        $result = mysql_query($sql);
                                    }
                                // if successful redirect to delete_multiple.php 
                                    if($result){
                                        echo "<meta http-equiv=\"refresh\" content=\"0;URL=company_insert.php\">";
                                    }
                                }
                                mysql_close();
                            ?>
                        </div>
                    </div>
                </div>
                <!-- end #content -->
                <div id="sidebar">
                    <ul>
                        <div style="clear: both;"> </div>
                        <?php
                            echo "<h2>Welcome <i>".$_SESSION['type'].".</i></h2>";
                        ?>
                        <li>
                            <div id="search" >
                                <form method="get" action="#">
                                    <div>
                                        <input type="text" name="s" id="search-text" value="Search a Student..." />
                                        <input type="submit" id="search-submit" value="GO" />
                                    </div>
                                </form>
                            </div>
                            <!--<div style="clear: both;"> </div>-->
                        </li>
                        <li>
                            <div style="clear: both;"> </div>
                            <!--<h2>Categories</h2>-->
                            <ul>
                                <li class="current_page_item"><a href="company_insert.php">Insert Student</a></li>
                                <li><a href="#">Time for Student</a></li>

                            </ul>
                        </li>
                    </ul>
                </div>
                <!-- end #sidebar -->
                <div style="clear: both;"> </div>
            </div>
        </div>
    </div>
    <!-- end #page -->
</div>
<div id="footer">
    <p>Copyright (c) 2011 USJ-R. All rights reserved. Design by Lacno and Ong.</p>
</div>
<!-- end #footer -->
</body>
</html>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.