所以我正在处理这个页面,我的代码中有一个错误,因为页面没有显示。
代码很好,直到第27行,就像我尝试加载页面时一样,执行两个echo语句。
当我注释掉$results语句(第28行)时,页面也会很好地加载。我只是看不出它有什么问题。
代码张贴如下:
<?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>发布于 2015-10-23 18:34:26
因此,我将connect函数放入if语句中,因此当表单未提交时,就没有到数据库的连接。我通过将连接函数移出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\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'");
?>发布于 2015-10-23 07:55:14
试试这个-
<?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>https://stackoverflow.com/questions/33296923
复制相似问题