MySQL 데이터베이스에서 동적으로 생성 된 퀴즈를 제공하는 기본 웹 사이트를 구축 중입니다. 현재 데이터베이스 스키마를 기반으로 퀴즈 웹 앱의 여러 질문에 '선택 사항'을 어떻게 생성하는지 이해하는 데 어려움을 겪고 있습니다.PHP를 사용하여 데이터베이스에서 HTML 폼 생성
// If this user has never taken this quiz, insert empty responses into the quiz_response table
$query = "SELECT * FROM quiz_response WHERE user_id = '" . $_SESSION['user_id'] . "'";
$data = mysqli_query($dbc, $query);
if (mysqli_num_rows($data) == 0) {
//First grab the list of questions to create empty responses
//Grab all questions from question table
//Rework code in the future to accommodate multiple quizes
$query = "SELECT question_id from question";
$data = mysqli_query($data, $query);
$questionIDs = array();
while ($row = mysqli_fetch_array($data)) {
array_push($questionIDs, $row['question_id']);
}
// Insert empty response rows into the response table, one row per question
foreach ($questionIDs as $question_id) {
$query = "INSERT INTO quiz_response (user_id, question_id) VALUES ('" . $_SESSION['user_id']. "', '$question_id')";
mysqli_query($dbc, $query);
}
}
// If the quiz form has been submitted, write the form responses to the database
if (isset($_POST['submit'])) {
// Write the quiz response rows to the response table
foreach ($_POST as $response_id => $response) {
$query = "UPDATE quiz_response SET response = '$response' WHERE response_id = '$response_id'";
mysqli_query($dbc, $query);
}
echo '<p>Your responses have been saved.</p>
}
// Grab the response data from the database to generate the form
$query = "SELECT qr.response_id, qr.question_id, qr.response, q.question, quiz.quiz " .
"FROM quiz_response AS qr " .
"INNER JOIN question AS q USING (question_id) " .
"INNER JOIN quiz USING (quiz_id) " .
"WHERE qr.user_id = '" . $_SESSION['user_id'] . "'";
$data = mysqli_query($dbc, $query);
$responses = array();
while ($row = mysqli_fetch_array($data)) {
// Pull up the choices for each question
$query2 = "SELECT choice_id, choice FROM question_choice " .
"WHERE question_id = '" . $row['question_id'] . "'";
$data2 = mysqli_query($dbc, $query2);
$choices = array();
while ($row2 = mysqli_fetch_array($data2)) {
array_push($choices, $row2);
}
// Rename choices
// Eventually push choices into $responses array
// array_push($responses, $row);
}
mysqli_close($dbc);
// Generate the quiz form by looping through the response array
echo '<form method="post" action="' . $_SERVER['PHP_SELF'] . '">';
echo '<h2>' . $page_title . '</h2>';
$question_title = $responses[0]['question'];
echo '<label for="' . $responses[0][response_id'] . '">' . $responses[0]['question'] . '</label><br />';
foreach ($responses as $response) {
// Only start a new question if the question changes
if ($question_title != $response['question']) {
$question_title = $response['question'];
echo '<br /><label for="' . $response['response_id'] . '">' . $response['question'] . '</label><br />';
}
// Display the choices
// Choice 1
// Choice 2
// Choice 3
// Choice 4
}
echo '<br /><br />';
echo '<input type="submit" value="Grade Me!" name="submit" />';
echo '</form>';
나는 문제가 밖으로 선택 옵션을 당기는 데 : 여기
CREATE TABLE user (
user_id INT UNSIGNED PRIMARY KEY,
username VARCHAR(32) NOT NULL UNIQUE,
password VARCHAR(128) NOT NULL,
...
) Engine=InnoDB;
CREATE TABLE quiz (
quiz_id INT UNSIGNED PRIMARY KEY,
title VARCHAR(64)
) Engine=InnoDB;
CREATE TABLE question (
question_id INT UNSIGNED PRIMARY KEY,
quiz_id INT UNSIGNED NOT NULL,
question VARCHAR(1024),
FOREIGN KEY (quiz_id) REFERENCES quiz (quiz_id)
) Engine=InnoDB;
CREATE TABLE question_choices (
choice_id INT UNSIGNED PRIMARY KEY,
question_id INT UNSIGNED NOT NULL,
is_correct_choice TINYINT(1),
choice VARCHAR(512),
FOREIGN KEY (question_id) REFERENCES question (question_id)
) Engine=InnoDB;
CREATE TABLE quiz_response (
response_id INT UNSIGNED PRIMARY KEY,
user_id INT UNSIGNED NOT NULL,
question_id INT UNSIGNED NOT NULL,
response INT UNSIGNED NOT NULL,
is_correct TINYINT(1),
answer_time FLOAT,
UNIQUE KEY (user_id, question_id)
FOREIGN KEY (user_id) REFERENCES user (user_id),
FOREIGN KEY (question_id) REFERENCES question (question_id),
FOREIGN KEY (response) REFERENCES question_choices (choice_id),
) Engine=InnoDB;
내가 내 quiz.php 스크립트에서 지금까지 생산 코드입니다 : 여기
는 데이터베이스 스키마입니다 question_choice 테이블을 열고 폼을 채우는 데 사용합니다. choice_id 및 choice 열을
$responses
배열에 넣고 이름을 바꾸지 않고 생성 양식 섹션에서 액세스 할 수 있습니까? 이 시점에서 나는 이름을 바꿀 필요가 있다고 느낍니다. 어떤 도움이라도 대단히 감사하겠습니다!
정확히 무엇이 문제입니까? '$ choices'로 올바른 방향으로 가고있는 것 같습니다. –
4 가지 선택 사항을 구분하는 데 문제가 있습니다. 테이블에서 꺼내어 $ respond 배열에 붙여 넣으면 차이를 구별 할 방법이 생기지 않습니다. 그래서, $ respond 배열에 넣기 전에 제목을 변경해야한다고 생각합니다. 정확히 어떻게 할 지 확신하지 못합니다 ... – Abundnce10
스키마를 보여주기위한 고유 한 방법을 고안하는 대신 표준 SQL 문을 사용하십시오. 잘 알려진 표준을 따르는 것 외에도 [예] (http://sscce.org/)는 자체 포함됩니다. 명령문에 직접 값을 삽입하는 대신 준비된 명령문을 사용하십시오.이 명령문은 쿼리를 반복 할 때보다 안전하고 성능이 좋습니다. 양식을 레이아웃하기 위해 '
'을 사용하지 말고, CSS (또는 의미 론적 요소 일 것임)를 사용하십시오.누락 된 작은 따옴표가 프로덕션 코드를 대표합니까? – outis