close ad
WARNING: Do Not Install the DREAMWEAVER CC 2017 or 2018 Update »
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

rating

Php code for inserting group checkbox data into the database

Thread began 3/16/2011 9:39 am by sandeep.kesarwani423749 | Last modified 11/26/2014 2:50 am by Nico | 2063 views | 4 replies |

sandeep.kesarwani423749

Php code for inserting group checkbox data into the database

Can anyone plz tell me what php code should be used to insert checkbox group data into the database. I am trying to insert left or right value of placement field. the code is as follows,


<label>
<input type="radio" name="placement[]" value="L" id="placement" />
Left</label>
<br />
<label>
<input type="radio" name="placement[]" value="R" id="placement" />
Right</label>

and for inserting:

if (isset($_POST["placement"]))?implode(", ", $_POST["placement"]):"") {
$placement = array($_POST['placement']);
$placement1 = explode(",", $placement);
}

$qry = "INSERT INTO `member`.`test` (`userid`, `placement`)
VALUES ('$userid', '$placement1');";

$result = @mysql_query($qry);

Can you plz provide me with full code.

Sign in to reply to this post

Jason ByrnesWebAssist

in the insert record behavior, select the column this data should be stored to, and use the following code:

php:
<?php echo ((isset($_POST["placement"]))?implode(", "$_POST["placement"]):""); ?>
Sign in to reply to this post

Nico

Put array data in seperate columns

This works when we save data in one column. How can I save each array value in separate columns.
I have a checkbox of four values (1,2,3,4). I want to save each selected checkbox in a different column and not all in one.

Sign in to reply to this post

Jason ByrnesWebAssist

you would need to hand code a loop to loop through the array, and use an insert to inner teach array piece, this will take hand coding to accomplish.

Here is a very simplistic example that shows how to do this;

<?php require_once('Connections/localhost.php'); ?>
<?php require_once("webassist/database_management/wa_appbuilder_php.php"); ?>
<?php
//if statement to make sure the checkbox group has a value
if(isset($_POST["CheckboxGroup1"])) {
//foreach loop added to loop through checkbox array
foreach($_POST["CheckboxGroup1"] as $k => $v) {
?>
<?php
// WA DataAssist Insert
if ($_SERVER["REQUEST_METHOD"] == "POST") // Trigger
{
$WA_connection = $localhost;
$WA_table = "options";
$WA_sessionName = "OptionID";
$WA_redirectURL = "";
if (function_exists("rel2abs")) $WA_redirectURL = $WA_redirectURL?rel2abs($WA_redirectURL,dirname(__FILE__)):"";
$WA_keepQueryString = false;
$WA_indexField = "OptionID";
$WA_fieldNamesStr = "OptionName";
// original biunding for the checkbox form element
//$WA_fieldValuesStr = "".((isset($_POST["CheckboxGroup1"]))?$_POST["CheckboxGroup1"]:"") ."";
// edited to use the $v variable created by the for each loop
$WA_fieldValuesStr = "".($v) ."";
$WA_columnTypesStr = "',none,''";
$WA_fieldNames = explode("|", $WA_fieldNamesStr);
$WA_fieldValues = explode($WA_AB_Split, $WA_fieldValuesStr);
$WA_columns = explode("|", $WA_columnTypesStr);
$WA_connectionDB = $database_localhost;
mysql_select_db($WA_connectionDB, $WA_connection);
@session_start();
$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());
$_SESSION[$WA_sessionName] = mysql_insert_id();
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);
}
}
?>
<?php
} //end foreach
} // end if
?>
<!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" />
<title>Untitled Document</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
<p>
<label>
<input type="checkbox" name="CheckboxGroup1[]" value="Item 1" id="CheckboxGroup1_0" />
Item 1</label>
<br />
<label>
<input type="checkbox" name="CheckboxGroup1[]" value="Item 2" id="CheckboxGroup1_1" />
Item 2</label>
<br />
<br />
<label>
<input type="submit" name="button" id="button" value="Submit" />
</label>
<br />
</p>
</form>
</body>
</html>



if you need more assistance, were can help through a premiere support appointment:
http://www.webassist.com/premier_request.php

Sign in to reply to this post

Nico

Thank you

Thank you for the code.
I accomplished it another way
Named my database columns for each answer like this
answer_1,answer_2,answer_3

My checkboxes have values 1,2,3 for each option.
So when I save the data in the database i add the checkbox value as a parameter to the column name that it will be saved and I get something like this.

$InsertQuery->bindColumn("answer_".$_POST['optionvalue'][0]."", "i", "".((isset($_POST['optionvalue'][0]))?$_POST['optionvalue'][0]:"") ."", "WA_DEFAULT");
$InsertQuery->bindColumn("answer_".$_POST['optionvalue'][1]."", "i", "".((isset($_POST['optionvalue'][1]))?$_POST['optionvalue'][1]:"") ."", "WA_DEFAULT");
$InsertQuery->bindColumn("answer_".$_POST['optionvalue'][2]."", "i", "".((isset($_POST['optionvalue'][2]))?$_POST['optionvalue'][2]:"") ."", "WA_DEFAULT");



My checkboxes are like this

<input type="checkbox" name="optionvalue[]" id="optionvalue_1" class="optionvalueclass" value="1">
a) first answer</dd>

<input type="checkbox" name="optionvalue[]" id="optionvalue_2" class="optionvalueclass" value="2">
b) second answer</dd>

<input type="checkbox" name="optionvalue[]" class="optionvalueclass" id="optionvalue_3" value="3">
c) third answer


So what it happens when I submit the form, the query adds the data depending on the values of the checkboxes user has checked

Sign in to reply to this post

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question

Content

rating

Layout

rating

Ease of use

rating

security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.

Close Windowclose

We were unable to retrieve the attached file

Close Windowclose

Attach and remove files

add attachmentAdd attachment
Close Windowclose

Enter the URL you would like to link to in your post

Close Windowclose

This is how you use right click RTF editing

Enable right click RTF editing option allows you to add html markup into your tutorial such as images, bulleted lists, files and more...

-- click to close --

Uploading file...