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 391,609 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,626 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: 855 | Replies: 8 | Solved
Reply
Join Date: Apr 2008
Posts: 31
Reputation: shadiadiph is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
shadiadiph shadiadiph is offline Offline
Light Poster

multiple checkboxes adding values to single sql column

  #1  
Jun 14th, 2008
Hi I have a form and I have a list of checkboxes I am trying to check 2 or all of them and get them to insert into one sql column in my database. When I select them it submits ok but in the cloumn on the database is storing it as Array not the values I have checked.

Here is the form and the code.


<form name="padform" method="post" action="catsubmit.php" />
<input name="userID" type="hidden" id="userID" value="<?=$userID?>">
    
<table width="540" cellspacing="3" cellpadding="0" border="0"> 
<tr>
<td width="300"  id="t_category1">
<input type="checkbox" name="vccategory[]" value="Quality Control &amp; Consultants">Quality Control &amp; Consultants</td></tr>
<tr>
<td><input type="checkbox" name="vccategory[]" value="Quilting Materials &amp; Supplies">Quilting Materials &amp; Supplies</td></tr>
<tr>
<td><input type="checkbox" name="vccategory[]" value="Quilts &amp; Quilting">Quilts &amp; Quilting</td></tr>
<tr>
<td><input name="submit" type="submit" class="button" value="Submit Ad" /><input class="button" type="reset" name="reset" value="Reset" /></td>
</tr>
</table>
</form>

the submit file

<? 
session_start();
error_reporting(apsolutions);
?>
<?PHP
	include("../secure/global/connection.php");
	include("../secure/global/mail.class.php");
	error_reporting(7);

            $vccategory             =    ($_POST['vccategory']);
		$intUserID    		=    ($_POST["userID"]);

		{
			$insertsql = " insert into tbladvertdetails (intUserID, vccategory, dtCreatedOn) values ('".addslashes($intUserID)."','".addslashes($vccategory)."','".date("Y-m-d h:m:s")."')";
			
			$DB_site->query($insertsql);
			$accountid = mysql_insert_id();
			
}
?>

		<script>
			alert("Profile UPdated")
			document.location.href="adcat.php"
		</script>

Any help with this wil be appreciated
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2008
Location: Phoenix, AZ
Posts: 534
Reputation: R0bb0b is on a distinguished road 
Rep Power: 2
Solved Threads: 50
R0bb0b's Avatar
R0bb0b R0bb0b is offline Offline
Posting Pro

Re: multiple checkboxes adding values to single sql column

  #2  
Jun 14th, 2008
right, because you are submitting it as an array and trying to enter it directly in the db that way.

When you say
$array = array("val1", "val2");
echo $array;
Your output will be just "array".

The following will process the array and create a comma separated list of selected values.
$vccategory = "";
foreach($_POST['vccategory'] as $value)
{
	$vccategory .= $value . ", ";
}
$vccategory = substr($vccategory, 0, -2); //to get rid of the final ", "
Last edited by R0bb0b : Jun 14th, 2008 at 11:09 am.
Reply With Quote  
Join Date: Apr 2008
Posts: 31
Reputation: shadiadiph is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
shadiadiph shadiadiph is offline Offline
Light Poster

Re: multiple checkboxes adding values to single sql column

  #3  
Jun 14th, 2008
Yes you are right i managed to just solved the problem myself quite simple really this is what i used it posts all the values to the database now here is the updated version of my script for others to use.

<form name="padform" method="post" action="catsubmit.php" />
<input name="userID" type="hidden" id="userID" value="<?=$userID?>">
    
<table width="540" cellspacing="3" cellpadding="0" border="0"> 
<tr>
<td width="300"  id="t_category1">
<input type="checkbox" name="vccategory[]" value="Quality Control &amp; Consultants">Quality Control &amp; Consultants</td></tr>
<tr>
<td><input type="checkbox" name="vccategory[]" value="Quilting Materials &amp; Supplies">Quilting Materials &amp; Supplies</td></tr>
<tr>
<td><input type="checkbox" name="vccategory[]" value="Quilts &amp; Quilting">Quilts &amp; Quilting</td></tr>
<tr>
<td><input name="submit" type="submit" class="button" value="Submit Ad" /><input class="button" type="reset" name="reset" value="Reset" /></td>
</tr>
</table>
</form>



the submit script

<? 
session_start();
error_reporting(apsolutions);
?>
 
<?PHP
	include("../secure/global/connection.php");
	include("../secure/global/mail.class.php");
	error_reporting(7);

            $vccategory             =    ($_REQUEST['vccategory']);
		$intUserID    		=    ($_POST["userID"]);

		{
			$insertsql = " insert into tbladvertdetails (intUserID, vccategory, dtCreatedOn) values ('".addslashes($intUserID)."','".addslashes($vccategory[0]).",".addslashes($vccategory[1]).",".addslashes($vccategory[2])."','".date("Y-m-d h:m:s")."')";
			
			$DB_site->query($insertsql);
			$accountid = mysql_insert_id();
			
}
?>

		<script>
			document.location.href="adcat.php"
		</script>

hope this helps someone in the future
Last edited by peter_budo : Jun 16th, 2008 at 8:16 am. Reason: Adding forgoten closing tag [/code]
Reply With Quote  
Join Date: Jun 2008
Location: Phoenix, AZ
Posts: 534
Reputation: R0bb0b is on a distinguished road 
Rep Power: 2
Solved Threads: 50
R0bb0b's Avatar
R0bb0b R0bb0b is offline Offline
Posting Pro

Re: multiple checkboxes adding values to single sql column

  #4  
Jun 14th, 2008
but how does your script determine count($array)?
Last edited by R0bb0b : Jun 14th, 2008 at 11:26 am.
Reply With Quote  
Join Date: Apr 2008
Posts: 31
Reputation: shadiadiph is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
shadiadiph shadiadiph is offline Offline
Light Poster

Re: multiple checkboxes adding values to single sql column

  #5  
Jun 14th, 2008
hi it doesn't but then i tried your script it didn't work? returned a parse error.

I only want the user to be able to select a maximum of 3 values but one thing that is a bit annoying is that when ithe user only selects 1 value it stores in the database like this Quilts,,
not sure how to get rid of the 2 commas? but at the end of the day it works.
Reply With Quote  
Join Date: Jun 2008
Location: Phoenix, AZ
Posts: 534
Reputation: R0bb0b is on a distinguished road 
Rep Power: 2
Solved Threads: 50
R0bb0b's Avatar
R0bb0b R0bb0b is offline Offline
Posting Pro

Re: multiple checkboxes adding values to single sql column

  #6  
Jun 14th, 2008
tested, works fine for me
$_POST['vccategory'] = array("one", "two");
$vccategory = "";
foreach($_POST['vccategory'] as $value)
{
	$vccategory .= $value . ", ";
}
$vccategory = substr($vccategory, 0, -2); //to get rid of the final ", "
echo $vccategory;
Reply With Quote  
Join Date: Jun 2008
Location: Phoenix, AZ
Posts: 534
Reputation: R0bb0b is on a distinguished road 
Rep Power: 2
Solved Threads: 50
R0bb0b's Avatar
R0bb0b R0bb0b is offline Offline
Posting Pro

Re: multiple checkboxes adding values to single sql column

  #7  
Jun 14th, 2008
<form name="padform" method="post" action="<? echo $_SERVER['REQUEST_URI']; ?>" />
<input name="userID" type="hidden" id="userID" value="<?=$userID?>">
    
<table width="540" cellspacing="3" cellpadding="0" border="0"> 
<tr>
<td width="300"  id="t_category1">
<input type="checkbox" name="vccategory[]" value="Quality Control &amp; Consultants">Quality Control &amp; Consultants</td></tr>
<tr>
<td><input type="checkbox" name="vccategory[]" value="Quilting Materials &amp; Supplies">Quilting Materials &amp; Supplies</td></tr>
<tr>
<td><input type="checkbox" name="vccategory[]" value="Quilts &amp; Quilting">Quilts &amp; Quilting</td></tr>
<tr>
<td><input name="submit" type="submit" class="button" value="Submit Ad" /><input class="button" type="reset" name="reset" value="Reset" /></td>
</tr>
</table>
</form>
<?
$vccategory = "";
foreach($_POST['vccategory'] as $value)
{
	$vccategory .= $value . ", ";
}
$vccategory = substr($vccategory, 0, -2); //to get rid of the final ", "
echo $vccategory;
?>
Reply With Quote  
Join Date: Apr 2008
Posts: 31
Reputation: shadiadiph is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
shadiadiph shadiadiph is offline Offline
Light Poster

Re: multiple checkboxes adding values to single sql column

  #8  
Jun 15th, 2008
sorry so how does that work it doesn't link to my submit to database script which is on another page
Reply With Quote  
Join Date: Jul 2007
Location: Devon UK
Posts: 1
Reputation: Yamthief is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Yamthief Yamthief is offline Offline
Newbie Poster

Re: multiple checkboxes adding values to single sql column

  #9  
Jun 19th, 2008
"sorry so how does that work it doesn't link to my submit to database script which is on another page"

<form ... action="<? echo $_SERVER['REQUEST_URI']; ?>" />

This submits the form back to the same URL you're currently viewing without destroying any variables that may be in the address bar...
(more info can be found here: http://blog.taragana.com/index.php/a...n-to-use-what/)

You just have to set the page to retrieve the variables from the POST.

<?
$vccategory = $_POST['vccategory']
?>

This will set the variable '$vccategory' to whatever form field was submitted with that name

eg:
<input type="text" name="vccategory" value="this is vccategory!" />
...would mean $vccategory = "this is vccategory!";

hope this helps
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

Other Threads in the PHP Forum

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