View Full Version : inserting data into mysql from radio button form in PHP

11-01-2012, 09:21 PM
I am trying to insert my form's data radio buttons into my mysql database. it works very well but when i skip any one value of my radio button,as this is more than 20, its giving error that:

( ! ) Notice: Undefined index: qr4 in C:\wamp\www\exam\contents\omr.php on line 1475
Call Stack
# Time Memory Function Location
1 0.0019 194432 {main}( ) ..\omr.php:0

but in the database, i am getting my desired result, means all the values are got inserted and which radio button is not put in the form, inseting with blank, as i want.. and also executing lines which is written after mysql query in the program.
so how can i debug my error??
hope you got my problem..i am also copying my code below..

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<body bgcolor="green">
// Connects to your Database
//checks cookies to make sure they are logged in
$username = $_COOKIE['ID_my_site'];
$pass = $_COOKIE['Key_my_site'];
$check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error());
while($info = mysql_fetch_array( $check ))
//if the cookie has the wrong password, they are taken to the login page
if ($pass != $info['password'])
header("Location: login.php");
<form name="input" action="" method="post">
User Name: <input type="text" name="roll" value="" /><br />
<br />
<input type="radio" name="qr1" value="a">
<input type="radio" name="qr1" value="b">
<input type="radio" name="qr1" value="c">
<input type="radio" name="qr1" value="d">
<input type="radio" name="qr1" value="e">
<br />
<input type="radio" name="qr2" value="a">
<input type="radio" name="qr2" value="b">
<input type="radio" name="qr2" value="c">
<input type="radio" name="qr2" value="d">
<input type="radio" name="qr2" value="e">
<br />
<input type="radio" name="qr3" value="a">
<input type="radio" name="qr3" value="b">
<input type="radio" name="qr3" value="c">
<input type="radio" name="qr3" value="d">
<input type="radio" name="qr3" value="e">
<br />
<input type="radio" name="qr4" value="a">
<input type="radio" name="qr4" value="b">
<input type="radio" name="qr4" value="c">
<input type="radio" name="qr4" value="d">
<input type="radio" name="qr4" value="e">
<br />
<input type="radio" name="qr5" value="a">
<input type="radio" name="qr5" value="b">
<input type="radio" name="qr5" value="c">
<input type="radio" name="qr5" value="d">
<input type="radio" name="qr5" value="e">
<br />
till qr50......
<input type="submit" name="submit" value="Submit">
if (isset($_POST['submit']))
$insert = "INSERT INTO omr (roll, qr1, qr2, qr3, qr4, qr5)
VALUES('".$_POST['roll']."', '".$_POST['qr1']."', '".$_POST['qr2']."', '".$_POST['qr3']."',
'".$_POST['qr4']."', '".$_POST['qr5']."')";
echo "Your answers is submitted now";
//if the cookie does not exist, they are taken to the login screen
header("Location: login.php");

so my problems are
(1)when put or select all radio button and then submitted then no error.
(2)when i skip any one radio button, as i don't want to tick that button then getting just error message(which is already written above) but values are stored in database.
(3)and one more help that can i make an additional radio button by which if i select option and after some time decided don't want to put that option then selected option got removed for that question only.
pls sir help me..

11-01-2012, 09:40 PM
If you skip a radio button, the HTML considers the input to not be successful (which makes perfect sense, otherwise you'd end up with *every* radio button regardless of if its checked).
Theoretically, your DB should never have a null value for a field if its bcnf normalized. That said, sometimes its just easier to take a step back (ie: I have home and work phone numbers for my contact table, but a few people didn't want to give me home phones so I just leave it as null). Not the best practice no, but its not a breaker either.

The fix is simple. Default the value.

$qr4 = isset($_POST['qr4']) ? $_POST['qr4'] : "";

Extract all the variables like that and use them in the query instead. You'll also want to look into SQL Injection to prevent that, or using prepared statements with Mysqli or PDO to bind. Much better especially now that the mysql library is discouraged from usage (which means we'll see it deprecated probably by PHP 6).

For your last one, you can't do that using radio input. Once something is selected, you must choose at least one option of that name. Perhaps a select menu would be better? Or you can add another radio with a value of "" and give it a label of "None" or something like that? So long as you enter the form, populate the data from the db, and then save it you can issue an update on every field if you like. An empty field I would presume is allowed from the looks of it.

11-02-2012, 12:02 AM
Thank you so much..i got my mistake..
ok is that poosible to use java script in php code??
actually i want my auto close browser function after clicking on submit..
is that poosible then pls help..

11-02-2012, 01:16 AM
JS and PHP are not compatible language as one is server processed and the other is client processed.
PHP is capable of generating JS, and JS is capable of calling a PHP script (not code, but the entire script with ajax), but the languages themselves cannot be combined. Your output can include a JS that issues a close on the window though, so you can add like a 5 second thank you notice, with JS timing out with a window.close.