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

标签 php mysql

我正在学习 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/

相关文章:

php - 您如何清理数据?

php - 500 内部服务器错误仅在 public_html 中?

mysql 转储查询挂起

MySQL 查询特定月份每天登录的用户

php - 如何用php隐藏表格行的可见性

php - 如何从 sql 中的 now() 中减去时间?

php - 禁止直接访问某些文件(包括)

MySQL:构建一个 View ,其中的列的数据依赖于另一个 View

php - 如果数据库已有值,但仍获取 ID,如何忽略 INSERT?

mysql - 更改mysql数据库的位置