用户输入分为3个类别,即DEPRESSION,STRESS和ANXIETY。我想根据类别计算用户的answer_value。抑郁阵列将单独计算与焦虑和压力阵列相同。有人可以帮忙:(
TABLES
usersTbl (user_id, first_name, last_name, email, pwd);
questionTbl (question_id, question_no, ques_category, question_desc);
answerTbl (answer_id, user_id, question_no, answer_value);
recordTbl (record_id, user_id, depression_score, stress_score, anxiety_score);
question.php
<?php
include 'includes/test.inc.php';?><br><br>
<form action="submit_answer.php" method="POST">
<center>
<table>
<tr>
<th><center>Question</center></th>
<th><center>0</center></th>
<th><center>1</center></th>
<th><center>2</center></th>
<th><center>3</center></th>
</tr>
<tr>
<td>I found it hard to wind down.</td>
<td><center><input id="stress11" name="stress1" type="radio" value="0"/></center></td>
<td><center><input id="stress12" name="stress1" type="radio" value="1"/></center></td>
<td><center><input id="stress13" name="stress1" type="radio" value="2"/></center></td>
<td><center><input id="stress14" name="stress1" type="radio" value="3"/></center></td>
</tr>
<tr>
<td>I was aware of the dryness of my mouth.</td>
<td><center><input id="anxiety11" name="anxiety1" type="radio" value="0"/></center></td>
<td><center><input id="anxiety12" name="anxiety1" type="radio" value="1"/></center></td>
<td><center><input id="anxiety13" name="anxiety1" type="radio" value="2"/></center></td>
<td><center><input id="anxiety14" name="anxiety1" type="radio" value="3"/></center></td>
</tr>
<tr>
<td>I couldn't seem to experience any positive feeling at all.</td>
<td><center><input id="depression11" name="depression1" type="radio" value="0"/></center></td>
<td><center><input id="depression12" name="depression1" type="radio" value="1"/></center></td>
<td><center><input id="depression13" name="depression1" type="radio" value="2"/></center></td>
<td><center><input id="depression14" name="depression1" type="radio" value="3"/></center></td>
</tr>
<tr>
<td>I experienced breathing difficulty(i.e excessively rapid breathing,<br>breathlessnes in the absence of physical exertion)</td>
<td><center><input id="anxiety21" name="anxiety2" type="radio" value="0"/></center></td>
<td><center><input id="anxiety22" name="anxiety2" type="radio" value="1"/></center></td>
<td><center><input id="anxiety23" name="anxiety2" type="radio" value="2"/></center></td>
<td><center><input id="anxiety24" name="anxiety2" type="radio" value="3"/></center></td>
</tr>
<tr>
<td>I found it difficult to work up on the initiative to do things</td>
<td><center><input id="depression21" name="depression2" type="radio" value="0"/></center></td>
<td><center><input id="depression22" name="depression2" type="radio" value="1"/></center></td>
<td><center><input id="depression23" name="depression2" type="radio" value="2"/></center></td>
<td><center><input id="depression24" name="depression2" type="radio" value="3"/></center></td>
</tr>
<tr>
<td>I tended to over-react to situations.</td>
<td><center><input id="stress21" name="stress2" type="radio" value="0"/></center></td>
<td><center><input id="stress22" name="stress2" type="radio" value="1"/></center></td>
<td><center><input id="stress23" name="stress2" type="radio" value="2"/></center></td>
<td><center><input id="stress24" name="stress2" type="radio" value="3"/></center></td>
</tr>
<tr>
<td>I experienced trembling (i.e. in the hands)</td>
<td><center><input id="anxiety31" name="anxiety3" type="radio" value="0"/></center></td>
<td><center><input id="anxiety32" name="anxiety3" type="radio" value="1"/></center></td>
<td><center><input id="anxiety33" name="anxiety3" type="radio" value="2"/></center></td>
<td><center><input id="anxiety34" name="anxiety3" type="radio" value="3"/></center></td>
</tr>
<tr>
<td>I felt that I was using a lot of nervous energy.</td>
<td><center><input id="stress31" name="stress3" type="radio" value="0"/></center></td>
<td><center><input id="stress32" name="stress3" type="radio" value="1"/></center></td>
<td><center><input id="stress33" name="stress3" type="radio" value="2"/></center></td>
<td><center><input id="stress34" name="stress3" type="radio" value="3"/></center></td>
</tr>
<tr>
<td>I was worried about situations in which I might panic and make a fool of myself.</td>
<td><center><input id="anxiety41" name="anxiety4" type="radio" value="0"/></center></td>
<td><center><input id="anxiety42" name="anxiety4" type="radio" value="1"/></center></td>
<td><center><input id="anxiety43" name="anxiety4" type="radio" value="2"/></center></td>
<td><center><input id="anxiety44" name="anxiety4" type="radio" value="3"/></center></td>
</tr>
<tr>
<td>I felt that I had nothing to look forward to.</td>
<td><center><input id="depression31" name="depression3" type="radio" value="0"/></center></td>
<td><center><input id="depression32" name="depression3" type="radio" value="1"/></center></td>
<td><center><input id="depression33" name="depression3" type="radio" value="2"/></center></td>
<td><center><input id="depression34" name="depression3" type="radio" value="3"/></center></td>
</tr>
<tr>
<td>I found myself getting agitated.</td>
<td><center><input id="stress41" name="stress4" type="radio" value="0"/></center></td>
<td><center><input id="stress42" name="stress4" type="radio" value="1"/></center></td>
<td><center><input id="stress43" name="stress4" type="radio" value="2"/></center></td>
<td><center><input id="stress44" name="stress4" type="radio" value="3"/></center></td>
</tr>
<tr>
<td>I found it difficult to relax.</td>
<td><center><input id="stress51" name="stress5" type="radio" value="0"/></center></td>
<td><center><input id="stress52" name="stress5" type="radio" value="1"/></center></td>
<td><center><input id="stress53" name="stress5" type="radio" value="2"/></center></td>
<td><center><input id="stress54" name="stress5" type="radio" value="3"/></center></td>
</tr>
<tr>
<td>I felt down-hearted and blue.</td>
<td><center><input id="depression41" name="depression4" type="radio" value="0"/></center></td>
<td><center><input id="depression42" name="depression4" type="radio" value="1"/></center></td>
<td><center><input id="depression43" name="depression4" type="radio" value="2"/></center></td>
<td><center><input id="depression44" name="depression4" type="radio" value="3"/></center></td>
</tr>
<tr>
<td>I was intolerant of anything that kept me from getting on with what I was doing.</td>
<td><center><input id="stress61" name="stress6" type="radio" value="0"/></center></td>
<td><center><input id="stress62" name="stress6" type="radio" value="1"/></center></td>
<td><center><input id="stress63" name="stress6" type="radio" value="2"/></center></td>
<td><center><input id="stress64" name="stress6" type="radio" value="3"/></center></td>
</tr>
<tr>
<td>I felt that I was so close to panic.</td>
<td><center><input id="anxiety51" name="anxiety5" type="radio" value="0"/></center></td>
<td><center><input id="anxiety52" name="anxiety5" type="radio" value="1"/></center></td>
<td><center><input id="anxiety53" name="anxiety5" type="radio" value="2"/></center></td>
<td><center><input id="anxiety54" name="anxiety5" type="radio" value="3"/></center></td>
</tr>
<tr>
<td>I was unable to become enthusiastic about anything.</td>
<td><center><input id="depression51" name="depression5" type="radio" value="0"/></center></td>
<td><center><input id="depression52" name="depression5" type="radio" value="1"/></center></td>
<td><center><input id="depression53" name="depression5" type="radio" value="2"/></center></td>
<td><center><input id="depression54" name="depression5" type="radio" value="3"/></center></td>
</tr>
<tr>
<td>I felt that I wasnt worth as much as a person.</td>
<td><center><input id="depression61" name="depression6" type="radio" value="0"/></center></td>
<td><center><input id="depression62" name="depression6" type="radio" value="1"/></center></td>
<td><center><input id="depression63" name="depression6" type="radio" value="2"/></center></td>
<td><center><input id="depression64" name="depression6" type="radio" value="3"/></center></td>
</tr>
<tr>
<td>I felt that I was rather touchy.</td>
<td><center><input id="stress71" name="stress7" type="radio" value="0"/></center></td>
<td><center><input id="stress72" name="stress7" type="radio" value="1"/></center></td>
<td><center><input id="stress73" name="stress7" type="radio" value="2"/></center></td>
<td><center><input id="stress74" name="stress7" type="radio" value="3"/></center></td>
</tr>
<tr>
<td>I was aware of the action of my heart in the absence of physical exertion(i.e. sense of heart rate increases)</td>
<td><center><input id="anxiety61" name="anxiety6" type="radio" value="0"/></center></td>
<td><center><input id="anxiety62" name="anxiety6" type="radio" value="1"/></center></td>
<td><center><input id="anxiety63" name="anxiety6" type="radio" value="2"/></center></td>
<td><center><input id="anxiety64" name="anxiety6" type="radio" value="3"/></center></td>
</tr>
<tr>
<td>I felt scared without any good reason.</td>
<td><center><input id="anxiety71" name="anxiety7" type="radio" value="0"/></center></td>
<td><center><input id="anxiety72" name="anxiety7" type="radio" value="1"/></center></td>
<td><center><input id="anxiety73" name="anxiety7" type="radio" value="2"/></center></td>
<td><center><input id="anxiety74" name="anxiety7" type="radio" value="3"/></center></td>
</tr>
<tr>
<td>I felt that life was meaningless.</td>
<td><center><input id="depression71" name="depression7" type="radio" value="0"/></center></td>
<td><center><input id="depression72" name="depression7" type="radio" value="1"/></center></td>
<td><center><input id="depression73" name="depression7" type="radio" value="2"/></center></td>
<td><center><input id="depression74" name="depression7" type="radio" value="3"/></center></td>
</tr>
</table><br>
<button type="submit" value="insert" name="submit">submit</button>
</center>
</form>
?>
submit_answer.php
<?php
session_start();
if (isset($_POST['submit'])) {
include_once("C:/xampp/htdocs/questionnaire/includes/dbh.inc.php");
$answers = array();
$uid = $_SESSION['u_id'];
// Fill the answers array with answers
$answers[1] = $_POST['stress1'];
$answers[2] = $_POST['anxiety1'];
$answers[3] = $_POST['depression1'];
$answers[4] = $_POST['anxiety2'];
$answers[5] = $_POST['depression2'];
$answers[6] = $_POST['stress2'];
$answers[7] = $_POST['anxiety3'];
$answers[8] = $_POST['stress3'];
$answers[9] = $_POST['anxiety4'];
$answers[10] = $_POST['depression3'];
$answers[11] = $_POST['stress4'];
$answers[12] = $_POST['stress5'];
$answers[13] = $_POST['depression4'];
$answers[14] = $_POST['stress6'];
$answers[15] = $_POST['anxiety5'];
$answers[16] = $_POST['depression5'];
$answers[17] = $_POST['depression6'];
$answers[18] = $_POST['stress7'];
$answers[19] = $_POST['anxiety6'];
$answers[20] = $_POST['anxiety7'];
$answers[21] = $_POST['depression7'];
// loop through the answers array
for ($i=1;$i<22;$i++) {
// $i contains the ques_no (1,2,3,4,5,....)
// $answers[$i] contains the answer value
$answer = $answers[$i];
// Let's control the user input
switch ($answer){
case 0:
case 1:
case 2:
case 3:
// OK
break;
default:
// This is abnormal, we might face a hacker, lets reset the answer to nothing.
$answer = '';
}
// all done, ready to insert
$sql = "INSERT INTO record (user_id, question_no, answer_value)
VALUES ('$uid', '$i', '$answer');";
mysqli_query($conn, $sql);
}
exit ();
}
else {
header("Location: ../index.php");
exit ();
}
?>
答案 0 :(得分:0)
我希望这些查询对您有用。
SELECT usersTbl.user_id, usersTbl.first_name, usersTbl.last_name, usersTbl.email
, SUM(COALESCE(CASE WHEN questionTbl.ques_category = 'DEPRESSION' THEN answerTbl.answer_value END,0)) depression_score
, SUM(COALESCE(CASE WHEN questionTbl.ques_category = 'STRESS' THEN answerTbl.answer_value END,0)) stress_score
, SUM(COALESCE(CASE WHEN questionTbl.ques_category = 'ANXIETY' THEN answerTbl.answer_value END,0)) anxiety_score
FROM usersTbl
Left JOIN answerTbl ON answerTbl.user_id = usersTbl.user_id,
Left JOIN questionTbl ON questionTbl.question_id = answerTbl.question_no,
GROUP
BY usersTbl.user_id;
如果您有任何疑问,请在评论部分发表评论
答案 1 :(得分:0)
试试这个
更改您的单选按钮名称,
<form action="" method="POST">
<center>
<table>
<tr>
<th><center>Question</center></th>
<th><center>0</center></th>
<th><center>1</center></th>
<th><center>2</center></th>
<th><center>3</center></th>
</tr>
<tr>
<td>I found it hard to wind down.</td>
<td><center><input id="stress11" name="1_stress" type="radio" value="0"/></center></td>
<td><center><input id="stress12" name="1_stress" type="radio" value="1"/></center></td>
<td><center><input id="stress13" name="1_stress" type="radio" value="2"/></center></td>
<td><center><input id="stress14" name="1_stress" type="radio" value="3"/></center></td>
</tr>
<tr>
<td>I was aware of the dryness of my mouth.</td>
<td><center><input id="anxiety11" name="2_anxiety" type="radio" value="0"/></center></td>
<td><center><input id="anxiety12" name="2_anxiety" type="radio" value="1"/></center></td>
<td><center><input id="anxiety13" name="2_anxiety" type="radio" value="2"/></center></td>
<td><center><input id="anxiety14" name="2_anxiety" type="radio" value="3"/></center></td>
</tr>
<tr>
<td>I couldn't seem to experience any positive feeling at all.</td>
<td><center><input id="depression11" name="3_depression" type="radio" value="0"/></center></td>
<td><center><input id="depression12" name="3_depression" type="radio" value="1"/></center></td>
<td><center><input id="depression13" name="3_depression" type="radio" value="2"/></center></td>
<td><center><input id="depression14" name="3_depression" type="radio" value="3"/></center></td>
</tr>
<tr>
<td>I experienced breathing difficulty(i.e excessively rapid breathing,<br>breathlessnes in the absence of physical exertion)</td>
<td><center><input id="anxiety21" name="4_anxiety" type="radio" value="0"/></center></td>
<td><center><input id="anxiety22" name="4_anxiety" type="radio" value="1"/></center></td>
<td><center><input id="anxiety23" name="4_anxiety" type="radio" value="2"/></center></td>
<td><center><input id="anxiety24" name="4_anxiety" type="radio" value="3"/></center></td>
</tr>
<tr>
<td>I found it difficult to work up on the initiative to do things</td>
<td><center><input id="depression21" name="5_depression" type="radio" value="0"/></center></td>
<td><center><input id="depression22" name="5_depression" type="radio" value="1"/></center></td>
<td><center><input id="depression23" name="5_depression" type="radio" value="2"/></center></td>
<td><center><input id="depression24" name="5_depression" type="radio" value="3"/></center></td>
</tr>
<tr>
<td>I tended to over-react to situations.</td>
<td><center><input id="stress21" name="6_stress" type="radio" value="0"/></center></td>
<td><center><input id="stress22" name="6_stress" type="radio" value="1"/></center></td>
<td><center><input id="stress23" name="6_stress" type="radio" value="2"/></center></td>
<td><center><input id="stress24" name="6_stress" type="radio" value="3"/></center></td>
</tr>
<tr>
<td>I experienced trembling (i.e. in the hands)</td>
<td><center><input id="anxiety31" name="7_anxiety" type="radio" value="0"/></center></td>
<td><center><input id="anxiety32" name="7_anxiety" type="radio" value="1"/></center></td>
<td><center><input id="anxiety33" name="7_anxiety" type="radio" value="2"/></center></td>
<td><center><input id="anxiety34" name="7_anxiety" type="radio" value="3"/></center></td>
</tr>
<tr>
<td>I felt that I was using a lot of nervous energy.</td>
<td><center><input id="stress31" name="8_stress" type="radio" value="0"/></center></td>
<td><center><input id="stress32" name="8_stress" type="radio" value="1"/></center></td>
<td><center><input id="stress33" name="8_stress" type="radio" value="2"/></center></td>
<td><center><input id="stress34" name="8_stress" type="radio" value="3"/></center></td>
</tr>
<tr>
<td>I was worried about situations in which I might panic and make a fool of myself.</td>
<td><center><input id="anxiety41" name="9_anxiety" type="radio" value="0"/></center></td>
<td><center><input id="anxiety42" name="9_anxiety" type="radio" value="1"/></center></td>
<td><center><input id="anxiety43" name="9_anxiety" type="radio" value="2"/></center></td>
<td><center><input id="anxiety44" name="9_anxiety" type="radio" value="3"/></center></td>
</tr>
<tr>
<td>I felt that I had nothing to look forward to.</td>
<td><center><input id="depression31" name="10_depression" type="radio" value="0"/></center></td>
<td><center><input id="depression32" name="10_depression" type="radio" value="1"/></center></td>
<td><center><input id="depression33" name="10_depression" type="radio" value="2"/></center></td>
<td><center><input id="depression34" name="10_depression" type="radio" value="3"/></center></td>
</tr>
<tr>
<td>I found myself getting agitated.</td>
<td><center><input id="stress41" name="11_stress" type="radio" value="0"/></center></td>
<td><center><input id="stress42" name="11_stress" type="radio" value="1"/></center></td>
<td><center><input id="stress43" name="11_stress" type="radio" value="2"/></center></td>
<td><center><input id="stress44" name="11_stress" type="radio" value="3"/></center></td>
</tr>
<tr>
<td>I found it difficult to relax.</td>
<td><center><input id="stress51" name="12_stress" type="radio" value="0"/></center></td>
<td><center><input id="stress52" name="12_stress" type="radio" value="1"/></center></td>
<td><center><input id="stress53" name="12_stress" type="radio" value="2"/></center></td>
<td><center><input id="stress54" name="12_stress" type="radio" value="3"/></center></td>
</tr>
<tr>
<td>I felt down-hearted and blue.</td>
<td><center><input id="depression41" name="13_depression" type="radio" value="0"/></center></td>
<td><center><input id="depression42" name="13_depression" type="radio" value="1"/></center></td>
<td><center><input id="depression43" name="13_depression" type="radio" value="2"/></center></td>
<td><center><input id="depression44" name="13_depression" type="radio" value="3"/></center></td>
</tr>
<tr>
<td>I was intolerant of anything that kept me from getting on with what I was doing.</td>
<td><center><input id="stress61" name="14_stress" type="radio" value="0"/></center></td>
<td><center><input id="stress62" name="14_stress" type="radio" value="1"/></center></td>
<td><center><input id="stress63" name="14_stress" type="radio" value="2"/></center></td>
<td><center><input id="stress64" name="14_stress" type="radio" value="3"/></center></td>
</tr>
<tr>
<td>I felt that I was so close to panic.</td>
<td><center><input id="anxiety51" name="15_anxiety" type="radio" value="0"/></center></td>
<td><center><input id="anxiety52" name="15_anxiety" type="radio" value="1"/></center></td>
<td><center><input id="anxiety53" name="15_anxiety" type="radio" value="2"/></center></td>
<td><center><input id="anxiety54" name="15_anxiety" type="radio" value="3"/></center></td>
</tr>
<tr>
<td>I was unable to become enthusiastic about anything.</td>
<td><center><input id="depression51" name="16_depression" type="radio" value="0"/></center></td>
<td><center><input id="depression52" name="16_depression" type="radio" value="1"/></center></td>
<td><center><input id="depression53" name="16_depression" type="radio" value="2"/></center></td>
<td><center><input id="depression54" name="16_depression" type="radio" value="3"/></center></td>
</tr>
<tr>
<td>I felt that I wasnt worth as much as a person.</td>
<td><center><input id="depression61" name="17_depression" type="radio" value="0"/></center></td>
<td><center><input id="depression62" name="17_depression" type="radio" value="1"/></center></td>
<td><center><input id="depression63" name="17_depression" type="radio" value="2"/></center></td>
<td><center><input id="depression64" name="17_depression" type="radio" value="3"/></center></td>
</tr>
<tr>
<td>I felt that I was rather touchy.</td>
<td><center><input id="stress71" name="18_stress" type="radio" value="0"/></center></td>
<td><center><input id="stress72" name="18_stress" type="radio" value="1"/></center></td>
<td><center><input id="stress73" name="18_stress" type="radio" value="2"/></center></td>
<td><center><input id="stress74" name="18_stress" type="radio" value="3"/></center></td>
</tr>
<tr>
<td>I was aware of the action of my heart in the absence of physical exertion(i.e. sense of heart rate increases)</td>
<td><center><input id="anxiety61" name="19_anxiety" type="radio" value="0"/></center></td>
<td><center><input id="anxiety62" name="19_anxiety" type="radio" value="1"/></center></td>
<td><center><input id="anxiety63" name="19_anxiety" type="radio" value="2"/></center></td>
<td><center><input id="anxiety64" name="19_anxiety" type="radio" value="3"/></center></td>
</tr>
<tr>
<td>I felt scared without any good reason.</td>
<td><center><input id="anxiety71" name="20_anxiety" type="radio" value="0"/></center></td>
<td><center><input id="anxiety72" name="20_anxiety" type="radio" value="1"/></center></td>
<td><center><input id="anxiety73" name="20_anxiety" type="radio" value="2"/></center></td>
<td><center><input id="anxiety74" name="20_anxiety" type="radio" value="3"/></center></td>
</tr>
<tr>
<td>I felt that life was meaningless.</td>
<td><center><input id="depression71" name="21_depression" type="radio" value="0"/></center></td>
<td><center><input id="depression72" name="21_depression" type="radio" value="1"/></center></td>
<td><center><input id="depression73" name="21_depression" type="radio" value="2"/></center></td>
<td><center><input id="depression74" name="21_depression" type="radio" value="3"/></center></td>
</tr>
</table><br>
<!-- dont give name to submit button -->
<button type="submit">submit</button>
</center>
</form>
PHP代码
if(isset($_POST)){
$depression = $stress = $anxiety =0;
$arr = $_POST;
foreach($arr as $key=>$val){
$ans = explode('_',$key);
if(count($ans) > 0){
$user_id = $_SESSION['u_id'];
$question_no = $ans[0];
$answer_value = $val;
if(isset($$ans[1])){
$$ans[1] += $val; // dynamic variable method
}else{
$$ans[1] = $val; // dynamic variable method
}
// answer table insert query
$sql = "INSERT INTO record (user_id, question_no, answer_value)
VALUES ('$user_id', '$question_no', '$answer_value')";
// run query
mysqli_query($conn, $sql);
}
}
// recordTbl insert query
$sql = "INSERT INTO record (user_id, depression_score, stress_score, anxiety_score)
VALUES ('$user_id', '$depression', '$stress','$anxiety')";
// run query
mysqli_query($conn, $sql);
}