Ok, here's a quick script that should point you in at least one direction.
Here's the progression:
User submits form
Checks if form was submitted
Checks if any of the eating disorder boxes are checked
if yes, starts building the string needed in the mysql query later.
then finds which boxes were checked, and continues to build the SET string and VALUES string.
Removes the comma from the beginning of the strings (there are other ways around this)
Queries the database and inserts the SET and VALUES into the database.
You'll have to:
Set up a database table and replace "testtable" with your table's name
Your table should have all of the same names as the form components. If not, you should be able to see what you need to change to accomodate.
Look up how to connect to databases on your own.
Note: I did not add any protection against MYSQL injection, so before using this on a public server, make sure you read up on this subject.
<?php
//Turn on error reporting
error_reporting(E_ALL);
if(isset($_POST['Submit']))
{
//define variables
$set = "";
$values = "";
if (isset($_POST['ed_curr_anorexia']) OR
isset($_POST['ed_curr_bulimia']) OR
isset($_POST['ed_prior_anorexia']) OR
isset($_POST['ed_prior_bulimia']) )
{
$set .= ",ed";
$values .= ",'Yes'";
if (isset($_POST['ed_curr_anorexia']))
{
$set .= ",ed_curr_anorexia";
$values .= ",'Yes'";
}
if (isset($_POST['ed_curr_bulimia']))
{
$set .= ",ed_curr_bulimia";
$values .= ",'Yes'";
}
if (isset($_POST['ed_prior_anorexia']))
{
$set .= ",ed_prior_anorexia";
$values .= ",'Yes'";
}
if (isset($_POST['ed_prior_bulimia']))
{
$set .= ",ed_prior_bulimia";
$values .= ",'Yes'";
}
}
if ($set != "" && $values != "")
{
//remove comma from beginning of strings
$finalset = substr($set, 1, strlen($set));
$finalvalues = substr($values, 1, strlen($values));
// Connect to Database, look this up seperately to
// see how to do it.
include("connect.php");
// Insert Data into table, replace testtable
// with your table's name
@mysql_query("INSERT INTO testtable ($finalset)
VALUES ($finalvalues)")or die(mysql_error());
echo "Data was successfully entered";
}
}
else{
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Current:<br />
<input type=checkbox name=ed_curr_anorexia>Anorexia<br />
<input type=checkbox name=ed_curr_bulimia>Bulimia<br />
Prior: <br />
<input type=checkbox name=ed_prior_anorexia>Anorexia<br />
<input type=checkbox name=ed_prior_bulimia>Bulimia<br />
<input type=submit name=Submit>
</form>
<?php
}
?>
Note: I did not add any protection against MYSQL injection, so before using this on a public server, make sure you read up on this subject.