dongzhou1865 2015-10-23 07:25
浏览 59
已采纳

PHP和DataBase

So I'm working on this page and I have an error somewhere in my code as the page is not being displayed.

The code is fine until line 27 as when I try to load the page both echo statements are executed.

The page also loads fine when I comments out the $results statement which is line 28. I just cant seem to fine whats wrong with it.

Code is posted below:

<?php
session_start();
include 'phpFunctions.php';
$error = "";

//if(!isset($_SESSION["id"]))
//{
//  header("Location: http://tylerforaie.com/csproject/login.php");
//}
if(!empty($_POST))
{
    $connect = new mysqli("localhost", "username", "password", "dbname");
    if ($connect->connect_errno) {
        printf("Connect failed: %s
", $connect->connect_error);
        exit();
    }

    $sql = "INSERT INTO requestOff (employeeId, day, approved, reason)       VALUES ('".$_SESSION['id']."', '".$_POST['date']."', 'Pending', '".$_POST['reason']."')";
    if (!$connect->query($sql)) {
        printf("Errormessage: %s
", $connect->error);
    }
    $connect->query($sql);
}       
$id = $_SESSION['id'];
echo $id;
$sql = "SELECT * FROM requestedOff where employeeId='".$id."'";
echo $sql;
$result = $connect->query($sql);/*******THIS IS LINE 28********/
if(!$result){
    echo $connect->error;
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Request Day Off</title>
</head>
<body bgcolor="">

    <div id="wrapper">
        <div class="float left">
            <?php navigation(); ?>
        </div>       
        <div class="float right">
            <h3>Request a Day Off</h3>
            <form action="dayOffRequest.php" method="post">
                <p><?php print $error; ?></p>
                <table align="center">
                    <tr>
                        <td>Date</td>
                        <td><input type="text" name="date" placeholder="YYYY/MM/DD"/></td>
                    </tr>
                    <tr>
                        <td>Reason for Request</td>
                        <td><textarea class="width" type="text" name="reason" height="50px"></textarea></td>
                    </tr>
                </table>
                <p><input type="submit" value="Submit" /></p>
            </form>
            <hr />
            <h3>Submitted Requests</h3>
            <table align="center">
                <tr>
                    <th>Date</th>
                    <th>Approved</th>
                </tr>
                <?php
                    while($row = $result->fetch_assoc())
                    {
                        print "<tr>";
                            print "<td>".$row['day']."</td>";
                            print "<td>".$row['approved']."</td>";
                        print "</tr>";
                    }
                ?>
            </table>
        </div>
    </div>
</body>
</html>
  • 写回答

2条回答 默认 最新

  • dourao3960 2015-10-23 18:34
    关注

    So as it turns out I had put the connect function inside the if statement so when the form wasnt being submitted there was no connection to the database. I fixed it by moving the connection function outside of the if

    <?php
        session_start();
        include 'phpFunctions.php';
        $error = "";
    
        //if(!isset($_SESSION["id"]))
        //{
        //  header("Location: http://tylerforaie.com/csproject/login.php");
        //}
        $connect = new mysqli("localhost", "username", "password", "db");
            if ($connect->connect_errno) {
                printf("Connect failed: %s
    ", $connect->connect_error);
                exit();
            }
        if(!empty($_POST))
        {
    
    
            $sql = "INSERT INTO requestOff (employeeId, day, approved, reason) VALUES ('".$_SESSION['id']."', '".$_POST['date']."', 'Pending', '".$_POST['reason']."')";
            if (!$connect->query($sql)) {
                printf("Errormessage: %s
    ", $connect->error);
            }
        }       
        $result = $connect->query("SELECT * FROM requestedOff where employeeId='1'");
    
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启