Inserting Checkbox Values using Arrays in Database

I'm currently doing a reservation system for my thesis, but I stil

Page 1 of 1

3 Replies - 9895 Views - Last Post: 17 March 2009 - 01:13 PM Rate Topic: -----

#1 mikxxx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 18-November 08

Inserting Checkbox Values using Arrays in Database

Post icon  Posted 15 March 2009 - 04:08 PM

I was the one who posted "Updating my Records", so far I've made it, thanks for the help of everyone who replied there.

So here goes my major problem..

I'm currently making a reservation system for my thesis and it's already on Wednesday but I haven't finished the program yet so I'm kindly asking for your help here please, I'd really appreciate your help.

I can't seem to make it work.. :( It's not yet complete, but I'd really like to get the idea of inserting the checkbox value in the database.

THe values there are from the service table, now, when it's checked it's going to be inserted into the reservation table..

<?php 
// Connects to your Database 
mysql_connect("localhost", "root", "") or die(mysql_error()); 
mysql_select_db("artista_salon") or die(mysql_error()); 


//checks cookies to make sure they are logged in 
if(isset($_COOKIE['ID_my_site'])) 
{ 
$username = $_COOKIE['ID_my_site']; 
$pass = $_COOKIE['Key_my_site']; 
$check = mysql_query("SELECT * FROM salon WHERE username = '$username'")or die(mysql_error()); 
while($info = mysql_fetch_array( $check )) 
{ 

//if the cookie has the wrong password, they are taken to the login page 
if ($pass != $info['password']) 
{ header("Location: member.php"); 
}

// now we insert it into the database
$insert = "INSERT INTO reservation (servicename, month, day, year, time, add_msg)
VALUES ('".$_POST['servicename']."', '".$_POST['month']."', '".$_POST['day']."', '".$_POST['year']."', '".$_POST['time']."', '".$_POST['add_msg']."')";
$add_reservation = mysql_query($insert);
else 
{ 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="style.css" />
<title>Artista Salon - Angeles</title>
<!--[if IE 6]>
<link rel="stylesheet" type="text/css" href="iecss.css" />
<![endif]-->
</head>

<body>

<div id="main_content"> 
  <!--Heading-->
  <div class="stars_head"> </div>
  <div id="header"> <a href="login.php"><img src="images/header02.gif" /></a> 
  </div>
  <div id="area"> <img src="images/angeles.gif" /> </div>
  <!--End of Heading-->
  <!--Part of Menu-->
  <div id="top_menu"> 
	<ul class="menu">
	  <li><a class="nav" href="login.php">home</a></li>
	  <li><a class="nav" href="services.php">services</a></li>
	  <li><a class="nav" href="promos.php">promos</a></li>
	  <li><a class="nav" href="reservation.php">reservation</a></li>
	  <li><a class="nav" href="gallery.php" >gallery</a></li>
	  <li><a class="nav" href="contact.php">contact</a></li>
	</ul>
  </div>
  <!--End of Menu-->
  <!--Right Content-->
  <div id="rightpart"> 
	<div id="right_part"> <img src="images/right_part.png" /> </div>
	<div class="reservationimg">&nbsp;</div>
  </div>
  <!-----------end of log in ------------------>
  <div id="body_content"> 
	<!--	<div id="reservebox">--><form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> 
	<table class="serv">
	  <tr> 
		<td>&nbsp; <h5>Hi 
			<?php 
							$con = mysql_connect("localhost","root","");
							if (!$con)
							  {
								  die('Could not connect: ' . mysql_error());
							  }
							mysql_select_db("artista_salon", $con);
							$result = mysql_query("SELECT * FROM salon WHERE username = '$username'" );
								while($row = mysql_fetch_array($result))
							  {
							  echo "<b>".$row['fname'] ."</b>";
							  }
							?>
			!!</h5></td>
	  </tr>
	  <tr> 
		<td> <label>What kind of services would you like to book?</label> </td>
	  </tr>
	</table>
	<tr> <table> 
	  <tr> 
		<td> 
		<td style="border: solid 1px; width:160px;"> <center>
			Hair Services 
			<hr />
		  </center>
		  <?php
if (isset($_POST['submit'])) { 
	
	
		?>
	<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
  
	  <?php 

							$result = mysql_query("SELECT * FROM service" );

								while($row = mysql_fetch_array($result))
							  {
							  echo "<input class=\"serv_radio\" type=\"checkbox\" name=\"servicename[]\">" . $row['servicename'];											  
							  echo "<br />";
							  }
						?>
	  <br />
	  <br /></td>
	  <td style="border: solid 1px; width:200px;"> <center>
		  Facial Care 
		  <hr />
		</center>
		<?php 

							$result = mysql_query("SELECT * FROM  facialservice" );

								while($row = mysql_fetch_array($result))
							  {
							  echo "<input class=\"serv_radio\" type=\"checkbox\" name=\"servicename[]\">" . $row['facialname'];																			echo "<br />";			
							  echo "<br />";
							  }
						?>
		<center>
		  Body Treatment 
		  <hr />
		</center>
		<?php 

							$result = mysql_query("SELECT * FROM  bodyservice" );

								while($row = mysql_fetch_array($result))
							  {
							  echo "<input class=\"serv_radio\" type=\"checkbox\" name=\"servicename[]\">" . $row['bodyservname'];											  
							  echo "<br />";
							  }
						?>


	  </td>
	  <td style="border: solid 1px; width:160px;"> <center>
		  Hand & Foot
		  <hr />
		</center>
		<?php 

							$result = mysql_query("SELECT * FROM  handfootservice" );

								while($row = mysql_fetch_array($result))
							  {
							  echo "<input class=\"serv_radio\" type=\"checkbox\" name=\"servicename[]\">" . $row['hfservicename'];											  
							  echo "<br />";
							  }
						?>
						
	  </td></td>
	  </tr> </table> </tr> <br />
	  <table class="serv">
		<tr> 
		  <td> <label>Preferred Date &nbsp;</label> <select  name="month">
			  <option value="01">Jan </option>
			  <option value="02">Feb </option>
			  <option value="03">Mar </option>
			  <option value="04">Apr </option>
			  <option value="05">May </option>
			  <option value="06">Jun </option>
			  <option value="07">Jul </option>
			  <option value="08">Aug </option>
			  <option value="09">Sep </option>
			  <option value="10">Oct </option>
			  <option value="11">Nov </option>
			  <option value="12">Dec </option>
			</select> <select name="day" >
			  <script language = "javascript">
									 <!--
 										for(var i=1;i<=31;++i)
											document.write("<option value = "+ i + ">"+ i + "</option>");	
			   						 //-->
								  </script>
			</select> <select  name="year">
			  <script language = "javascript">
									 <!--
							 			for(var i=2008;i<=2010;++i)
											document.write("<option value = "+ i + ">"+ i + "</option>");
									//-->
								  </script>
			</select> </td>
		</tr>
		<tr> 
		  <table>
			<tr> 
			  <td> 
			  <td> <label>Preferred Time</label> <br /> <input class="checkbox_time" type="checkbox" name="time" size="40px" />
				&nbsp;&nbsp;&nbsp;9:00am <br /> <input class="checkbox_time" type="checkbox" name="time" size="40px" />
				&nbsp;&nbsp;&nbsp;10:00am <br /> <input class="checkbox_time" type="checkbox" name="time" size="40px" />
				&nbsp;&nbsp;&nbsp;11:00am <br /> <input class="checkbox_time" type="checkbox" name="time" size="40px" />
				&nbsp;&nbsp;&nbsp;12:00nn <br /> <input class="checkbox_time" type="checkbox" name="time" size="40px" />
				&nbsp;&nbsp;&nbsp;1:00pm <br /> <input class="checkbox_time" type="checkbox" name="time" size="40px" />
				&nbsp;&nbsp;&nbsp;2:00pm <br /> <input class="checkbox_time" type="checkbox" name="time" size="40px" />
				&nbsp;&nbsp;&nbsp;3:00pm <br /> </td>
			  <td> <input class="checkbox_time" type="checkbox" name="time" size="40px" />
				&nbsp;&nbsp;&nbsp;4:00pm <br /> <input class="checkbox_time" type="checkbox" name="time" size="40px" />
				&nbsp;&nbsp;&nbsp;5:00pm <br /> <input class="checkbox_time" type="checkbox" name="time" size="40px" />
				&nbsp;&nbsp;&nbsp;6:00pm <br /> <input class="checkbox_time" type="checkbox" name="time" size="40px" />
				&nbsp;&nbsp;&nbsp;7:00pm <br /> <input class="checkbox_time" type="checkbox" name="time" size="40px" />
				&nbsp;&nbsp;&nbsp;8:00pm <br /> <input class="checkbox_time" type="checkbox" name="time" size="40px" />
				&nbsp;&nbsp;&nbsp;9:00pm <br /> </td></td>
			</tr>
		  </table>
		</tr>
		<tr> 
		  <td> <input class="sub" type="reset" value="Reset" name="reset" /> <input class="sub" type="submit" value="Submit" name="submit"> 
								  <?php
									}
										else
											{
													foreach($_POST['servicename'] as $ok)
														{
															echo $ok.'<br>';
														}
	
									}?>
		  </td>
		</tr>
	  </table><div id="addmsg_box">
	  <div class="addmsg_inbox"> 
	  <table class="addmsg">
		<tr class="addmsg"> 
		  <td class="addmsg"> Contact </td>
		</tr>
		<tr class="addmsg"> 
		  <td class="addmsg"> <input class="contact_in" type="text" size="20px" border="1px" name="name" value="phone number" /> 
		  </td>
		</tr>
		<tr class="addmsg"> 
		  <td class="addmsg"> <input class="contact_in" type="text" name="text" value="email" /> 
		  </td>
		</tr>
		<tr class="addmsg"> 
		  <td class="addmsg"> Additional Message </td>
		</tr>
		<tr class="addmsg"> 
		  <td class="addmsg"> <textarea name="add_msg" wrap="physical" ></textarea> 
		  </td>
		</tr>
	  </table>
	</form>

  </div>
</div>   

	<div id="reserve_footer">
 			<marquee behavior="scroll" direction="left">ARTISTA SALON - ANGELES</marquee>
	</div>
	
	<div class="footerlinks">
		<a class="footlinks" href="member.php">about us</a>
		 ||
		  <a class="footlinks" href="services.php">services</a>
		 ||
		 <a class="footlinks" href="promos.php">promos</a>
		 ||
		 <a class="footlinks" href="reservation.php">reservation</a>
		 ||
		 <a class="footlinks" href="gallery.php">gallery</a>		 
		 ||
		 <a class="footlinks" href="contact.php">contact</a>
		 <br />
		 <font class="copyright">&copy; Artista Salon - Angeles Branch 2009</font>
	</div>  
</body>
</html>
<?php
}
}
}
?>


Is This A Good Question/Topic? 0
  • +

Replies To: Inserting Checkbox Values using Arrays in Database

#2 dgersting  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 23-February 09

Re: Inserting Checkbox Values using Arrays in Database

Posted 16 March 2009 - 01:49 PM

I'm not sure exactly what your wanting returned from the check boxes, but my guess is you are wanting an array? Meaning that if the user checks 1 and 3 pm, you want an array of
Array(
   [0] = 1:00pm
   [1] = 3:00pm
)



If this correct you need to modify your check boxes to be like
<input class="checkbox_time" type="checkbox" name="time[]" size="40px" />

the [] at the end of the field name tells the browser to POST/GET the vales of the check box (all of them) as an array

Then, for your SQL; (Assuming you don't want/need anything special)
INSERT INTO reservation (servicename, month, day, year, time, add_msg) VALUES ('".$_POST['servicename']."', '".$_POST['month']."', '".$_POST['day']."', '".$_POST['year']."', '".implode(",",$_POST['time'])."', '".$_POST['add_msg']."')";



I hope that's what you were wanting.

This post has been edited by dgersting: 16 March 2009 - 01:57 PM

Was This Post Helpful? 0
  • +
  • -

#3 mikxxx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 18-November 08

Re: Inserting Checkbox Values using Arrays in Database

Posted 16 March 2009 - 04:17 PM

[font=Century Gothic]@dgersting:
Thanks! I have it now inserted to the database. But the problem is, if I check 4 boxes, the records in the database will have 4 same records. :\

							  <?php
									}
										else
											{
													foreach($_POST['servicename'] as $ok)
														{
															// now we insert it into the database
															$insert = "INSERT INTO reservation (servicename, month, day, year,time)
															VALUES ('".implode(",",$_POST['servicename'])."','".$_POST['month']."','".$_POST['day']."','".$_POST['year']."','".$_POST['time']."')";
															$add_reservation = mysql_query($insert);
															echo $ok.'<br>';
															
														}
	
									}
							   ?>

This post has been edited by mikxxx: 16 March 2009 - 05:32 PM

Was This Post Helpful? 0
  • +
  • -

#4 dgersting  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 23-February 09

Re: Inserting Checkbox Values using Arrays in Database

Posted 17 March 2009 - 01:13 PM

If you are wrapping the database call with a foreach, then you don't need the implode.
The implode is essentially a foreach loop which puts all the nodes of the passed array into a string.
inplode() documentation

For your code;

View Postmikxxx, on 16 Mar, 2009 - 05:17 PM, said:

							  <?php
									}
										else
											{
													foreach($_POST['servicename'] as $ok)
														{
															// now we insert it into the database
															$insert = "INSERT INTO reservation (servicename, month, day, year,time)
															VALUES ('".implode(",",$_POST['servicename'])."','".$_POST['month']."','".$_POST['day']."','".$_POST['year']."','".$_POST['time']."')";
															$add_reservation = mysql_query($insert);
															echo $ok.'<br>';
															
														}
	
									}
							   ?>



You should have;
<?php
 }
 else{
  foreach($_POST['servicename'] as $ok)
   {
	// now we insert it into the database
	$insert = "INSERT INTO reservation (servicename, month, day, year,time) VALUES 
				 ('".$ok."','".$_POST['month']."','".$_POST['day']."','".$_POST['year']."','".$_POST['time']."')";
	$add_reservation = mysql_query($insert);
	echo $ok.'<br>';
  }
}
?>


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1