radio button in mysql

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ilikephp
    New Member
    • Dec 2007
    • 3

    radio button in mysql

    hello.

    when I am creating a form using the PHP language, everything is going fine and I have the data stored in the sql database,
    But when I add some radio button for example:

    <body>
    age:&nbsp; &lt;12
    <input type="radio" value="V1" name="12" />
    &nbsp;&nbsp;&nb sp; 13 - 18
    <input type="radio" name="13-18" value="V2" />
    &nbsp;&nbsp;&nb sp; 19 and up
    <input type="radio" name="13-18" value="V3" />

    </body>



    how can I let them be stored in the sql?
    need help plz!! thx in advance,
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Originally posted by ilikephp
    hello.

    when I am creating a form using the PHP language, everything is going fine and I have the data stored in the sql database,
    But when I add some radio button for example:

    <body>
    age:&nbsp; &lt;12
    <input type="radio" value="V1" name="12" />
    &nbsp;&nbsp;&nb sp; 13 - 18
    <input type="radio" name="13-18" value="V2" />
    &nbsp;&nbsp;&nb sp; 19 and up
    <input type="radio" name="13-18" value="V3" />

    </body>



    how can I let them be stored in the sql?
    need help plz!! thx in advance,
    Radio buttons cannot be stored in the database... shame, i know! I'd store a whole form in there if i could - pen included! The value of said radio button, however, can be. [php]$radio = $_POST['name_of_radio_ button'];[/php]:)

    Comment

    • zlash
      New Member
      • Dec 2007
      • 9

      #3
      It should be noted that radio buttons from the same group, should have the same element name. So radio buttons for A , B and C can have "value" attributes of 'A','B' and 'C' respectively but the same "name" attribute, something like 'letter'.

      So you will always receive the 'letter' key on the $_POST or $_GET array with the value of the option chosen.

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Originally posted by zlash
        It should be noted that radio buttons from the same group, should have the same element name. So radio buttons for A , B and C can have "value" attributes of 'A','B' and 'C' respectively but the same "name" attribute, something like 'letter'.

        So you will always receive the 'letter' key on the $_POST or $_GET array with the value of the option chosen.
        <input type="radio" name="name_For[]" />
        Creates an array which you can use in php - which is what zlash is talking about... i think.

        Comment

        • sumeetk
          New Member
          • Dec 2007
          • 6

          #5
          I m not sure what "ilikephp" is after but hope this can help him

          For example :

          I have purchase table that has 4 fields :

          Id,Quantity,Pri cePerItem, TotalCost

          [PHP]<?
          $sql = "SELECT * FROM purchase order by id";
          $result = mysql_query($sq l);

          echo "<TABLE borderColor=#cc cccc cellSpacing=0 cellPadding=0 align=center border=1>
          <TBODY>
          <TR align=middle bgColor=#ffffff >
          <TD width=115 height=20>
          <DIV align=center><S TRONG><span class=\"o\"><b> <font color=\"#000080 \">Options</font></b></span></STRONG></DIV></TD>
          <TD width=115 height=20>
          <DIV align=center><S TRONG><span class=\"o\"><b> <font color=\"#000080 \">Quantity</font></b></span></STRONG></DIV></TD>
          <TD width=115 height=20>
          <DIV align=center><S TRONG><span class=\"o\"><b> <font color=\"#000080 \">Price Per Item </font></b></span></STRONG></DIV></TD>
          <TD width=115 height=20>
          <DIV align=center><S TRONG><span class=\"o\"><b> <font color=\"#000080 \">Total Cost</font></b></span></STRONG></DIV></TD></TR>";

          while ($row = mysql_fetch_ass oc($result)) {

          echo "<TR vAlign=center align=middle bgColor=#eaeaea >
          <td><input type=\"radio\" value=\"$row[TotalCost]\" name=\"R1\"></td>
          <TD width=115 bgColor=#edf2fb height=20>
          <DIV align=center><s pan class=\"o\"><fo nt color=\"#000080 \">$row[Quanity]</font></span></DIV></TD>
          <TD width=115 bgColor=#edf2fb height=20>
          <DIV align=center><s pan class=\"o\"><fo nt color=\"#000080 \">$row[PricePerItem]</font></span></DIV></TD>
          <TD width=115 bgColor=#edf2fb height=20>
          <DIV align=center><s pan class=\"o\"><fo nt color=\"#000080 \">$$row[TotalCost]</font></span></DIV></TD></TR>";

          }
          }

          echo "</TBODY></TABLE>";

          ?>[/PHP]

          Comment

          • ilikephp
            New Member
            • Dec 2007
            • 3

            #6
            Hello guys ;)

            THANKS a lot for your help,
            but because I am new in PHP language and SQL, I find difficulties in doing the form. So I made a copy for the whole form, if you can please help me in the script that I should type in the sql server and in the php in order to make a link between them and the information will be stored. (so I can copy and paste it)
            THANKS in advance !! :)



            <table width="400" border="0" align="center" cellpadding="3" cellspacing="0" >
            <tr>
            <td><strong>FOR M</strong></td>
            </tr>
            </table>
            <table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCC C">
            <tr>
            <form id="form1" name="form1" method="post" action="addgues tbook.php">
            <td>
            <table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFF F">
            <tr>
            <td width="117">Nam e</td>
            <td width="14">:</td>
            <td width="357"><in put name="name" type="text" id="name" size="40" /></td>
            </tr>
            <tr>
            <td>Email</td>
            <td>:</td>
            <td><input name="email" type="text" id="email" size="40" /></td>
            </tr>
            <tr>
            <td>Age</td>
            <td>:</td>
            <td><label>
            <input name="radiobutt on" type="radio" value="radiobut ton" />
            &lt; 12
            <input name="radiobutt on" type="radio" value="radiobut ton" />
            13 - 18
            <input name="radiobutt on" type="radio" value="radiobut ton" />
            18 and up</label></td>
            </tr>
            <tr>
            <td>gender</td>
            <td>:</td>
            <td><label>
            male
            <input name="gender" type="radio" value="m" />
            female
            <input name="gender" type="radio" value="f" />
            </label></td>
            </tr>
            <tr>
            <td valign="top">Co mment</td>
            <td valign="top">:</td>
            <td><textarea name="comment" cols="40" rows="3" id="comment"></textarea></td>
            </tr>
            <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td><input type="submit" name="Submit" value="Submit" /> <input type="reset" name="Submit2" value="Reset" /></td>
            </tr>
            </table>
            </td>
            </form>
            </tr>
            </table>

            Comment

            • Markus
              Recognized Expert Expert
              • Jun 2007
              • 6092

              #7
              One thing you will not get from tsdn is code written for you.

              To retrieve the form you use $_POST

              i.e
              [php]
              $username = $_POST['username'];
              /* username is the name of the input of previus page */
              $email = $_POST['email'];
              /* email is the name of the input of previous page */
              $comment = $_POST['comment'];
              /* ... */

              //code for mysql connection and insert here **
              [/php]

              ** A good tutorial for connecting to mysql and insert/update/deleting data can be found here

              :)

              Comment

              • sumeetk
                New Member
                • Dec 2007
                • 6

                #8
                Hello,

                You may create a mysql.php file that will store your connection information and then add this file mysql.php to the guestbook.php which contains the form

                mysql.php file

                [PHP]<?

                $server = 'localhost';
                $database = 'guestbook';
                $username = 'root';
                $password = 'root';
                $connection = mysql_connect($ server,$usernam e,$password);

                if (!$connection) {
                die('Could not connect to MySQL database, the server return the error: '.mysql_error() );
                }
                $db = @mysql_select_d b($database);

                mysql_close();

                ?>[/PHP]

                The in the guestbook.php you will include this line on top

                [PHP]<?
                include "mysql.php" ;
                ...

                ?>[/PHP]

                Hope this helps

                Comment

                • ilikephp
                  New Member
                  • Dec 2007
                  • 3

                  #9
                  Thanks a lot,
                  but please I still need the code that I should put it in MySQL server.

                  and especially the code for the form that includes "age"

                  Thanks... :)

                  Comment

                  Working...