gpt4 book ai didi

php - ShoutBox 不显示消息和用户名

转载 作者:行者123 更新时间:2023-11-29 11:45:53 26 4
gpt4 key购买 nike

我正在学习 PHP,现在我正在尝试制作简单的 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 数据库工作正常。

最佳答案

第 14 行应为...

if(!isset($user) || $user == '' || !isset($message) || $message == '') {

记住==那么语句就变成“if”user isempty/null“或者”message isempty/nullpost message“Please fill in your name and a message”

关于php - ShoutBox 不显示消息和用户名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34960932/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com