php radio buttons and mysql

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rnorr002
    New Member
    • Jun 2010
    • 2

    php radio buttons and mysql

    hey i have a question i have been struggling with a php script that will take the value of radio groups and submit them to mysql through phpmyadmin. i have searched the net for answers and cant seem to find anything. my goal is to have users select between four radio groupd who they wish elected for positions then have that data sent to mysql. any help here would be awesome. i am new to php btw.

    Code:
     
    <form id="form1" name="form1" method="post" action="process.php">
    
    <p>President</p>
      <p>
        <label>
          <input type="radio" name="president[]" value="field11" id="president" />
    Me</label>
        <br />
        <label>
          <input type="radio" name="president[]" value="field12" id="president" />
    You</label>
        <br />
        <label>
          <input type="radio" name="president[]" value="field13" id="president" />
    Other </label>
        <label> 
        <input type="text" name="president" id="president" value="" size="25" />
      </p>
      <p>
        </label>
      </p>
    <p>VicePresident</p>
      <p>
        <label>
        <input type="radio" name="vp" value="field21" id="vp" />
    John</label>
        <br />
        <label>
        <input type="radio" name="vp" value="field22" id="vp" />
    Marty</label>
        <br />
        <label>
        <input type="radio" name="vp" value="field23" id="vp" />
    Other</label>
        <input type="text" name="vp" id="vp" value="" size="25" />
        </label>
      </p>
      <p>&nbsp;</p>
    <p>Treasurer</p>
      <p>
        <label>
        <input type="radio" name="tr" value="field31" id="tr" />
    Bob</label>
        <br />
        <label>
        <input type="radio" name="tr" value="field32" id="tr" />
    Rob</label>
        <br />
        <label>
        <input type="radio" name="tr" value="field33" id="tr" />
    Other</label>
        <input type="text" name="tr" id="tr" value="" size="25" />
        </label>
      </p>
    <p>Sectretary</p>
      <p>
        <label>
        <input type="radio" name="sect" value="field41" id="sect" />
    Richard</label>
        <br />
        <label>
        <input type="radio" name="sect" value="field42" id="sect" />
    Bella</label>
        <br />
        <label>
        <input type="radio" name="sect" value="field43" id="sect" />
    Other</label>
        <label>
        <input type="text" name="sect" id="sect" value="" size="25" />
        </label>
      </p>
      <p>
        <label>
        <input type="reset" name="reset" id="reset" value="Reset" />
        <input type="submit" name="Submit" id="Submit" value="Submit" />
        </label>
        <br />
      </p>
    </form>
    
    <?php
    //Show all $_POST variables the page is receiving.
    echo '<pre>Debugging:'; print_r($_POST); echo '</pre>';
    reset($_POST); //print_r() moves the array pointer to the last value, this resets it to number one.
    //OR,
    var_dump($_POST);
    
    $president=$_POST['field11'];
     $vp=$_POST['field21'];
     $tr=$_POST['field31'];
     $sect=$_POST['field41'];
     mysql_connect("localhost", "root", "kungfu") or die(mysql_error());
     mysql_select_db("membership") or die(mysql_error());
     mysql_query("INSERT INTO `data` VALUES ('$president', '$vp', '$tr', '$sect')");
     Print "Your information has been successfully added to the database.";
     ?>
    oh yea and the text portion will shop up in mysql if filled out but no other data is sent
    Last edited by Niheel; Jun 20 '10, 08:58 PM. Reason: provided more information about question
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    You are accessing one button value instead of the group name.

    Comment

    • rnorr002
      New Member
      • Jun 2010
      • 2

      #3
      I figured it out with some help from another site. The problem was with the text boxes being named the same as the radio buttons. This caused the text box empty of full to override any data from the radio buttons. The fix was to rename the text box and access it using an if statement. I was thinking that I had to access the radio buttons in an array to get the data but I was wrong. Still learning PHP so this is progress.

      Comment

      Working...