I have checkboxes in my HTML form and i want to delete several records from my database using those checkboxes. I'm quite new to php so I have a dificulty in manipulating the checkboxes. so can anyone help me? thank you!
Example....

I have 5 records and i want to delete two. So i'll check 2 checkboxes and then "submit" it. how do i do this in php? making sure that the records were really deleted in my database.

<input type='checkbox' name='someArray[]' value='1' />
<input type='checkbox' name='someArray[]' value='2' />
<input type='checkbox' name='someArray[]' value='3' />

Then in PHP

$someArray = $_REQUEST['someArray'];
foreach($someArray as $key=>$someElem){
    $query = "DELETE FROM someTable WHERE id = ".$someElem;
}

I think something's wrong with this code that i made.....the 1st part is the FORM part, the 2nd part is the PHP part
......................FORM.............

<?php
session_start();
?>


<html>
	<body link='000000' vlink='000000' alink='gray' >
		<table border='0' width='100%' align='center' bgcolor='ffffff' >
			<tr><td></br></td></tr>
			<tr>
				<td width='25%' valign='bottom'>
					<table border='0'>
						<tr><td><font face='tahoma' size='4'><a href='home.php' style='text-decoration:none;'>Home</a></font></td></tr>
						<tr><td background='line2.jpg' height='3'></td></tr>
						<tr><td><font face='tahoma' size='4' ><a href='createEmp.php' style='text-decoration:none;'>Create Personnel Record</a></font></td></tr>
						<tr><td background='line2.jpg' height='3'></td></tr>
						<tr><td><font face='tahoma' size='4' ><a href='search_view5.php' style='text-decoration:none;'>Edit Personnel Record</a></font></td></tr>
						<tr><td background='line2.jpg' height='3'></td></tr>
						<tr><td><font face='tahoma' size='4'color='silver'>Delete Personnel Record</font></td></tr>
						<tr><td background='line2.jpg' height='3'></td></tr>
						<tr><td><font face='tahoma' size='4' ><a href='search_view3.php' style='text-decoration:none;'>View Personnel Record</a></font></td></tr>
						<tr><td background='line2.jpg' height='3'></td></tr>
						<tr><td><font face='tahoma' size='4'><a href='sign.php' style='text-decoration:none;'>Logout</a></font></td></tr>
					</table>

				
				
				</td>
				<td><img src='header5.jpg' align='center'/></td>
			</tr>
			<tr>
				<td colspan='2' background='line.jpg' height='8'></td>
			</tr>
			<tr>
				<td colspan='2' background='line2.jpg' height='2'></td>
			</tr>
			<tr>
				<td colspan='2' height='20' valign='top'>
					</br>&nbsp;&nbsp;&nbsp;<font face='tahoma' size='5' color='gray'>Search Patient Profile</font>
					</br><hr width='30%' align='left' size='3' color='silver'></hr></br></br></br>
					<center>
						<form action="search_view6.php" method="post" >
						<input type="submit" value="choice" />
						<input type="text" size='40' maxLength="50" name="choice" /> 
						<select name='choose' >   
									<option value='name'>Lastname</option>
									<option value='id'>Doctor's License</option>
						</select>
						</form>
					</center>
			</tr>		
			<tr>
				<td colspan='2' height='220' valign='top'>
						<?php
							$con=mysql_connect("localhost", "root", "x");
							if(!$con)
							{
								die('could not connect: '. mysql_error());
							}
							mysql_select_db("clinic", $con);
							if($_POST[choice]!=NULL)
							{
								$_SESSION['var'] = $_POST[choice];	
								$result=mysql_query("SELECT doc_license, doc_fname, doc_mname, doc_lname FROM doctor where doc_license= '$_POST[choice]' or doc_lname= '$_POST[choice]' ");
								$row =mysql_fetch_assoc($result);
								
								if($row!=NULL)
								{
									echo "<table border='5' align='center' bordercolor='skyblue' bordercolorlight='skyblue' bordercolordark='skyblue' cellpadding='2' cellspacing='0' width='50%'>
									<tr>
									<th>Doctor's License</th>
									<th>Name</th>
									<th>Delete</th>
									</tr>";
									echo "<tr align='center'>";
		 							echo "<td>". $row['doc_license']."</td>";
									echo "<td>". $row['doc_fname']." ". $row['doc_mname']." ". $row['doc_lname']."</td>";
									echo "<td> <input type=\"checkbox\" name=\"id" . $row[doc_license] . "\" value=\"1\"></td>";

									$idList = $sep.$row[id];
									$sep = ",";
										
							        		

									echo "</tr>";
									echo "</table>";
					
									echo "<form action='deleteRec.php' method='post' >
								 			<input type='submit' value='Delete Personnel Record'/>
							            </form>";
									echo "<input type=\"hidden\" name=\"idlist\" value=\"$idList\">";
								}						
								else
								{ 
									echo "<center>Sorry, record does not exist...</center>";
								}
								
							}
							
							mysql_close($con);
						?>
			</tr>		
			<tr>
				<td></br></br></br></br></td>
			</tr>
			<tr>
				<td colspan='2' background='line2.jpg' height='2'></td>
			</tr>
		</table>
	
	
	</body>
</html>

.......................PHP ...................

<?php
session_start();
$idlist=$_POST['id'];

	$con=mysql_connect("localhost", "root", "x");
	if(!$con)
	{
		die('could not connect: '. mysql_error());
	}
	mysql_select_db("clinic", $con)or die('could not connect: '. mysql_error());


	$array_id = explode("," $idlist");
	foreach($array_id as $id)
	{
    		if(${"id" . $id} == 1)
		{
      		$result=mysql_query("DELETE * FROM doctor where doc_license= $id ");
		}
	}
	
?>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
		<title>Search Patient Record</title>
	</head>
	<body link='000000' vlink='000000' alink='gray'>
		<table border='0' width='100%' align='right' bgcolor='ffffff'>
			<tr><td></br></td></tr>
			<tr>
				<td width='25%' valign='bottom'>
					<table border='0'>
						<tr><td><font face='tahoma' size='4'><a href='home.php' style='text-decoration:none;'>Home</a></font></td></tr>
						<tr><td background='line2.jpg' height='3'></td></tr>
						<tr><td><font face='tahoma' size='4' ><a href='createEmp.php' style='text-decoration:none;'>Create Personnel Record</a></font></td></tr>
						<tr><td background='line2.jpg' height='3'></td></tr>
						<tr><td><font face='tahoma' size='4' ><a href='search_view5.php' style='text-decoration:none;'>Edit Personnel Record</a></font></td></tr>
						<tr><td background='line2.jpg' height='3'></td></tr>
						<tr><td><font face='tahoma' size='4'color='silver'>Delete Personnel Record</font></td></tr>
						<tr><td background='line2.jpg' height='3'></td></tr>
						<tr><td><font face='tahoma' size='4' ><a href='search_view3.php' style='text-decoration:none;'>View Personnel Record</a></font></td></tr>
						<tr><td background='line2.jpg' height='3'></td></tr>
						<tr><td><font face='tahoma' size='4'><a href='sign.php' style='text-decoration:none;'>Logout</a></font></td></tr>
					</table>

				
				</td>
				<td><img src='header5.jpg' align='center'/></td>
			</tr>
			<tr><td height='10'></td></tr>
			<tr>
				<td colspan='2' background='line.jpg' height='8'></td>
			</tr>
			<tr>
				<td colspan='2' background='line2.jpg' height='2'></td>
			</tr>
			<tr>
				<td colspan='2' height='220' valign='top'>

					

					</br>
					&nbsp;&nbsp;&nbsp;
					<font face='tahoma' size='5' color='gray'>Record Has Been Deleted!</font>
					<br>
			</td>
			</tr>		
			<tr>
				<td></br></br></br></br></td>
			</tr>
			<tr>
				<td colspan='2' background='line2.jpg' height='2'></td>
			</tr>
		</table>	
	</body>
</html>

I have checkboxes in my HTML form and i want to delete several records from my database using those checkboxes. I'm quite new to php so I have a dificulty in manipulating the checkboxes. so can anyone help me? thank you!
Example....

I have 5 records and i want to delete two. So i'll check 2 checkboxes and then "submit" it. how do i do this in php? making sure that the records were really deleted in my database.

Assuming that you have the checkboxes named as deletethis[] where the indexes are the indexes in the table, on submitting the form with check box include a code like this. DeleteFunction is a function which deletes the row with id from a specific table.

$chk=$_POST['deletethis'];
$iserror=false;
for($i=0;$i<count($_POST['deletethis']);$i++){
     $result=DeleteFunction(key($chk));
     if(!is_numeric($result)){
         $iserror=true;
         break;
     }
     next($chk);
}

if u having any problems ask

Just a tip:

If you are going to use the checkboxes on each record, you cannot use another form with in that table.

Start the <form> at the top and insert a checkbox on each row... then add a submit button at the to and/or bottom of the table and close that form.

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.