我正在建立一个网站以提交调查问卷答案。
每个问题都是由管理员从信息中心创建的,目标是通过从数据库中获取问题和这些问题的答案来使页面动态化。
基本上是这样:
因此,目标是当我单击其中一个按钮的答案时,显示下一个问题(从数据库中获取第二个问题以及与该问题相关的三个答案,依此类推。)
对于后端开发,由于我是编程新手,所以我使用纯PHP,并使用PDO
处理来自数据库的数据。
为了管理我正在使用Controllers的数据,并与数据库连接,我已经在/core/Database.php
上建立了连接
感谢您的帮助。.谢谢您的时间
这是我到目前为止尝试过的。
<?php
session_start();
require './controllers/UserController.php';
$user = new UserController;
$arr = $users=$user->getQuestions();
$index = 0;
$answ1 = $user->getAnswers();
$index = 0;
$_SESSION['answerOne'] = $_POST['question1'];
// $_SESSION['answerTwo'] = $_POST['question2'];
// $_SESSION['answerThree'] = $_POST['question3'];
// $_SESSION['answerFour'] = $_POST['question4'];
echo $_SESSION['answerOne'];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="./styling/questionstyle.css">
<title>Question</title>
</head>
<body>
<div class="hero">
<div class="questionContainer">
<div class="header">
<img src="./assets/back.png" height="35%" class="backImage">
<h2 class="questionNumber">01/04</h2>
</div>
<progress max="100" value="10"></progress>
<h1 class="question">
<?php echo $arr[$index]['description'] ?>
</h1>
</div>
<div class="answerContainer">
<img src="./assets/shadow.png" width="100%">
<form action="" method="POST">
<button class="button" onclick="document.getElementById('q1').value ='a'"><h4 class="choice">a</h4><h4 class="answer"><?php echo $answ1[$index]['choice1']?></h4></button> <br>
<button class="button2" onclick="document.getElementById('q1').value ='b'"><h4 class="choice">b</h4><h4 class="answer"><?php echo $answ1[$index]['choice2']?></h4></button> <br>
<button class="button2"onclick="document.getElementById('q1').value ='c'" ><h4 class="choice">c</h4><h4 class="answer"><?php echo $answ1[$index]['choice3']?></h4></button>
<input type="hidden" id="q1" name="question1">
<input type="hidden" id="q2" name="question2">
<input type="hidden" id="q3" name="question3">
<input type="hidden" id="q4" name="question4">
<br>
</form>
</div>
</div>
</body>
</html>
<?php
include './core/Database.php';
?>
class UserController
{
protected $db;
public function __construct()
{
$this->db = new Database;
}
public function all()
{
$query = $this->db->pdo->query('SELECT * FROM user');
return $query->fetchAll();
}
public function store($request)
{
isset($request['is_admin']) ? $isAdmin = 1 : $isAdmin = 0;
$password = password_hash($request['password'], PASSWORD_DEFAULT);
$query = $this->db->pdo->prepare('INSERT INTO users (name, email, password, is_admin) VALUES (:name, :email, :password, :is_admin)');
$query->bindParam(':name', $request['fullName']);
$query->bindParam(':email', $request['email']);
$query->bindParam(':password', $password);
$query->bindParam(':is_admin', $isAdmin);
$query->execute();
return header('Location: ./index.php');
}
public function edit($id)
{
$query = $this->db->pdo->prepare('SELECT * FROM users WHERE id = :id');
$query->execute(['id' => $id]);
return $query->fetch();
}
public function update($id, $request)
{
isset($request['is_admin']) ? $isAdmin = 1 : $isAdmin = 0;
$query = $this->db->pdo->prepare('UPDATE users SET name = :name, email = :email, is_admin = :is_admin WHERE id = :id');
$query->execute([
'name' => $request['fullName'],
'email' => $request['email'],
'is_admin' => $isAdmin,
'id' => $id
]);
return header('Location: ./index.php');
}
public function destroy($id)
{
$query = $this->db->pdo->prepare('DELETE FROM users WHERE id = :id');
$query->execute(['id' => $id]);
return header('Location: ./index.php');
}
public function totalpa(){
$query= $this->db->pdo->query('SELECT count(*) FROM user');
$query->execute();
return $query->fetchColumn();
}
public function totalfemra(){
$query= $this->db->pdo->query("SELECT count(*) FROM user WHERE gender='F'");
$query->execute();
return $query->fetchColumn();
}
public function totalmeshkuj(){
$query=$this->db->pdo->query("SELECT count(*) from user WHERE gender ='M'");
$query->execute();
return $query->fetchColumn();
}
public function storeUser($req){
$query = $this->db->pdo->prepare('INSERT INTO user (city, gender, age) VALUES (:city, :gender, :age)');
$query->bindParam(':city', $_SESSION['cityID']);
$query->bindParam(':gender', $_SESSION['gender']);
$query->bindParam(':age', $_SESSION['age']);
$query->execute();
}
public function storeQuestion($req){
$query=$this->db->pdo->prepare('INSERT INTO question(description) VALUES(:description)');
$query->bindParam(':description',$_POST['pyetja']);
$query->execute();
}
public function storeAnswer($param){
$query=$this->db->pdo->prepare('INSERT into add_answer (q_id,choice1,choice2,choice3) VALUES(:q_id,:ch1,:ch2,:ch3)');
$query->bindParam(':q_id',$param);
$query->bindParam(':ch1', $_POST['choice1']);
$query->bindParam(':ch2',$_POST['choice2']);
$query->bindParam(':ch3',$_POST['choice3']);
$query->execute();
}
public function getLastInsertedId(){
$query=$this->db->pdo->prepare('SELECT MAX(qid) from question');
$query->execute();
return $query->fetchColumn();
}
public function getQuestions(){
$query= $this->db->pdo->query('SELECT * FROM question');
$query->execute();
return $query->fetchAll();
}
public function getAnswers(){
try{
$query =$this->db->pdo->query('SELECT * FROM add_answer a INNER JOIN question q ON a.q_id = q.qid');
$query -> execute();
return $query->fetchAll();
}catch(Exception $ex){
echo 'error';
}
}
}