Member Avatar for anmol.raghuvanshi1

I am Creating a form with some question about 3 or 4 question.answers are submitted via radio button which has to be inserted in database.I have 2 problems

problem 1: in given code i have implemented for one question as we know for inserting the ans in database we have to use name attribute in $_post method now my question do we have repeat the same for radio buttons having different name attribute or can be done in other way.

problem 2:i have created table diseases with q1 as type enum with value month year weeks.
do i have to set these value for different questions in database or there is another way.

//que ans form

<td colspan="2" align="left" valign="top"><b>Do you suffer with bloated feeling in the abdomen</b> 
                                                            </td>
                                                        </tr>
                                                                                                                    <tr>
                                                            <td colspan="2" align="left" valign="top"><input name="150rboption13" value="1" type="radio">                                                         <span class="content"> Yes</span>
                                                            </td>
                                                        </tr>
                                                                                                                        <tr>
                                                            <td colspan="2" align="left" valign="top"><input name="150rboption13" value="2" type="radio">                                                         <span class="content"> No</span>
                                                            </td>
                                                        </tr>
                                                                                                                        <tr>
                                                            <td colspan="2" align="left" valign="top"><input name="150rboption13" value="3" type="radio">                                                         <span class="content">Occasionally</span>
                                                            </td>
                                                        </tr>
                                                                                                                        <tr>
                                                            <td colspan="2" align="left" valign="top"><b>Do you suffer with belching/burps?</b>    
                                                            </td>
                                                        </tr>
                                                                                                                    <tr>
                                                            <td colspan="2" align="left" valign="top"><input name="151rboption13" value="1" type="radio">                                                         <span class="content"> Yes</span>
                                                            </td>
                                                        </tr>
                                                                                                                        <tr>
                                                            <td colspan="2" align="left" valign="top"><input name="151rboption13" value="2" type="radio">                                                         <span class="content"> No</span>
                                                            </td>
                                                        </tr>
                                                                                                                        <tr>
                                                            <td colspan="2" align="left" valign="top"><input name="151rboption13" value="3" type="radio">                                                         <span class="content">Occasionally</span>
                                                            </td>
                                                        </tr>
                                                                                                                        <tr>
                                                            <td colspan="2" align="left" valign="top"><b>Do you pass wind/gas ?</b>    
                                                            </td>
                                                        </tr>
                                                                                                                    <tr>
                                                            <td colspan="2" align="left" valign="top"><input name="152rboption13" value="1" type="radio">                                                         <span class="content"> Yes </span>
                                                            </td>
                                                        </tr>
                                                                                                                        <tr>
                                                            <td colspan="2" align="left" valign="top"><input name="152rboption13" value="2" type="radio">                                                         <span class="content"> No</span>
                                                            </td>
                                                        </tr>
                                                                                                                        <tr>
                                                            <td colspan="2" align="left" valign="top"><input name="152rboption13" value="3" type="radio">                                                         <span class="content">Occasionally</span>
                                                            </td>
                                                        </tr>



//form processing php
<?php
    //Start session
    session_start();

    //Include database connection details
    require_once('connection/config.php');

    //Array to store validation errors
    //$errmsg_arr = array();

    //Validation error flag
    //$errflag = false;

    //Connect to mysql server
    $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
    if(!$link) {
        echo"Not Connected";
        die('Failed to connect to server: ' . mysql_error());
    }

    //Select database
    $db = mysql_select_db(DB_DATABASE);
    if(!$db) {
        echo "Not Selected";
        die("Unable to select database");
    }

    //Function to sanitize values received from the form. Prevents SQL injection
    /*function clean($str) {
        $str = @trim($str);
        if(get_magic_quotes_gpc()) {
            $str = stripslashes($str);
        }
        return mysql_real_escape_string($str);
    }
    */
    //Sanitize the POST values
    //$ans = clean($_POST['25rboption13']);



    //If there are input validations, redirect back to the same form
    /*if($errflag) {
        $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
        session_write_close();
        header("location: .php");
        exit();
    }
    */

    //Check whether the query was successful or not

        if(isset($_POST['submit'])){
        $ans=isset($_POST['25rboption13']);
    //  $q = in_array($_POST['25rboption13'], array('Days', 'Weeks','Months','Years')) ? $_POST['25rboption13'];
        if($ans!=""){
        //Create query
        $qry="INSERT INTO diseases (q1) values('$ans')";
        $result=mysql_query($qry);
        echo "data inserted";
    }       //Successful


    //      exit();
        /*else {
            //Login failed
            header("location: login-failed.php");
            exit();
        }*/
    }else {
        die("Query failed");
    }
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Form Processing</title>
</head>

<body>
</body>
</html>

make the radiobuttons fall under the same radiogroup by giving the same name
e.g.

<input name="gender" value="Male" type="radio"> 
    <input name="gender" value="Female" type="radio">

Then in PHP code

$gender=$_GET['gender']
Member Avatar for anmol.raghuvanshi1

its 15 question length form if i make fall all radio buttons under same name how different options will be selected

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.