Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Exclamation inserting data into mysql from radio button form in PHP

    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:
    Code:
    ( ! ) 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..
    Code:
        <!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">
        <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>rightframe</title>
        </head>
        <body bgcolor="green">
        <?php
        // Connects to your Database
        require_once("connection.php");
        //checks cookies to make sure they are logged in
        if(isset($_COOKIE['ID_my_site']))
        {
        $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");
        }
        else
        {
        ?>
        <form name="input" action="" method="post">
        User Name: <input type="text" name="roll" value="" /><br />
        Reasoning
        <br />
        QR01.
        <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 />
        QR02.
        <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 />
        QR03.
        <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 />
        QR04.
        <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 />
        QR05.
        <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">
        </form>
        <?php
        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']."')";
        mysql_query($insert);
        echo "Your answers is submitted now";
        }
        }
        }
        }
        else
        //if the cookie does not exist, they are taken to the login screen
        {
        header("Location: login.php");
        }
        ?>
        </body>
        </html>
    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..

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.
    PHP Code:
    $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.

  • Users who have thanked Fou-Lu for this post:

    manupathak (11-01-2012)

  • #3
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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..

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.


  •  

    Posting Permissions

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