By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,044 Members | 1,291 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,044 IT Pros & Developers. It's quick & easy.

radio button in mysql

P: 3
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;&nbsp; 13 - 18
<input type="radio" name="13-18" value="V2" />
&nbsp;&nbsp;&nbsp; 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,
Dec 28 '07 #1
Share this Question
Share on Google+
8 Replies


Markus
Expert 5K+
P: 6,050
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;&nbsp; 13 - 18
<input type="radio" name="13-18" value="V2" />
&nbsp;&nbsp;&nbsp; 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]:)
Dec 29 '07 #2

P: 9
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.
Dec 29 '07 #3

Markus
Expert 5K+
P: 6,050
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.
Dec 29 '07 #4

P: 6
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,PricePerItem, TotalCost

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

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

while ($row = mysql_fetch_assoc($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><span class=\"o\"><font color=\"#000080\">$row[Quanity]</font></span></DIV></TD>
<TD width=115 bgColor=#edf2fb height=20>
<DIV align=center><span class=\"o\"><font color=\"#000080\">$row[PricePerItem]</font></span></DIV></TD>
<TD width=115 bgColor=#edf2fb height=20>
<DIV align=center><span class=\"o\"><font color=\"#000080\">$$row[TotalCost]</font></span></DIV></TD></TR>";

}
}

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

?>[/PHP]
Dec 30 '07 #5

P: 3
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>FORM</strong></td>
</tr>
</table>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form id="form1" name="form1" method="post" action="addguestbook.php">
<td>
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td width="117">Name</td>
<td width="14">:</td>
<td width="357"><input 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="radiobutton" type="radio" value="radiobutton" />
&lt; 12
<input name="radiobutton" type="radio" value="radiobutton" />
13 - 18
<input name="radiobutton" type="radio" value="radiobutton" />
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">Comment</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>
Jan 3 '08 #6

Markus
Expert 5K+
P: 6,050
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

:)
Jan 3 '08 #7

P: 6
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,$username,$password);

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

mysql_close();

?>[/PHP]

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

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

?>[/PHP]

Hope this helps
Jan 3 '08 #8

P: 3
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... :)
Jan 4 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.