gpt4 book ai didi

php - fetch_assoc 返回缺少的行

转载 作者:可可西里 更新时间:2023-11-01 08:24:35 25 4
gpt4 key购买 nike

我从数据库表中读取了一些问题,并在内部循环中使用外键从另一个表中获取了可能的答案。

显示的答案行应该是固定的 5 行,但它总是只返回 4 行,内循环中的答案的第一行似乎总是丢失。

$_POST['nbr']是题目数

$_POST['type'] 是问题的类型

$row 用于问题 $row2 用于与

相关的建议答案

这是代码

<?php 
// session
session_start();
?>
<html>
<head>
<meta charset="UTF-8" >
<title>passé un examen</title>

</head>
<body>

<form method = "post" action ="" >
<b>Specialité : </b>

<select name="sp">
<?php
require_once("connect_db.php");
$result = $connect->query("Select nom from specialite");
while($row = $result->fetch_assoc()){
echo '<option>'.$row['nom'].'</option>'.PHP_EOL;
}
?>
</select>

<b>choisi le nombre de questions : </b><input type="number" name="nbr" step="1" />
<b>type de question : </b>

<select name="type">
<option value="qcm" >QCM</option>
<option value="qcs">QCS</option>
<option value="qc2">LES DEUX</option>
</select>

<input type="submit" name="submit" value="générer" />
</form>

<?php
$count=1;
$count2=1;
$type;

if(isset($_POST['submit'])){

if($_POST['nbr'] <= 0)
die('choisi au moin une question');

$_SESSION['type']=$_POST['type'];
$_SESSION['nbr_qst']=$_POST['nbr'];
echo '<form action="" method="post" >';
//TYPE QCS
if($_POST['type']=="qcs"){

$result = $connect->query("SELECT * FROM question WHERE specialite = '".$_POST['sp']."' ORDER BY RAND() LIMIT ".$_POST['nbr']." ");
if(($result->num_rows == 0) ||($result->num_rows < $_POST['nbr']))
die('questions dans cette specialité est insuffusant');
echo '<p><b>Question QCS</b></p>'.PHP_EOL;
while($row = $result->fetch_assoc()){

echo 'Question '.$count.' : '.$row['question'].'<br>'.PHP_EOL;

echo '<input type="hidden" value="'.$row['question'].'" name="q'.$count.'" ';
$result2=$connect->query("SELECT * FROM reponses WHERE id_Q=".$row['id_Q']);
while($row2 = $result2->fetch_assoc()){

echo $count2.'- '.$row2['reponse'].'<input type="radio" name="srep'.$count.'" value="'.$row2['iscorrect'].'" /><br>'.PHP_EOL;
$count2++;
}
$count2=1;
$count++;
}


}
//Type QCM
if($_POST['type']=="qcm"){
$result = $connect->query("SELECT * FROM question_qcm WHERE specialite = '".$_POST['sp']."' ORDER BY RAND() LIMIT ".$_POST['nbr']." ");
if(($result->num_rows == 0) ||($result->num_rows < $_POST['nbr']))
die('questions dans cette specialité est insuffusant');

echo '<p><b>Question QCM</b></p>';
while($row = $result->fetch_assoc()){

echo 'Question '.$count.' : '.$row['question'].'<br>'.PHP_EOL;
// get the question primary key
echo '<input type="hidden" value="'.$row['id_qcm'].'" name="q'.$count.'" ';
$result2=$connect->query("SELECT * FROM reponse_qcm WHERE id_qcm=".$row['id_qcm']);
while($row2 = $result2->fetch_assoc()){
echo $count2.'- '.$row2['reponse'].'<input type="checkbox" name="mrep'.$count.$count2.'" value="'.$row2['id'].'" /><br>'.PHP_EOL;
$count2++;
}
$count2=1;
$count++;
}

}

//Type de deux
if($_POST['type']=="qc2"){
$nbr=$_POST['nbr']/2;
if($_POST['nbr']==1)
die('choisi au moin deux question pour le type <b>LES DEUX</b>'.PHP_EOL);


$result = $connect->query("SELECT * FROM question WHERE specialite = '".$_POST['sp']."' ORDER BY RAND() LIMIT ".(int)$nbr." ");
if(($result->num_rows == 0) )
die('questions QCS dans cette specialité est insuffusant');

echo '<p><b>Question QCS</b></p>'.PHP_EOL;
while($row = $result->fetch_assoc()){

echo 'Question '.$count.' : '.$row['question'].'<br>';
echo '<input type="hidden" value="'.$row['id_Q'].'" name="sq'.$count.'" '.PHP_EOL;
$result2=$connect->query("SELECT * FROM reponses WHERE id_Q=".$row['id_Q']);
while($row2 = $result2->fetch_assoc()){
echo $count2.'- '.$row2['reponse'].'<input type="radio" name="srep'.$count.'" /><br>'.PHP_EOL;
$count2++;
}
$count2=1;
$count++;
}







$nbr = ($_POST['nbr']/2)+($_POST['nbr']%2);

$result1 = $connect->query("SELECT * FROM question_qcm WHERE specialite = '".$_POST['sp']."' ORDER BY RAND() LIMIT ".(int)$nbr." ");
if(($result1->num_rows == 0))
die('questions QCM dans cette specialité est insuffusant');


echo '<p><b>Question QCM</b></p>';
while($row = $result1->fetch_assoc()){

echo 'Question '.$count.' : '.$row['question'].'<br>'.PHP_EOL;
echo '<input type="hidden" value="'.$row['id_qcm'].'" name="mq'.$count.'" ';
$result2=$connect->query("SELECT * FROM reponse_qcm WHERE id_qcm=".$row['id_qcm']);
while($row2 = $result2->fetch_assoc()){
echo $count2.'- '.$row2['reponse'].'<input type="radio" name="mrep'.$count.'" /><br>'.PHP_EOL;
$count2++;
}
$count2=1;
$count++;
}

}





echo '<input type="submit" name="valider" value="valider" />';





}
?>

<br><br>

</form>










<?php
if(isset($_POST['valider'])){

//IF THERE's NOT a GENERATION YET

//IF ALL Question are answered


$Score=0;

//TYPE QCS
if($_SESSION['type']=="qcs"){
$Score_Finale = $_SESSION['nbr_qst'];
for($i=1;$i<=$_SESSION['nbr_qst'];$i++){

if(isset($_POST['srep'.$i])){

$Score+=$_POST['srep'.$i];
}



}

die('<p><b>Ton Score est : '.$Score.'/'.$Score_Finale.'</b></p>');
}
/*
//TYPE QCM (deux boule c tt nbrquest,5)
if($_SESSION['type']=="qcm"){
$Score_Finale = $_SESSION['nbr_qst'];
for($i=1;$i<=$_SESSION['nbr_qst'];$i++){
for($j=1;$j<=5;$j++){

if(isset($_POST['mrep'.$i.$j])){

$result = $connect->query("Select * FROM reponse_qcm where id=".$_POST['mrep'.$i.$j]);
$row = $result->fetch_assoc();
$Score = $row['bareme'];

}


}



}


die('<p><b>Ton Score est : '.$Score.'/'.$Score_Finale.'</b></p>');
}
*/

}


// Il FAUT vérifier que le bareme totale de question est < 1 est la bonne réponse tjr = 1
?>



</body>
</html>

数据库截图

questions table

responses table

浏览器结果截图

output

**过了一会儿我找到了解决方案**

我没有做两个循环,而是为第二个循环创建了一个函数并返回每个问题的答案

函数:

function getRepS($id){

require_once("connect_db.php");
global $connect;
$result = $connect->query("SELECT * FROM reponses WHERE id_Q=".$id);
$count=0;
$Send;
while($row = $result->fetch_assoc()){

$Send[$count] = $row;
$count++;
}
return $Send;
}

主脚本:

    if($_POST['type']=="qcs"){
echo '################';
$result2=$connect->query("SELECT * FROM reponses WHERE id_Q=7");
while($row2 = $result2->fetch_assoc()){

echo $row2['reponse'].'<br>';


}
echo '################';


$result = $connect->query("SELECT * FROM question WHERE specialite = '".$_POST['sp']."' ORDER BY RAND() LIMIT ".(int)$_POST['nbr']." ");
if(($result->num_rows == 0) ||($result->num_rows < $_POST['nbr']))
die('questions dans cette specialité est insuffusant');
echo '<p><b>Question QCS</b></p>'.PHP_EOL;
while($row = $result->fetch_assoc()){

echo 'Question '.$count.' : '.$row['question'].'<br>'.PHP_EOL;

echo '<input type="hidden" value="'.$row['question'].'" name="q'.$count.'" ';

$row2=getRepS($row['id_Q']);

$count2=0;
while($count2 != count($row2)){

echo ($j+1).'- '.$row2[$j]['reponse'].'<input type="radio" name="srep'.$count.'" value="'.$row2[$j]['iscorrect'].'" /><br>'.PHP_EOL;
$count2++;
}






$count++;
}


}

最佳答案

由于评论中的代码可读性不是很好,所以我写了一些建议来寻找错误作为答案。如果它没有帮助,我会删除它。

在您的开发服务器上(不是生产服务器!)在您的网络根目录中添加一个文件 .htaccess(或编辑现有文件)。

php_flag display_startup_errors on
php_flag display_errors on
php_flag html_errors on
php_flag log_errors on
php_value error_reporting -1
php_value error_log /path/to/your/webproject/phperror.log

这应该显示并记录 php 中的任何错误。

查看生成的 HTML 页面的源代码并将其发布在这里。

安装 xdebug在您的开发服务器上。 XAMPP 已经包含 xdebug。使用像 Netbeans、Eclipse 或 PhpStorm 这样的 IDE 来利用 xdebug 的调试功能。您可以逐个语句跟踪您的代码,观察当前变量状态。设置完成后,您再也不想工作了。

关于php - fetch_assoc 返回缺少的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43702522/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com