简体   繁体   English

您如何将具有许多元素的数组与php中的数据库进行比较?

[英]How do you compare an array with many elements to an database in php?

I have made an online exam portal where different radio options selected by the users are being stored in an array, then i am sending that array in a php file to get checked with the database,and this is where i am facing the problem.The answers are in the database and after i fetch them i compare them to the array but it is not getting solved. 我已经建立了一个在线考试门户,其中将用户选择的不同无线电选项存储在数组中,然后我将该数组发送到php文件中以与数据库进行检查,这就是我面临的问题。答案在数据库中,在我获取它们之后,我将它们与数组进行比较,但是并没有解决。 please help . 请帮忙 。

This is the table questionpaper from which answers are coming from:- 这是表格问题纸,答案来自:

qid     questions       opa    opb    opc   opd   ans
1      what is that?    car    dog   john   star   4
2   .  what is that?    car    dog   john   star   4
3      what is that?    car    dog   john   star   4
4      what is that?    car    dog   john   star   4
5      what is that?    car    dog   john   star   4
6      what is that?    car    dog   john   star   4

Here is the javascript code from where i am send it:- 这是我从那里发送的JavaScript代码:-

function Checkanswers(){
        var Selected = document.querySelectorAll('[name="op"]:checked');
        if (Selected != null) {
        Selected.forEach(function(radio) {
        arr.push(radio.value);
        });

        }
        alert(JSON.stringify(arr));
        $.ajax({
            type: "POST",
            url:"check.php",
            data: {"myJSarray" : JSON.stringify(arr)},
            success: function(data)
            {
                alert(data);
            }
        });

    }

Here is the php code which is doing all of it:- 这是完成所有工作的php代码:-

<?php

session_start();
require 'includes/dbh.inc.php';

$array = array();

$arr =$_POST["myJSarray"];

$arr1=substr($arr,1,strlen($arr)-1);

$array=explode(',',$arr1);



$result = 0;
$i= 1;
$sql = " SELECT * FROM questionpaper ";
$query = mysqli_query($conn,$sql);
while($rows = mysqli_fetch_array($query)){

    $checked = ($rows['ans'] ==  (int)$array[$i]) ;

    if($checked){

        $result++;

    }
    $i++;

}

echo "<br> Your total score is".$result;

?> ?>

error:- 错误:-

Notice: Undefined index: myarray in D:\XAMPP\htdocs\assignments\check.php on line 8

Notice: Undefined offset: 1 in D:\XAMPP\htdocs\assignments\check.php on line 20

Notice: Undefined offset: 2 in D:\XAMPP\htdocs\assignments\check.php on line 20

Notice: Undefined offset: 3 in D:\XAMPP\htdocs\assignments\check.php on line 20

Notice: Undefined offset: 4 in D:\XAMPP\htdocs\assignments\check.php on line 20

Notice: Undefined offset: 5 in D:\XAMPP\htdocs\assignments\check.php on line 20

Notice: Undefined offset: 6 in D:\XAMPP\htdocs\assignments\check.php on line 20

Notice: Undefined offset: 7 in D:\XAMPP\htdocs\assignments\check.php on line 20

Notice: Undefined offset: 8 in D:\XAMPP\htdocs\assignments\check.php on line 20

Notice: Undefined offset: 9 in D:\XAMPP\htdocs\assignments\check.php on line 20

Notice: Undefined offset: 10 in D:\XAMPP\htdocs\assignments\check.php on line 20

Notice: Undefined offset: 11 in D:\XAMPP\htdocs\assignments\check.php on line 20

Notice: Undefined offset: 12 in D:\XAMPP\htdocs\assignments\check.php on line 20

Notice: Undefined offset: 13 in D:\XAMPP\htdocs\assignments\check.php on line 20

Notice: Undefined offset: 14 in D:\XAMPP\htdocs\assignments\check.php on line 20

Notice: Undefined offset: 15 in D:\XAMPP\htdocs\assignments\check.php on line 20

Your total score is0

change this 改变这个

var arr = [];
 var Selected = document.querySelector('[name="op"]:checked');
 if (Selected!= null) {
 arr.push(Selected.value);
 } 
console.log(arr);

change the php to this i assume answer is in same order as you are sending in the array. 将PHP更改为此,我认为答案与您在数组中发送的顺序相同。

   $count = 0;
   $sql = " SELECT * FROM questionpaper ";
   $query = mysqli_query($conn,$sql);
   $i=0;
   while($row=mysqli_fetch_array($query)){
   if ($array1[$i]==$row['answer']) {
    $count++;
    $i++;

    }
   echo $count;

if you can show how db structure is it will be easy to give you exact answer. 如果您可以显示数据库结构,将很容易为您提供准确的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM