0

I am trying to submit this radio buttons but it shows this

Notice: Array to string conversion in C:...

i don't really know what to do to make it work.

I've to try some other ways but its not working

    <?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);} } } }

it was suppose to submit but its just bringing up errors

Victor
  • 11
  • 1
  • 5
  • 1
    Possible duplicate of [How to solve PHP error 'Notice: Array to string conversion in...'](https://stackoverflow.com/questions/20017409/how-to-solve-php-error-notice-array-to-string-conversion-in) – Nico Haase Feb 04 '19 at 13:52

2 Answers2

0

Error is with your name tag radio option in form.

Replace your echo "" part --

with

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

and

$insert->bindParam(':uans', $uans); can bind only single value not the array data.
Abhishek
  • 1,543
  • 3
  • 13
  • 29
  • the radio button is working perfectly its just the submitting that is giving errors i.e the second function – Victor Feb 04 '19 at 13:42
  • ok, the second function is suppose to insert the answers from the radio, but it's not working – Victor Feb 04 '19 at 13:45
  • check for $insert->bindParam(':uans', $uans); can bind only single values not array data – Abhishek Feb 04 '19 at 13:49
  • so how should i do it – Victor Feb 04 '19 at 13:51
  • check for $uans = $_POST['opt'] . $i; var_dump($uans); var_dump will let you knw the data and its type you are receiving in value., if its string its ok, if array, then make it string and insert it and also check for data type in table structure too. – Abhishek Feb 04 '19 at 13:55
-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