tags:

views:

24

answers:

1
<input name="names[]" type="text"/><select><option value="male">male</option><option value="female">female</option></select>  
<input name="names[]" type="text"/><select><option value="male">male</option><option value="female">female</option></select>  
<input name="names[]" type="text"/><select><option value="male">male</option><option value="female">female</option></select>  





    if (!empty($_POST['names'])) {
    foreach ($_POST['names'] AS $names) {
        // add to the database
     if (!empty($names)){
      $rs=mysql_query("SELECT names FROM table WHERE names = '$names' LIMIT 1");
      if(mysql_num_rows($rs) == 0) {
     mysql_query("INSERT INTO table (names,gender) VALUES ('$names','$gender')");
      }
     }
    }
}

This does inset the names however i don't know how i can make the genders work for each individual name.

+1  A: 

You need to give your SELECT elements a name:

<input name="names[]" type="text"/><select name="genders[]"><option value="male">male</option><option value="female">female</option></select>

Now you can use the key of $_POST['names'] to get the corresponding gender:

foreach ($_POST['names'] AS $key => $names) {
    // add to the database
    if (trim($names) != '' && !empty($_POST['genders'][$key]) && in_array($_POST['genders'][$key], array('male', 'female'))) {
        $gender = $_POST['genders'][$key];
        $rs = mysql_query("SELECT names FROM table WHERE names = '".mysql_real_escape_string($names).' LIMIT 1");
        if (mysql_num_rows($rs) == 0) {
            mysql_query("INSERT INTO table (names,gender) VALUES ('".mysql_real_escape_string($names)."','$gender')");
        }
    }
}

You should also use mysql_real_escape_string to sanatize the data sent by the user.

Gumbo