Page 1 of 1

Inserting and Retrieving Checkbox data from mySQL with PHP

Posted: Sat Aug 24, 2002 11:52 am
by blkthndr
I am attempting to put in some checkbox info and pull out the values without much success. I have worked with text info just fine in the past and it goes in and comes out fine for me with the example below, however the checkbox info does not display anything.

Checkbox field:

Code: Select all

<tr>
			<td colspan="1" class="regularright">
			Years Applicable:
			</td>
			<td  colspan="6" class="regular">
			<input type="checkbox" name="yearsapp&#1111;]" value="1989"> 1989
			<input type="checkbox" name="yearsapp&#1111;]" value="1990"> 1990
			<input type="checkbox" name="yearsapp&#1111;]" value="1991"> 1991
			<input type="checkbox" name="yearsapp&#1111;]" value="1992"> 1992
			<input type="checkbox" name="yearsapp&#1111;]" value="1993"> 1993	
			<input type="checkbox" name="yearsapp&#1111;]" value="1994"> 1994
			<input type="checkbox" name="yearsapp&#1111;]" value="1995"> 1995													
			</td>
		</tr>
My insert:

Code: Select all

mysql_connect(localhost,$username,$password);
				@mysql_select_db($database) or die( "Unable to select database"); 		
		
				$sql = "INSERT INTO parts (partnumber, partdescription, yearsapp) VALUES ('$partnumber','$partdescription','$yearsapp')";
			  	$result = mysql_query($sql);
I use phpmyAdmin and can see the years app field listed as "Array".

My query to pull it out:

Code: Select all

mysql_connect(localhost,$username,$password);
		@mysql_select_db($database) or die( "Unable to select database"); 
		
		$query = "SELECT * FROM parts";
		$result = mysql_query($query) or die("Invalid query");
		$num_results = mysql_num_rows($result);
		
		for ($i=0; $i <$num_results; $i++)
  		&#123;
    		$row = mysql_fetch_array($result);
			if ($i % 2)
			&#123;
				echo "<tr bgcolor="E2E2EB"><td valign="top" class="regular">".stripslashes($row&#1111;"partnumber"]); // partnumber
			&#125;
			else
			&#123;
				echo "<tr bgcolor="B8B8CE"><td valign="top" class="regular">".stripslashes($row&#1111;"partnumber"]);  // partnumber
			&#125;
			echo "</td><td valign="top" class="regular">".stripslashes($row&#1111;"partdescription"]);
			echo "</td><td valign="top" class="regular">".stripslashes($row&#1111;"yearsapp"]);

	  	&#125;
Any help here is appreciated.

Thanks in advance.

Posted: Sat Aug 24, 2002 1:44 pm
by volka
try this

Code: Select all

<html><body>
<?php
	if (isset($_POST&#1111;'yearsapp']))
	&#123;
		print('print($_POST&#1111;''yearsapp'']): '); print($_POST&#1111;'yearsapp']);
		print('<pre>print_r($_POST&#1111;''yearsapp'']): '); print_r($_POST); print('</pre>');
		print ('imploded array:');
		print ( implode(',', $_POST&#1111;'yearsapp']) );
	&#125;
	else
		print('no data submitted yet');

?>
<form method="POST">
	<table>
	<tr> 
		<td colspan="1" class="regularright"> 
		Years Applicable: 
		</td> 
		<td  colspan="6" class="regular"> 
			<input type="checkbox" name="yearsapp&#1111;]" value="1989"> 1989 
			<input type="checkbox" name="yearsapp&#1111;]" value="1990"> 1990 
			<input type="checkbox" name="yearsapp&#1111;]" value="1991"> 1991 
			<input type="checkbox" name="yearsapp&#1111;]" value="1992"> 1992 
			<input type="checkbox" name="yearsapp&#1111;]" value="1993"> 1993    
			<input type="checkbox" name="yearsapp&#1111;]" value="1994"> 1994 
			<input type="checkbox" name="yearsapp&#1111;]" value="1995"> 1995                                        
		</td> 
	</tr>
	<tr><td><input type="submit"/></td></tr>
	</table>
</form>
</body></html>
$sql = "INSERT INTO parts (partnumber, partdescription, yearsapp) VALUES ('$partnumber','$partdescription','$yearsapp')";
this will replace $yearsapp by it's string representation and for variables of the type array this is "Array" not the contents of it

manual: implode()

Thanks

Posted: Sat Aug 24, 2002 3:09 pm
by blkthndr
I inserted this:

Code: Select all

$yearsapp = implode(', ', $_POST&#1111;'yearsapp']);
right before my insert command and it works just like I want it to work.

Thanks!