User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 396,968 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,955 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 306 | Replies: 4
Reply
Join Date: May 2008
Posts: 3
Reputation: charvie is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
charvie charvie is offline Offline
Newbie Poster

Checkboxes

  #1  
May 3rd, 2008
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.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2005
Location: New York state
Posts: 453
Reputation: ShawnCplus will become famous soon enough ShawnCplus will become famous soon enough 
Rep Power: 5
Solved Threads: 66
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey

Re: Checkboxes

  #2  
May 3rd, 2008
  1. <input type='checkbox' name='someArray[]' value='1' />
  2. <input type='checkbox' name='someArray[]' value='2' />
  3. <input type='checkbox' name='someArray[]' value='3' />

Then in PHP
  1. $someArray = $_REQUEST['someArray'];
  2. foreach($someArray as $key=>$someElem){
  3. $query = "DELETE FROM someTable WHERE id = ".$someElem;
  4. }
GCS d- s+:+ a-->? C++(++++) UL+++ P+>+++ L+++ !E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r z+*
Reply With Quote  
Join Date: May 2008
Posts: 3
Reputation: charvie is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
charvie charvie is offline Offline
Newbie Poster

Re: Checkboxes

  #3  
May 4th, 2008
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>

Reply With Quote  
Join Date: Apr 2008
Posts: 5
Reputation: tver3305 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
tver3305 tver3305 is offline Offline
Newbie Poster

Re: Checkboxes

  #4  
May 5th, 2008
Originally Posted by charvie View Post
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.

  1. $chk=$_POST['deletethis'];
  2. $iserror=false;
  3. for($i=0;$i<count($_POST['deletethis']);$i++){
  4. $result=DeleteFunction(key($chk));
  5. if(!is_numeric($result)){
  6. $iserror=true;
  7. break;
  8. }
  9. next($chk);
  10. }

if u having any problems ask
Reply With Quote  
Join Date: Apr 2008
Posts: 43
Reputation: Daedal is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 11
Daedal's Avatar
Daedal Daedal is offline Offline
Light Poster

Re: Checkboxes

  #5  
May 6th, 2008
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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb PHP Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 8:35 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC