如何在php

时间:2019-02-04 13:23:25

标签: php

我正在尝试提交此单选按钮,但显示为

  

注意:C:...中的数组到字符串的转换

我真的不知道该怎么做。

我必须尝试其他方法,但不起作用

    <?php

public function userQuestion($type)
{
    $option = $this->con->prepare("SELECT * FROM stem_questions WHERE category = :type");
    $option->bindParam(':type', $type);
    $option->execute();
    $id = array();
    while ($row = $option->fetch()) {
        $id[]  = $row["id"];
        $que[] = $row["question"];
        $o1[]  = $row["opt1"];
        $o2[]  = $row["opt2"];
        $o3[]  = $row["opt3"];
        $o4[]  = $row["opt4"];
        $cat[] = $row["category"];
        $isa[] = $row["is_anwer"];
    }
    $count = count($id);
    if ($count < 1) {
        echo "<span class='alert alert-info fa fa-warning col-lg-12' style='color:orange; font-size:14px; color: white'> You have not Set any Question Yet.</span>";
    } else {
        $k = 0;
        echo "";
        for ($i = 1, $j = 0; $i <= $count; $i++, $j++) {
            $k++;
            echo "<form method='POST'>                                      <p><strong> Question $i : $que[$j]</strong></p>                                         <div class='form-group'> <input type='hidden' name ='qust' value='$que[$j]'> <input type='hidden' name ='cat' value='$cat[$j]'><input type='hidden' name ='ans' value='$isa[$j]'> <input type='radio' name ='opt[$k]' value='$o1[$j]'> <label class='form-check-label'>&nbsp $o1[$j]</label><br> <input type='radio' name='opt[$k]' value='$o2[$j]'><label class='form-check-label'>&nbsp $o2[$j]</label><br> <input type='radio' name='opt[$k]' value='$o3[$j]'><label class='form-check-label'>&nbsp $o3[$j]</label><br> <input type='radio' name ='opt[$k]' value='$o4[$j]'> <label class='form-check-label'>&nbsp $o4[$j]</label><br>   </div> </form> ";}   } }
public function submitQuiz()
{
    //FOR THE QUIZ SYSTEM
    if (isset($_POST["submit"])) {            
        for ($i = 1; $i <= 5; $i++) {
            $userid   = $_SESSION['id'];
            $qid      = $_POST['qust'];
            $uans     = $_POST['opt'] . $i;
            $ans      = $_POST['ans'];
            $category = $_POST['cat'];
            $insert   = $this->con->prepare("INSERT INTO user_question_answer (user_id, question_id, user_ans, is_right, type)VALUES (:userid, :qid, :uans, :ans, :category)");
            $insert->bindParam(':userid', $userid);
            $insert->bindParam(':qid', $qid);
            $insert->bindParam(':uans', $uans);
            $insert->bindParam(':ans', $ans);
            $insert->bindParam(':cat', $cat);
            if (!$insert->execute()) {
                $atry = $insert->errorInfo();
                echo $atry[2];
                exit();
            } else {
                // this just my person function #return $this->just_notify("<strong>Successfully uploaded </strong>", 1);} } } }

本来是要提交的,但只是带来了错误

2 个答案:

答案 0 :(得分:0)

您的表单中的名称标签单选选项出错。

替换您的echo ""部分-

echo "<form method='POST'>
<p><strong> Question $i : $que[$j]</strong></p> 
<div class='form-group'>
 <input type='hidden' name ='qust' value='$que[$j]'>                             
 <input type='hidden' name ='cat' value='$cat[$j]'>                          
 <input type='hidden' name ='ans' value='$isa[$j]'>                          
<input type='radio' name ='opt1['.$k.']' value='$o1[$j]'> <label class='form-check-label'>&nbsp $o1[$j]</label><b`enter code here`r>
<input type='radio' name='opt2['.$k.']' value='$o2[$j]'><label class='form-check-label'>&nbsp $o2[$j]</label><br>
<input type='radio' name='opt3['.$k.']' value='$o3[$j]'><label class='form-check-label'>&nbsp $o3[$j]</label><br>
<input type='radio' name ='opt4['.$k.']' value='$o4[$j]'> <label class='form-check-label'>&nbsp $o4[$j]</label><br>
</div>
</form>"

$insert->bindParam(':uans', $uans); can bind only single value not the array data.

答案 1 :(得分:-1)

> <input type="radio" name="radio_name[]"
> value="id_of_row_from_database_or_identity_of_this_radio_button" />
> 
> ex:-
> 
> <input type="radio" name="age[]" value="G`enter code here`G101" />12 <input
> type="radio" name="age[]" value="GG102" />23