2016-01-23 47 views
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。

回答

1

线14应该读...

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

记得==那么语句变成了“如果”用户是空/空“或”消息是空/空帖子消息“请填写您的姓名和一条消息“