i have four checkboxes, user have option to select all or any of them as per the nature of complaint. the html code is as follow....

<input type="checkbox" name="comp1" value="Abusive words are used">
<input type="checkbox" name="comp2" value="Attachments are vulgar">
<input type="checkbox" name="comp3" value="Provided links are not valid or open fake website">
<input type="checkbox" name="comp4" value="Provided contents are fake">

now, i want to know which option has been selected by the user, because accordingly i put the option sequence in one column of mysql. i want to store only the values which are not empty or only selected by the user....

any help please that how can it be done in php....

Dani AI

Generated

Good question, . The trick is to submit all checkboxes under the same name so PHP gives you an array (as suggested). One important improvement: do not send the descriptive text as the checkbox value. Send stable IDs (e.g., 1, 2, 3, 4) and map those IDs to human-readable labels on the server. That way you are not trusting user-submitted text and you can store exactly which options were picked, in order.

If you plan to write to MySQL, prefer prepared statements over manual escaping. The snippet below shows a normalized approach that preserves the selection order. It assumes your form uses values 1..4 and you have a junction table complaint_option(complaint_id, option_id, position).

// Read selected IDs as an array
$selected = filter_input(INPUT_POST, 'comp', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY) ?? [];

// Keep only allowed IDs and enforce integers and order
$allowed  = [1, 2, 3, 4];
$selected = array_values(array_intersect(array_unique(array_map('intval', $selected)), $allowed));

if ($selected) {
    // Insert rows using PDO prepared statements
    $pdo->beginTransaction();
    $stmt = $pdo->prepare(
        'INSERT INTO complaint_option (complaint_id, option_id, position) VALUES (?, ?, ?)'
    );
    foreach ($selected as $i => $optionId) {
        $stmt->execute([$complaintId, $optionId, $i]); // $i preserves the sequence chosen by the user
    }
    $pdo->commit();
}

Notes:

  • is on the right track with turning the selections into a single value, but building SQL with string concatenation is fragile and the old mysql_* functions are long deprecated. Use PDO or MySQLi with prepared statements instead.
  • If you truly need one column, store the IDs as JSON (e.g., [1,3,4]) and decode when reading. It is compact and keeps order, but is less query-friendly than the normalized table.

Recommended Answers

All 3 Replies

hi
you can give the same name for all check box as per i given here in example

<input type="checkbox" name="comp[]" value="Abusive words are used">
<input type="checkbox" name="comp[]" value="Attachments are vulgar">
<input type="checkbox" name="comp[]" value="Provided links are not valid or open fake website">
<input type="checkbox" name="comp[]" value="Provided contents are fake">

Now you get this value in array which selected by user

$values=$_post['comp'];//$values is an array of selected value by user

Now if you want to find that which value is selected than you can use na is_array() function ok


Thanks

And, to process Tulsa's values, I recommend looking into the implode function.

<?php
if(isset($_POST['comp'])) {
  // Clean data
  $cleanData = array();
  foreach($_POST['comp'] as $_comp) {
    $cleanData = mysql_real_escape_query($_comp);
  }

  // Build query
  $compStr = implode("'), ('", $cleanData);
  $sql = "INSERT INTO tbl('compCol') VALUES('$compStr')";

  // Example query:
  //  INSERT INTO tbl('compCol') VALUES('first'), ('second'), ('third')
}
?>

thank you for the solution.

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.