PDA

View Full Version : Checkbox Group to MySQL db


Les Crowley
09-03-2009, 11:35 AM
I have a dynamic checkbox group where I want to allow multiple selections of the checkboxes to be made and written to the database. The code is pasted below.

The problem it's having is that it is only writing a one (the last checked) of the choices to the database - even when there are multiples checked.

I assume this can be accomplished just not sure how.

<?php do { ?>
<input type="checkbox" name="gallerycategories" value="<?php echo $row_artcategories['Category']; ?>" id="CheckboxGroup1_0">
<?php echo $row_artcategories['Category']; ?>
<br>

<?php } while ($row_artcategories = mysql_fetch_assoc($artcategories)); ?>

Jason Byrnes
09-03-2009, 04:33 PM
Use the DataAssist Insert Multiple records server behavior. This will allow you to store a record for each selected checkbox.

Les Crowley
09-03-2009, 06:35 PM
I thought there was a way using an array to store all the checkbox data as a single record that's comma separated?

Essentially once stored I want to use the data to filter a recordset of chosen art categories to display.

Dave Buchholz
09-04-2009, 01:40 AM
Les,

there is a tutorial here: http://www.plus2net.com/php_tutorial/checkbox-value.php that might help you

Les Crowley
09-04-2009, 07:06 AM
Thanks Dave,

Got it worked out.

And instead of trying to write the resulting variable to the db - I write the implode line and it writes the array values to a single record in the database.

Next step to use that to filter a recordset and return only those categories for use as gallery navigation.

<?php do { ?>
<input type="checkbox" name="$galcats[]" value="<?php echo $row_artcategories['Category']; ?>" id="$galcats[]">
<?php echo $row_artcategories['Category']; ?> <br>
<?php } while ($row_artcategories = mysql_fetch_assoc($artcategories)); ?>
<?PHP
if (isset($_POST['$galcats'])) {
$gallerycats = implode(' ',$_POST['$galcats']);
$_SESSION['galcats'] = $gallerycats;
} else {
$gallerycats = '';
}
?>

Les Crowley
09-04-2009, 01:41 PM
Jason,

I've decided to try it via DataAssist multiple insert and I've gotten the DA repeat region in place (along with the DW repeat region) and applied the DA Multiple Insert but it's not inserting the checked values into the db... am I missing something?

<?php do { ?>
<?php
// RepeatSelectionCounter_1 Begin Loop
$RepeatSelectionCounter_1_IterationsRemaining = $RepeatSelectionCounter_1_Iterations;
while($RepeatSelectionCounter_1_IterationsRemainin g--){
if($RepeatSelectionCounterBasedLooping_1 || $row_artcategories){
?>
<tr>
<td>
<input type="checkbox" name="$galcats_<?php echo $RepeatSelectionCounter_1; ?>" value="<?php echo $row_artcategories['Category']; ?>" id="$galcats_<?php echo $RepeatSelectionCounter_1; ?>">
<?php echo $row_artcategories['Category']; ?></td>
</tr>
<?php
} // RepeatSelectionCounter_1 Begin Alternate Content
else{
?>
<tr>
<td>&nbsp;</td>
</tr>
<?php } // RepeatSelectionCounter_1 End Alternate Content
if(!$RepeatSelectionCounterBasedLooping_1 && $RepeatSelectionCounter_1_IterationsRemaining != 0){
if(!$row_artcategories && $RepeatSelectionCounter_1_Iterations == -1){$RepeatSelectionCounter_1_IterationsRemaining = 0;}
$row_artcategories = mysql_fetch_assoc($artcategories);
}
$RepeatSelectionCounter_1++;
} // RepeatSelectionCounter_1 End Loop
?>
<?php } while ($row_artcategories = mysql_fetch_assoc($artcategories)); ?>



<?php
// WA DataAssist Multiple Inserts
if ($_SERVER["REQUEST_METHOD"] == "POST") // Trigger
{
if (!session_id()) session_start();
$WA_loopedFields = array("$galcats");
$WA_connection = $rockncom;
$WA_table = "retailcategories";
$WA_redirectURL = "";
$WA_keepQueryString = false;
$WA_fieldNamesStr = "artcategory";
$WA_columnTypesStr = "',none,''";
$WA_insertIfNotBlank = "";
$WA_fieldNames = explode("|", $WA_fieldNamesStr);
$WA_columns = explode("|", $WA_columnTypesStr);
$WA_connectionDB = $database_rockncom;
$WA_multipleInsertCounter = 0;
mysql_select_db($WA_connectionDB, $WA_connection);
while (WA_AB_checkLoopedFieldsExist($WA_loopedFields, $WA_multipleInsertCounter)) {
if ($WA_insertIfNotBlank == "" || WA_AB_checkLoopedFieldsNotBlank(array($WA_insertIf NotBlank), $WA_multipleInsertCounter)) {
$WA_fieldValuesStr = "".WA_AB_getLoopedFieldValue("$galcats", $WA_multipleInsertCounter) ."";
$WA_fieldValues = explode("|", $WA_fieldValuesStr);
$insertParamsObj = WA_AB_generateInsertParams($WA_fieldNames, $WA_columns, $WA_fieldValues, -1);
$WA_Sql = "INSERT INTO `" . $WA_table . "` (" . $insertParamsObj->WA_tableValues . ") VALUES (" . $insertParamsObj->WA_dbValues . ")";
$MM_editCmd = mysql_query($WA_Sql, $WA_connection) or die(mysql_error());
}
$WA_multipleInsertCounter++;
}
if ($WA_redirectURL != "") {
if ($WA_keepQueryString && $WA_redirectURL != "" && isset($_SERVER["QUERY_STRING"]) && $_SERVER["QUERY_STRING"] !== "" && sizeof($_POST) > 0) {
$WA_redirectURL .= ((strpos($WA_redirectURL, '?') === false)?"?":"&").$_SERVER["QUERY_STRING"];
}
header("Location: ".$WA_redirectURL);
}
}
?>

Jason Byrnes
09-08-2009, 01:48 PM
your checkbox name is starting with a dollar sign:
name="$galcats_

form elements names can contain only alpha numeric characters, the only exception is the underscore.