处理大型单选按钮数据



我创建了一个表单,其中包含15个问题,答案将通过单选按钮提交。我的代码适用于5个问题,但当我增加数据库中问题字段的数量时,就不会进行插入。

不同的问题有不同的名称字段,我无法使所有单选按钮的名称相同:(

这是我的代码

 <?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
    /*$ignorefield=array('submit');
    $fields=array_intersect($_POST,array_flip($ignorefield));
    $values=array();
    */  
    /*  $req1=$_REQUEST['25rboption13'];
        $req2=$_REQUEST['129rboption13'];
        $req3=$_REQUEST['150rboption13'];
        $req4=$_REQUEST['151rboption13'];
        $req5=$_REQUEST['152rboption13'];
        $req6=$_REQUEST['153rboption13'];
        $req7=$_REQUEST['154rboption13'];
        $req8=$_REQUEST['592rboption13'];
        $req9=$_REQUEST['593rboption13'];
        $req10=$_REQUEST['594rboption13'];
        $req11=$_REQUEST['595rboption13'];
        $req12=$_REQUEST['596rboption13'];
        $req13=$_REQUEST['1169rboption13'];
    */
    if(isset($_POST['submit'])){
                            $ans=isset($_POST['25rboption13']);
                                $ans=isset($_POST['129rboption13']);
                                    $ans=isset($_POST['150rboption13']);
                                        $ans=isset($_POST['151rboption13']);
                                            $ans=isset($_POST['152rboption13']);
                                                $ans=isset($_POST['153rboption13']);
                                                    $ans=isset($_POST['154rboption13']);
                                                $ans=isset($_POST['592rboption13']);
                                            $ans=isset($_POST['593rboption13']);
                                        $ans=isset($_POST['594rboption13']);
                                    $ans=isset($_POST['595rboption13']);
                                $ans=isset($_POST['596rboption13']);
                            $ans=isset($_POST['1169rboption13']);



        //foreach($_POST as $key => $val){
        if($ans!=""){
        //Create query
        $qry="INSERT INTO diseases (q1,q2,q3,q4,q5,q6,q7,q8,q9,q10,q11,q12,q13) values(' ".$_POST['25rboption13']." ',' ".$_POST['129rboption13']." ',' ".$_POST['150rboption13']." ',' ".$_POST['151rboption13']." ',' ".$_POST['152rboption13']." ',' ".$_POST['153rboption13']." ',' ".$_POST['154rboption13']." ',' ".$_POST['592rboption13']." ',' ".$_POST['593rboption13']." '' ".$_POST['594rboption13']." ',' ".$_POST['595rboption13']." ',' ".$_POST['596rboption13']." ',' ".$_POST['1169rboption13']." ')";
        $result=mysql_query($qry);}
        //}
        echo "data inserted";
            //Successful
        }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>

//样本

                                                                                                            <tr>
                                                    <td colspan="2" align="left" valign="top"><input name="25rboption13" value="1" type="radio">                                                            <span class="content"> Days</span>
                                                    </td>
                                                </tr>
                                                                                                                <tr>
                                                    <td colspan="2" align="left" valign="top"><input name="25rboption13" value="2" type="radio">                                                            <span class="content"> Weeks</span>
                                                    </td>
                                                </tr>
                                                                                                                <tr>
                                                    <td colspan="2" align="left" valign="top"><input name="25rboption13" value="3" type="radio" >                                                           <span class="content">Months</span>
                                                    </td>
                                                </tr>
                                                                                                                <tr>
                                                    <td colspan="2" align="left" valign="top"><input name="25rboption13" value="4" type="radio">                                                            <span class="content"> Years</span>
                                                    </td>
                                                </tr>
                                                                                                                <tr>
                                                    <td colspan="2" align="left" valign="top"><b>Do you have pain/burning in the chest region </b>  
                                                    </td>
                                                </tr>
                                                                                                            <tr>
                                                    <td colspan="2" align="left" valign="top"><input name="129rboption13" value="1" type="radio">                                                           <span class="content"> Yes</span>
                                                    </td>
                                                </tr>
                                                                                                                <tr>
                                                    <td colspan="2" align="left" valign="top"><input name="129rboption13" value="2" type="radio">                                                           <span class="content"> No</span>
                                                    </td>
                                                </tr>
                                                                                                                <tr>
                                                    <td colspan="2" align="left" valign="top"><input name="129rboption13" value="3" type="radio">                                                           <span class="content">Occasionally</span>
                                                    </td>
                                                </tr>
                                                                                                                <tr>

//sql

diseases

CREATE TABLE `diseases` (
  `id` int(11) NOT NULL auto_increment,
  `q1` enum('Days','Weeks','Months','Years') collate latin1_general_ci NOT NULL,
  `q2` enum('Yes','No','Ocassionally') collate latin1_general_ci NOT NULL,
  `q3` enum('Yes','No','Ocassionally') collate latin1_general_ci NOT NULL,
  `q4` enum('Yes','No','Ocassionally') collate latin1_general_ci NOT NULL,
  `q5` enum('Yes','No','Ocassionally') collate latin1_general_ci NOT NULL,
  `q6` enum('Yes','No','Ocassionally') collate latin1_general_ci NOT NULL,
  `q7` enum('Yes','No','Ocassionally') collate latin1_general_ci NOT NULL,
  `q8` enum('Yes','No','Ocassionally') collate latin1_general_ci NOT NULL,
  `q9` enum('After eating food','       When hungry and stomach is empty','Not sure') collate latin1_general_ci NOT NULL,
  `q10` enum('After eating food','  After drinking something','No relation to food') collate latin1_general_ci NOT NULL,
  `q11` enum('Yes','No','Not sure') collate latin1_general_ci NOT NULL,
  `q12` enum('Yes','No') collate latin1_general_ci NOT NULL,
  `q13` enum('Yes','No') collate latin1_general_ci NOT NULL,
  `name` text collate latin1_general_ci NOT NULL,
  `phone_no` int(10) NOT NULL,
  `address` text collate latin1_general_ci NOT NULL,
  PRIMARY KEY  (`id`)
) 

最后,我发现我的答案问题是,如果我替换我的代码mysql_query(query)。。。而不是$result=mysql_query($qry)我不知道为什么,但它工作

如下更改脚本:mysql连接:

$conn=mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

然后

$result=mysqli_query($conn,$query);

最新更新