首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >PHP和DataBase

PHP和DataBase
EN

Stack Overflow用户
提问于 2015-10-23 07:25:28
回答 2查看 75关注 0票数 1

所以我正在处理这个页面,我的代码中有一个错误,因为页面没有显示。

代码很好,直到第27行,就像我尝试加载页面时一样,执行两个echo语句。

当我注释掉$results语句(第28行)时,页面也会很好地加载。我只是看不出它有什么问题。

代码张贴如下:

代码语言:javascript
复制
<?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\n", $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\n", $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>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-10-23 18:34:26

因此,我将connect函数放入if语句中,因此当表单未提交时,就没有到数据库的连接。我通过将连接函数移出if来修复它。

代码语言:javascript
复制
<?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\n", $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\n", $connect->error);
        }
    }       
    $result = $connect->query("SELECT * FROM requestedOff where employeeId='1'");


?>
票数 0
EN

Stack Overflow用户

发布于 2015-10-23 07:55:14

试试这个-

代码语言:javascript
复制
<?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\n", $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\n", $connect->error);
    }
    $connect->query($sql);
}       
$id = $_SESSION['id'];
echo $id;
$sql = "SELECT * FROM requestOff 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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33296923

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档