Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2006

    Unanswered: Insert multiple rows from checkbox values

    have a list of checkboxes generated in php from an external table.
    Each checkbox has a dynamic value and name. I am using a repeating region to display all the available checkboxes. Each check box also has a hidden input field asigned to that row. The value of this field is the id number of the article the checkboxes are to be associated with.
    The insert statement I am using is supposed to insert all the ticked values along with their hidden id value field into a relationship table that will ink the two tables using the (hidden field) id number as the foreign key.
    At present the statement is only taking the first checked value and storing that in the table.
    I'm guessing i need to have some sort of loop in the SQL statement. Any ideas how I would do this? Just so I don't waste anyones time, I dont want to store the values in an array in the same table row. I want each checkbox ticked to be stored in a new row. (normalization table).
    I'm sure this can be done using a foreach loop. I just don't have the technical know how to get it working.

    My code is below.

    Thanks in advance
    Attach Code


    <?php do { ?>
    <input name="subject" type="checkbox" id="subject" value="<?php echo $row_subjects['subid']; ?>" />
    <?php echo $row_subjects['subjectname']; ?></label>

    <input name="info" type="hidden" value="<?php echo $row_infoid['infoid']+1; ?>" />

    <?php } while ($row_subjects = mysql_fetch_assoc($subjects)); ?>

    ***************HERE IS THE SQL INSERT STATEMENT********************

    if ((isset($_POST["MM_insert2"])) && ($_POST["MM_insert2"] == "document")) {
    $insertSQL = sprintf("INSERT INTO relationships (infoid, subid) VALUES (%s, %s)",
    GetSQLValueString($_POST['info'], "text"),
    GetSQLValueString($_POST['subject'], "text"));

    mysql_select_db($database_dbconnect, $dbconnect);
    $Result1 = mysql_query($insertSQL, $dbconnect) or die(mysql_error());

    Is it possible to loop this statement so it repeats over all of the checkboxes?

  2. #2
    Join Date
    Apr 2005
    Baltimore, MD
    $_POST["subject"] is an array of values, so you need to insert each one into your sql. There are a bunch of posts in the forum about this, so try searching ("checkbox").
    Last edited by jfulton; 03-22-06 at 08:40.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts