0

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

3
Contributors
8
Replies
13
Views
9 Years
Discussion Span
Last Post by Yamthief
0

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 ", "
0

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

0

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.

0

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;
0
<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;
?>
0

sorry so how does that work it doesn't link to my submit to database script which is on another page

0

"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; ?>" />

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/archive/understanding-_serverphp_self-php_self-_serverrequest_uri-and-_serverscript_name-in-php-and-when-to-use-what/)

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

<?
$vccategory = $_POST
?>

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 :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.