Multiple Select box to MySQL PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rick711
    New Member
    • May 2010
    • 2

    #1

    Multiple Select box to MySQL PHP

    I want a user to choose values from the Multiple Select box:

    Code:
    <select name="service[]" id="service" size="6" multiple="multiple">
    <option value="histopathology">Histopathology</option>
    <option value="cytology">Cytology</option>
    <option value="endometrial biopsy">Endometrial Biopsy</option>
    <option value="joint fluid">Joint Fluid</option>
    <option value="bone marrow">Bone Marrow</option>
    <option value="other">Other</option>
    </select>
    the table is:

    Code:
    services CREATE TABLE `services` (
    `service_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
    `form_number` int(20) NOT NULL,
    `histo` tinyint(1) DEFAULT NULL,
    `cytology` tinyint(1) DEFAULT NULL,
    `endo` tinyint(1) DEFAULT NULL,
    `joint_fluid` tinyint(1) DEFAULT NULL,
    `bone_marrow` tinyint(1) DEFAULT NULL,
    `other` varchar(50) DEFAULT NULL,
    PRIMARY KEY (`service_id`)
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1

    im not sure how to insert the information as one record with the selections they choose.
    Last edited by Dormilich; May 31 '10, 07:37 PM. Reason: Please use [code] tags when posting code
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hey.

    When you submit a select like that, the selected options are available in your PHP code as an array, found in the $_POST super-global. Like in your code, if I were to select "histopathology " and "other", PHP would make those choices available in this format:
    Code:
    $_POST => Array
    (
        [service] => Array
            (
                [0] => histopathology
                [1] => other
            )
    
    )
    So to figure out which choices were selected, you simple check if the choice exists in that array. To do that, you can use the in_array function.
    [code=php]$fieldExists = in_array("field Name", $_POST['service']);[/code]

    Then you use that to build your query. For example:
    [code=php]// Create the top of the SQL query
    $sql = "INSRT INTO `services` SET\n";

    // Find out which choices were selected.
    $data = array(
    'histo' => in_array('histo pathology', $_POST['service']),
    'cytology' => in_array('cytol ogy', $_POST['service']),
    'endo' => in_array('endom etrial biopsy', $_POST['service']),
    'joint_fluid' => in_array('joint fluid', $_POST['service']),
    'bone_marrow' => in_array('bone marrow', $_POST['service']),
    'other' => in_array('other ', $_POST['service']),
    );

    // Add each option to the SQL query.
    foreach($data as $_fieldName => $_fieldValue) {
    $_fieldValue = $_fieldValue ? "TRUE" : "FALSE";
    $sql .= " `{$_fieldName}` = $_fieldValue\n" ;
    }

    // Echo the query
    echo $sql;[/code]

    This might print something like:
    Code:
    INSRT INTO `services` SET
        `histo` = FALSE
        `cytology` = TRUE
        `endo` = FALSE
        `joint_fluid` = TRUE
        `bone_marrow` = FALSE
        `other` = TRUE

    Comment

    • rick711
      New Member
      • May 2010
      • 2

      #3
      thanks so much for your reply...it helps out for my project...

      Comment

      Working...