gpt4 book ai didi

javascript - 无法从 phpmyAdmin 数据库表检索问题/信息

转载 作者:行者123 更新时间:2023-12-03 06:21:52 26 4
gpt4 key购买 nike

我使用了 YouTube 指南来创建在线测验,但我无法使用 MAMP 从我的本地主机数据库中检索问题。

以下是我正在使用的 php 文件:

测验.php

<?php
/*
Presentation & Test Quiz
*/
session_start();
if(isset($_GET['question'])){
$question = preg_replace('/[^0-9]/', "", $_GET['question']);
$next = $question + 1;
$prev = $question - 1;
if(!isset($_SESSION['qid_array']) && $question != 1){
$msg = "Sorry! No cheating.";
header("location: index.php?msg=$msg");
exit();
}
if(isset($_SESSION['qid_array']) && in_array($question, $_SESSION['qid_array'])){
$msg = "Sorry, Cheating is not allowed. You will now have to start over.";
unset($_SESSION['answer_array']);
unset($_SESSION['qid_array']);
session_destroy();
header("location: index.php?msg=$msg");
exit();
}
if(isset($_SESSION['lastQuestion']) && $_SESSION['lastQuestion'] != $prev){
$msg = "Sorry, Cheating is not allowed. You will now have to start over.";
unset($_SESSION['answer_array']);
unset($_SESSION['qid_array']);
session_destroy();
header("location: index.php?msg=$msg");
exit();
}
}
?>
<!doctype html>
<html lang="en">
<head>
<title>Quiz Page</title>
<meta charset="utf-8">
<script type="text/javascript">
function countDown(secs,elem) {
var element = document.getElementById(elem);
element.innerHTML = "You have "+secs+" seconds remaining.";
if(secs < 1) {
var xhr = new XMLHttpRequest();
var url = "userAnswers.php";
var vars = "radio=0"+"&qid="+<?php echo $question; ?>;
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function() {
if(xhr.readyState == 4 && xhr.status == 200) {
alert("You did not answer the question in the allotted time. It will be marked as incorrect.");
clearTimeout(timer);
}
}
xhr.send(vars);
document.getElementById('counter_status').innerHTML = "";
document.getElementById('btnSpan').innerHTML = '<h2>Times Up!</h2>';
document.getElementById('btnSpan').innerHTML += '<a href="quiz.php?question=<?php echo $next; ?>">Click here now</a>';

}
secs--;
var timer = setTimeout('countDown('+secs+',"'+elem+'")',1000);
}
</script>
<script>
function getQuestion(){
var hr = new XMLHttpRequest();
hr.onreadystatechange = function(){
if (hr.readyState==4 && hr.status==200){
var response = hr.responseText.explode("|");
if(response[0] == "finished"){
document.getElementById('status').innerHTML = response[1];
}
var nums = hr.responseText.explode(",");
document.getElementById('question').innerHTML = nums[0];
document.getElementById('answers').innerHTML = nums[1];
document.getElementById('answers').innerHTML += nums[2];
}
}
hr.open("GET", "questions.php?question=" + <?php echo $question; ?>, true);
hr.send();
}
function x() {
var rads = document.getElementsByName("rads");
for ( var i = 0; i < rads.length; i++ ) {
if ( rads[i].checked ){
var val = rads[i].value;
return val;
}
}
}
function post_answer(){
var p = new XMLHttpRequest();
var id = document.getElementById('qid').value;
var url = "userAnswers.php";
var vars = "qid="+id+"&radio="+x();
p.open("POST", url, true);
p.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
p.onreadystatechange = function() {
if(p.readyState == 4 && p.status == 200) {
document.getElementById("status").innerHTML = '';
alert("Thanks, Your answer was submitted"+ p.responseText);
var url = 'quiz.php?question=<?php echo $next; ?>';
window.location = url;
}
}
p.send(vars);
document.getElementById("status").innerHTML = "processing...";
}
</script>
<script>
window.oncontextmenu = function(){
return false;
}
</script>
<style type="text/css">
.header {
text align: center;
background-image: url(Header_graphic.png);
background-repeat: no-repeat;
padding-bottom: 20px;
}
#header-logo {
text-align: right;
padding-top: 20px;
padding-right: 20px;
}
h2 {
text-align: center;
font-size: 26px;
font-family: WeblySleek UI Light;
color: #FFFFFF;
margin-top: -44px;
}
.content {
padding-top:50px;
padding-left: 20px;
padding-right: 20px;
padding-bottom:50px;
font-family: WeblySleek UI;
font-size: 16px;
color: #4a4745;
}
#status {
font-size: 14px;
font-family: WeblySleek UI;
color: #4a4745;
}
#counter_status {
font-size: 14px;
font-family: WeblySleek UI;
color: #4a4745;
}
#question {
font-size: 14px;
font-family: WeblySleek UI;
color: #4a4745;
}
#answers {
font-size: 14px;
font-family: WeblySleek UI;
color: #4a4745;
}
.footer {
width: 100%;
margin-top: -10px;
}
</style>
</head>

<body onLoad="getQuestion()">
<div class="header">
<div id="header-logo">
<img src="ESGPLC Logo_white_for quiz.png" />
</div>
<h2>ESGPLC HEALTH, SAFETY & ENVIRONMENT QUIZ TEST</h2>
</div>

<div class="content">
<div id="status"></div>
<div id="counter_status"></div>
<div id="question"></div>
<div id="answers"></div>
</div>

<script type="text/javascript">countDown(60,"counter_status");</script>

<div class="footer">
<img src="http://localhost/quiz/ESGPLC_BSBS_Footer.png" />
</div>
</body>
</html>

还有这个,question.php:

<?php 
/*
Presentation & Test Quiz
*/
session_start();
require_once("scripts/connect_db.php");
$arrCount = "";
if(isset($_GET['question'])){
$question = preg_replace('/[^0-9]/', "", $_GET['question']);
$output = "";
$answers = "";
$q = "";
$sql = mysqli_query("SELECT id FROM questions");
$numQuestions = mysqli_num_rows($sql);
if(!isset($_SESSION['answer_array']) || $_SESSION['answer_array'] < 1){
$currQuestion = "1";
}else{
$arrCount = count($_SESSION['answer_array']);
}
if($arrCount > $numQuestions){
unset($_SESSION['answer_array']);
header("location: index.php");
exit();
}
if($arrCount >= $numQuestions){
echo 'finished|<p>There are no more questions. Please enter your first and last name and click next</p>
<form action="userAnswers.php" method="post">
<input type="hidden" name="complete" value="true">
<input type="text" name="username">
<input type="submit" value="Finish">
</form>';
exit();
}
$singleSQL = mysqli_query("SELECT * FROM questions WHERE id='$question' LIMIT 1");
while($row = mysqli_fetch_array($singleSQL)){
$id = $row['id'];
$thisQuestion = $row['question'];
$type = $row['type'];
$question_id = $row['question_id'];
$q = '<h2>'.$thisQuestion.'</h2>';
$sql2 = mysqli_query("SELECT * FROM answers WHERE question_id='$question' ORDER BY rand()");
while($row2 = mysqli_fetch_array($sql2)){
$answer = $row2['answer'];
$correct = $row2['correct'];
$answers .= '<label style="cursor:pointer;"><input type="radio" name="rads" value="'.$correct.'">'.$answer.'</label>
<input type="hidden" id="qid" value="'.$id.'" name="qid"><br /><br />
';
}
$output = ''.$q.','.$answers.',<span id="btnSpan"><button onclick="post_answer()">Submit</button></span>';
echo $output;
}
}
?>

connect-db.php

<?php
$db_host = 'localhost';
$db_username = 'root';
$db_pass = 'root';
$db_name = 'esgplc_hse_induction';

$con = mysqli_connect($db_host, $db_username, $db_pass, $db_name);

if (mysqli_connect_errno()) {

echo "Failed to connect to MySQL: " . mysqli_connect_error();

}
?>

我看不出哪里出了问题。我知道 MySQL 已更改为 mysqli,我已在代码中更新了它。

非常感谢,

最佳答案

正如我在评论中提到的。 mysqli_query() 函数需要向其传递一个数据库参数。

  • 您的查询均不包含该信息。

示例:

$singleSQL = mysqli_query($con, "Your query...");

并将该逻辑应用于您的所有查询。

引用:

还检查错误:

还考虑使用准备好的语句:

关于javascript - 无法从 phpmyAdmin 数据库表检索问题/信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38831462/

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