为了减少上传网站后修改数据库的工作量,特建立统一的数据库配置文件如下,命名为 dataBase.php
这是统一的数据库配置文件
<?php
// 创建连接
$servername = "localhost:3306";
$username = "--";
$password = "--";
$dbname = "--";
// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}else{echo '连接成功!<br>';}
?>
以下是调用这个文件的一个页面:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>中医九种体质问卷</title>
</head>
<?php
if($_COOKIE['userRight']!= 'normalRight'){
header('location:loginPage.php');
exit();
}
echo '这是您第'.$_COOKIE['testTime'].'次测试<br>';
//build the connection
include_once('dataBase.php');
//then we can go on
$currentQuestionNumber=$_COOKIE['questionNumber'];
if($currentQuestionNumber != Null||$currentQuestionNumber >=1){
$i=$currentQuestionNumber;
}else{
$i=1;
}
/*这段代码感觉不是很合适,因为只是单向的考虑到了前进,没有考虑回退。所以要从后面的js代码上面调控更加合适
//we add 1 to jump to the next question then the male or female can be away the unfit question
if($i==37 and $_COOKIE['userSex']=='male'){
$i=$i+1;
}
if($i==38 and $_COOKIE['userSex']=='female'){
$i=$i+1;
}
*/
setcookie('questionNumber',$i);//record how many questions we have finished for js function
$sql = "SELECT * FROM the9kinds WHERE id=$i";//php的双引号里面可以放变量
$result = $conn->query($sql);
?>
<body>
<form action="storeData.php" method="get" onsubmit="return questionForward()">
<?php
$testUser=$_COOKIE['userName'];//read userName from cookies
if ($result->num_rows > 0) {
// 输出数据
while($row = $result->fetch_assoc()) {
echo $row["question"]. "?<br>";
?>
<input type='radio' name='answer' value='<?php echo $row["nothing"]?>'> 从来没有 <?php //echo $row["nothing"]?><br>
<input type='radio' name='answer' value='<?php echo $row["alittle"] ?>'> 很少的时候 <?php //echo $row["alittle"]?><br>
<input type='radio' name='answer' value='<?php echo $row["sometimes"] ?>'>有些时候<?php //echo $row["sometimes"]?> <br>
<input type='radio' name='answer' value='<?php echo $row["often"] ?>'> 经常这样 <?php //echo $row["often"]?><br>
<input type='radio' name='answer' value='<?php echo $row["always"] ?>'>总是这样 <?php //echo $row["always"]?><br>
<?php
}
} else {
echo "0 结果";
}
?>
<input type="hidden" name="userSex" value="<?php echo $_COOKIE['userSex'] ?>">
<input type="hidden" name="testTime" value="<?php echo $_COOKIE['testTime']?>">
<input type="hidden" name="testUser" value='<?php echo $_COOKIE['userName'] ?>'>
<input type="hidden" name="questionOrder" value='<?php echo $i ?>'>
<input type='button' value="上一题" onclick="questionBack()" <?php if($i==1){?>style="VISIBILITY: hidden"<?php } ?>><!--这里做了设定,避免呢超出范围-->
<input type="submit" value="提 交" <?php if($i>67){?>style="VISIBILITY: hidden"<?php } ?>><!--这里做了设定,避免超出范围-->
</form>
<script>
function questionBack(){
var x=document.getElementsByName("questionOrder")[0];
var i=x.value;
var y=document.getElementsByName('userSex')[0];
var userSex=y.value;
//alert(i);
if(i==38 && userSex=='male'){
i=Number(i)-1;
}
if(i==39 && userSex=='female'){
i=Number(i)-1;
}
var reduceQuestionNumber=Number(i)-1;
//alert(reduceQuestionNumber);
document.cookie='questionNumber='+String(reduceQuestionNumber);//这里不使用string()也可以,但主要是提醒自己string的转换函数
window.location.reload();//只有submit按钮有自动刷新的功能,其他的按钮没有这个功能,所以要加上这个刷新语句
}
function questionForward(){
var theRadioInputs = document.getElementsByName('answer');
var radioIsChecked = 0;
//heck radio is checked
for(let m=0;m<theRadioInputs.length;m++)
{
if(theRadioInputs[m].checked){
radioIsChecked = 1;
}
}
if(radioIsChecked==1){
var x=document.getElementsByName("questionOrder")[0];
var i=x.value;
var y=document.getElementsByName('userSex')[0];
var userSex=y.value;
//alert(i);
if(i==36 && userSex=='male'){
i=Number(i)+1;
}
if(i==37 && userSex=='female'){
i=Number(i)+1;
}
var addQuestionNumber=Number(i)+1;
//alert(addQuestionNumber);
document.cookie='questionNumber='+String(addQuestionNumber);//这里不使用string()也可以,但主要是提醒自己string的转换函数
//check radio which shoulb be checked before we submit
return true;
}else{
alert('您至少要选择一个');
return false;
}
}
</script>
<?php
$conn->close();
?>
</body>
</html>
结果错了,不能像之前那样工作了。请问,对include的使用有什么要求么?phpstorm显示的是变量 conn 是 没有被定义。