kangjacob 2021-08-09 16:42 采纳率: 96.8%
浏览 38
已结题

使用include的方式,用统一的数据库配置文件建立连接,但是错误

为了减少上传网站后修改数据库的工作量,特建立统一的数据库配置文件如下,命名为 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 是 没有被定义。

  • 写回答

2条回答 默认 最新

  • BigKay 2021-08-09 16:45
    关注

    试试require?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 8月17日
  • 已采纳回答 8月9日
  • 创建了问题 8月9日

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值