SQL在while循环中插入单选按钮

时间:2016-12-15 09:46:57

标签: php sql arrays while-loop insert

我有下面的代码,while循环中的单选按钮。它会显示问题&从值1到5的答案选项。由于每个单选按钮的名称必须是唯一的,我分配每个单选按钮如下。

但是我的问题是如何将ans[".$row['id']."]值插入到我的数据库中。我已经找到了插入我的数据库的代码,但我不确定如何相应地更改它。请帮助我,谢谢。

显示问题&回答选项

while ($row = mysqli_fetch_array($sql))
{
    echo $row['question'];
    echo "<input type='radio' name='ans[".$row['id']."]' value='1'> 1";
    echo "<input type='radio' name='ans[".$row['id']."]' value='2'> 2";
    echo "<input type='radio' name='ans[".$row['id']."]' value='3'> 3";
    echo "<input type='radio' name='ans[".$row['id']."]' value='4'> 4";
    echo "<input type='radio' name='ans[".$row['id']."]' value='5'> 5";
}

插入数据库

    $ans = array();
    if(is_array($ans))
    {
        foreach($_POST['ans'] as $key1=>$value1)
        {   
            $ans[]=$value1;
        }
    }

    for($loop = 0; $loop < count($ans); $loop++)
    {
        if($ans[$loop]=="" || $ans[$loop]==null)
            $error=1;
    }

    if(isset($error))
    $error=1;
    else
    $error=0;

    if($error==0)
    {       
        for ($i = 0; $i < count($ans); $i++)
        {
            //insert sql
        }
    }

2 个答案:

答案 0 :(得分:0)

$ans = array();
foreach($_POST['ans'] as $key1=>$value1)
{   
    array_push($ans, [$key1 => $value1]);
}

这会给你一个多维数组。 然后按照您的需要插入数据库中的每一行。

foreach($ans as $value){
    // you can access it for example by doing $value[0] (which would be the result of the 1st element)
    //insert whatever you want in the database!
}

请记住,您始终可以使用var_dump检查变量的结构,然后根据您的需要进行编辑!

答案 1 :(得分:0)

我写了一个带有虚拟数据的表单,向您展示如何解决您的问题。代码非常简单并且有文档记录。

if (sizeof($_POST) == 0)
{
    /*
     * No data have been submitted yet so let's print the form
     */
    $questions = array("question1", "question2", "question3", "question4", "question4", 
        "question5", "question6", "question7", "question8", "question9", "question10");

    echo '<form method="post">';
    foreach ($questions as $question)
    {
        echo "$v<br/>";
        echo "<input type='radio' name='ans[".$question."]' value='1'> 1";
        echo "<input type='radio' name='ans[".$question."]' value='2'> 2";
        echo "<input type='radio' name='ans[".$question."]' value='3'> 3";
        echo "<input type='radio' name='ans[".$question."]' value='4'> 4";
        echo "<input type='radio' name='ans[".$question."]' value='5'> 5";   
        echo "<br/><br/>";
    }
    echo '<input type="submit" value="Submit">';
    echo '</form>';
}
else
{
    /*
     * Data has been submitted so sanitize it, extract it and save it to SQL
     */
    $data = filter_var_array($_POST, FILTER_SANITIZE_STRING); 

    $vals = '';
    foreach ($data['ans'] as $question => $answer)
    {
        $vals .= "($question, $answer),";
    }
    $vals[strlen($vals) - 1] = ''; // get rid of the last comma

    $sql = "INSERT INTO your_table_name (question, answer) VALUES $vals";

    // now execute $sql with whatever DB driver you use 
    /*
     * with mysqli it would be like this 
     * 
     * if ($conn->query($sql) === TRUE) 
     * {
     *      echo "New record created successfully";
     *  } 
     * else 
     * {
     *       echo "Error: " . $sql . "<br>" . $conn->error;
     * }
     */
}