你如何将数组与许多元素比较到PHP中的数据库?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了你如何将数组与许多元素比较到PHP中的数据库?相关的知识,希望对你有一定的参考价值。
我已经建立了一个在线考试门户网站,其中用户选择的不同无线电选项存储在一个数组中,然后我将该数组发送到一个php文件中以检查数据库,这就是我面临的问题。答案在数据库中,在我获取它们之后我将它们与数组进行比较,但它没有得到解决。请帮忙 。
这是答案来自的表格问题: -
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
这是我发送它的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);
}
});
}
这是完成所有这些的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;
?>
错误:-
Notice: Undefined index: myarray in D:XAMPPhtdocsassignmentscheck.php on line 8
Notice: Undefined offset: 1 in D:XAMPPhtdocsassignmentscheck.php on line 20
Notice: Undefined offset: 2 in D:XAMPPhtdocsassignmentscheck.php on line 20
Notice: Undefined offset: 3 in D:XAMPPhtdocsassignmentscheck.php on line 20
Notice: Undefined offset: 4 in D:XAMPPhtdocsassignmentscheck.php on line 20
Notice: Undefined offset: 5 in D:XAMPPhtdocsassignmentscheck.php on line 20
Notice: Undefined offset: 6 in D:XAMPPhtdocsassignmentscheck.php on line 20
Notice: Undefined offset: 7 in D:XAMPPhtdocsassignmentscheck.php on line 20
Notice: Undefined offset: 8 in D:XAMPPhtdocsassignmentscheck.php on line 20
Notice: Undefined offset: 9 in D:XAMPPhtdocsassignmentscheck.php on line 20
Notice: Undefined offset: 10 in D:XAMPPhtdocsassignmentscheck.php on line 20
Notice: Undefined offset: 11 in D:XAMPPhtdocsassignmentscheck.php on line 20
Notice: Undefined offset: 12 in D:XAMPPhtdocsassignmentscheck.php on line 20
Notice: Undefined offset: 13 in D:XAMPPhtdocsassignmentscheck.php on line 20
Notice: Undefined offset: 14 in D:XAMPPhtdocsassignmentscheck.php on line 20
Notice: Undefined offset: 15 in D:XAMPPhtdocsassignmentscheck.php on line 20
Your total score is0
答案
改变这一点
var arr = [];
var Selected = document.querySelector('[name="op"]:checked');
if (Selected!= null) {
arr.push(Selected.value);
}
console.log(arr);
将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;
如果你能展示数据库结构是多么容易,你会很容易给出确切的答案。
以上是关于你如何将数组与许多元素比较到PHP中的数据库?的主要内容,如果未能解决你的问题,请参考以下文章