SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    RadioButton Group in multiple inserts?!?

    I'm trying to get a radiobuttongroup into a multiple insert query like this:

    The form:
    PHP Code:
    echo '<table>
            <tr>
                <td></td>
                <td>
                    '
    ._MALE.' <input type="radio" name="sex1" value="male" id="sex[]" checked />
                    &nbsp;&nbsp;'
    ._FEMALE.' <input type="radio" name="sex1" value="female" id="sex[]" />
                </td>
            </tr>
            <tr>
                <td align="right"><div align="right">'
    ._FIRST_NAME.':</td>
                <td><input name="fname[]" type="text" size="42"></td>
            </tr>
            <tr>
                <td align="right"><div align="right">'
    ._LAST_NAMES.':</td>
                <td><input name="lname[]" type="text" size="42"></td>
            </tr>
            <tr>
                <td align="right"><div align="right">'
    ._EMAIL.':</td>
                <td><input name="email[]" type="text" size="42"></td>
            </tr>
            <tr>
                <td colspan="2"><br><div style="border-bottom:1px solid #669900;"></div><br></td>
            </tr>
            <tr>
                <td></td>
                <td>
                    '
    ._MALE.' <input type="radio" name="sex2" value="male" id="sex[]" checked />
                    &nbsp;&nbsp;'
    ._FEMALE.' <input type="radio" name="sex2" value="female" id="sex[]" />
                </td>
            </tr>
            <tr>
                <td align="right"><div align="right">'
    ._FIRST_NAME.':</td>
                <td><input name="fname[]" type="text" size="42"></td>
            </tr>
            <tr>
                <td align="right"><div align="right">'
    ._LAST_NAMES.':</td>
                <td><input name="lname[]" type="text" size="42"></td>
            </tr>
            <tr>
                <td align="right"><div align="right">'
    ._EMAIL.':</td>
                <td><input name="email[]" type="text" size="42"></td>
            </tr>
            <tr>
                <td colspan="2"></td>
            </tr>
    </table>'

    The insert query:
    PHP Code:
       $sex $_POST['sex'];
        
    $firstname $_POST['fname']; 
        
    $lastname $_POST['lname'];
        
    $email $_POST['email'];

    for (
    $i 0$i count($firstname); $i++) {
                
                    
    $gacode 'GA';
                    
    $gacode .= chr(rand(65,90));
                    
    $gacode .= time();
                    
    $gacode .= uniqid($prefix);
                    
                    
    $nemail $email[$i];
                    
    $nfname $firstname[$i];
                    
    $nlname $lastname[$i];
                    
    $nsex $sex[$i];

    $result $db->sql_query("INSERT INTO ".$prefix."_users (fname,lname,sex,email,validatecode) VALUES ('$nfname','$nlname','$nsex','$nemail','$gacode')");


    Everything works fine except the radiobuttongroup thing... Where do I go wrong?

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Look at what data you are sending in the form and then at what data you're trying to achieve.

    Hint: $_POST['sex'] won't exist.

    You've mixed up the ID and Name on the radiobuttons.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The radio button names are named as arrays. You have the id attribute with what the name attribute should be. And even though you have sex1 and sex2, your for loop starts at zero.

  4. #4
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, if I make the radiobuttons group name "sex[]" which I think it should be, then all the radiogroupbuttons are linked together... How do I get passed that?

  5. #5
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could try:
    PHP Code:
    # your other table rows
    echo '
            <tr>
                <td></td>
                <td>
                    '
    ._MALE.' <input type="radio" name="sex0" value="male" checked />
                    &nbsp;&nbsp;'
    ._FEMALE.' <input type="radio" name="sex0" value="female" />
                </td>
            </tr>


            <tr>
                <td></td>
                <td>
                    '
    ._MALE.' <input type="radio" name="sex1" value="male" checked />
                    &nbsp;&nbsp;'
    ._FEMALE.' <input type="radio" name="sex1" value="female" />
                </td>
            </tr>
    '
    ;



    for (
    $i 0$i count($firstname); $i++) {
    # your other code

    $nsex $_POST['sex'.$i];


    Also remember to validate user input and use either mysql_real_escape_string or PDO prepared statements for your database queries. PDO is recommended.


Bookmarks

Posting Permissions

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