问题描述:

I have code below, radio button in a while loop. It will display question & the option for the answer from value 1 - 5. Since the name of each radio button needs to be unique, I assign each radio button as below.

However my problem is how do I insert the ans[".$row['id']."] value into my database. I already found the code to insert my database but I'm not sure how to change it accordingly. Please help me, thanks.

Display question & answer option

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";

}

Insert into database

 $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

}

}

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

This would give you a multidimensional array. Then insert each row in the database as how you want it.

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!
}

Remember you can always use var_dump to check the structure of your variable and edit however you want it to be!

网友答案:

I wrote a form with dummy data that shows you how to solve your problem. The code is quite easy and documented.

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;
     * }
     */
}
相关阅读:
Top