0
我正在學習PHP,現在我試圖做出簡單的ShoutBox。 爲什麼ShoutBox不顯示任何用戶名,消息和錯誤消息?代碼中的錯誤在哪裏?ShoutBox不顯示消息和用戶名
我的index.php:
<!-- INCLUDES -->
<?php include 'database.php'; ?>
<!-- Create select query -->
<?php
$query = "SELECT * FROM shouts";
$shouts = mysqli_query($con, $query);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>SHOUTBOX</title>
<link rel="stylesheet" href="css/style.css" type="text/css" />
</head>
<body>
<!-- MAIN CONTAINER -->
<div id="container">
<header>
<h1>SHOUT IT! Shoutbox</h1>
</header>
<!-- SHOUTS -->
<div id="shouts">
<ul>
<?php while($row = mysqli_fetch_assoc($shouts)) : ?>
<li class="shout"><span><?php echo $row['time'] ?> - </span><strong><?php echo $row['user'] ?></strong> : <?php echo $row['message'] ?></li>
<?php endwhile; ?>
</ul>
</div>
<!-- INPUT -->
<div id="input">
<?php if(isset($_GET['error'])) : ?>
<div class="error"> <?php echo $_GET['error']; ?> </div>
<?php endif; ?>
<form method="post" action="process.php">
<input type="text" name="user" placeholder="Your name" />
<input type="text" name="message" placeholder="Your message" />
</br >
<input class="shout-btn" type="submit" name="submit" value="Shout it out" />
</form>
</div>
</div>
</body>
</html>
我process.php:
<?php
include 'database.php';
//Check if form submitted
if(isset($_POST['submit'])) {
$user = mysqli_real_escape_string($con, $_POST['user']);
$message = mysqli_real_escape_string($con, $_POST['message']);
//Set timezone
date_default_timezone_set('America/New_York');
$time = date('h:i:s a',time());
//Validate input
if(!isset($user) || $user = '' || !isset($message) || $message = '') {
$error = "Please fill in your name and a message";
header("Location: index.php?error=".urlencode($error));
exit();
} else {
$query = "INSERT INTO shouts (user, message, time)
VALUES ('$user','$message','$time')";
if(!mysqli_query($con, $query)) {
die('Error: '.mysqli_error($con));
} else {
header("Location: index.php");
exit();
}
}
}
MySQL數據庫正在corectly。