Hi, actually i've tried the simple checkbox code but it doesn't work. My point is want to insert the selected checkbox value into the same database row.

Example : one,three --> if there are selected.

This is my code. Anybody can help me?

<?php
include 'connection/db_connect.php';

if(isset($one))
 $val1=$one.";";
 else
 $val1=NULL;

if(isset($two))
 $val2=$two.";";
 else
 $val2=NULL;
 
if(isset($three))
  $val3=$three.";";
  else
 $val3=NULL;
 
$printVal=$val1."".$val2."".$val3;

$val=array($printVal);


if(isset($_POST['Submit'])) {

    if($error ==0){
	
	
	foreach($val as $values)
	{
		
	
	if(!empty($values)){	
	$sql="INSERT INTO `request` (detail) values('$values')";
        
    mysql_query($sql);}
	
if (mysql_error()) trigger_error(mysql_error());
	}
} }?>

<html>
<body>
<form name="frm" method="POST" action="<?php $_SERVER['PHP_SELF']; ?> "  enctype="multipart/form-data">
<input name="one" type="checkbox" value="$one" /> One
<input name="two" type="checkbox" value="$two" /> Two
<input name="three" type="checkbox" value="$three" /> Three
<input type="submit" name="submit" value="submit">
</form>

</body>
</html>

the result of your form will be in $_POST[ ]

if(isset($_POST['one']))
      $val='one;';
else
      $val=NULL;

if(isset($_POST['two']))
      $val.='two;';

if(isset($_POST['tree']))
      $val.='tree;';

you now have a string $val : one;three;
just put it into your database

if(!empty($val)){
   $sql="INSERT INTO `request` (detail) values('$val')";
   mysql_query($sql)or die(mysql_error());
   }

yaaa!! thanks to pzuurveen.. i got it! Now my problem is solved. Thank you for the help.

My point is want to insert the selected checkbox value into the same database row.

Example : one,three --> if there are selected.

I think it's worth pointing out that this is a very bad idea. The first rule of relational database design is to: Never store repeating groups of data.

What that means is:

  1. Don't put more than one value into a single field.
  2. Don't put more than one identical column into a table. (Such as "phone1", "phone2", etc...)

Ideally you would want to create one "request" table, and one "request_details" table. The first table is to store each request, and the second is to store each checkbox selected for that request. Something like:

+---------+     +---------------------+
| request |     | request_details     |
+---------+     +---------------------+
| id (PK) |1---*| request_id (FK, PK) |
| etc...  |     | detail (PK)         |
+---------+     | etc...              |
                +---------------------+

The PHP code to work with that could look something like:

<?php
// Stores the " checked" attribute for the boxes that are
// selected. Will be filled in in the for loop.
$checkedBoxes = array("", "", "");

// What data do you *need* to be set for this to work? 
// The submit button? NO! The checkboxes.
// My point: Don't check if the submit button is
// set, make sure the *data* is set.
if (isset($_POST["checks"])) 
{
    // Stores the values for the selected checkboxes, the once
    // we want inserted into the database.
    $selectedvalues = array();
    
    // We have three boxes, so we loop three times.
    for ($i = 0; $i < 3; ++$i) 
    {
        // See if the user selected this checkbox.
        if (isset($_POST["checks"][$i])) 
        {
            // Fetch the value for the database.
            $selectedvalues[] = mysql_real_escape_string($_POST["checks"][$i]);
            
            // Set the "checked" attribute on the checkbox
            // in the form.
            $checkedBoxes[$i] = " checked";
        }
    }
    
    // Only proceed if there is anything to add to the database.
    if ($count($selectedvalues) > 0) 
    {
        // Insert the request. I assume there is more data
        // that you just left out?
        $sql = "INSERT INTO `request`() VALUES()";
        $result = mysql_query($sql);
        if (!$result || mysql_affected_rows($result) != 1)
            trigger_error("Failed to insert request: " . mysql_error(), E_USER_ERROR);
        
        $requestID = mysql_insert_id();
        
        // And then add each checkbox value for the request to the
        // request_details table.
        $sql = "INSERT INTO `request_details`(`request_id`, `value`)
                VALUES (%d, '%s')";
        
        // We want ONE ROW FOR EACH OPTION, so we loop through
        // each of them. To link the options to the request, we
        // set the "requestID" as well. (A Foreign Key.)
        foreach ($selectedValues as $value) {
            $result = mysql_query(sprintf($sql, $value)) or die(mysql_error());
            if (!$result || mysql_affected_rows($result) != 1)
                trigger_error("Failed to insert request option: " . mysql_error(), E_USER_ERROR);
        }
    }
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>Example</title>
    <meta charset="UTF-8"/>
</head>
<body>
    <form name="frm" method="POST" action="<?php $_SERVER['PHP_SELF']; ?> "  enctype="multipart/form-data">
        <input name="checks[]" type="checkbox" value="one"<?php echo $checkedBoxes[0]; ?>> One
        <input name="checks[]" type="checkbox" value="two"<?php echo $checkedBoxes[1]; ?>> Two
        <input name="checks[]" type="checkbox" value="three"<?php echo $checkedBoxes[2]; ?>> Three
        <input type="submit" name="submit" value="submit">
    </form>
</body>
</html

Note that the value attribute of a <input type="checkbox"> does not determine it's state. It's essentially a static value passed only if the box is checked, and ignored if it's not. - To have a checkbox initially checked, add the checked attribute to it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.