显示仅由特定用户在自己的仪表板上创建的帖子

时间:2018-03-12 10:15:59

标签: php mysql

Displaying posts by specific user?我看到的是铁轨上的红宝石,它无法帮助我......

我有两个表,用户和帖子。 如果用户发布任何内容,它会显示在他的仪表板上,现在可以正常工作。但我需要的是用户只能查看他的帖子。

请帮忙......

以下是我的代码:

server.php

<?php
    // connect to database
    require_once 'database.php';
    // initialize variables
        $note = "";
        $id = 0;
        $edit_state = false;


    // if save button is clicked
    if (isset($_POST['save'])) {
        $note = addslashes($_POST['note']);
        $created_at = date("Y-m-d H:i:s");

    // basic first name validation
    if (empty($note)) {
        $error = true;
        $noteError = "Field cannot be empty.";
    }else {
        // insert records if no error
        $query = "INSERT INTO posts (note, created_at, updated_at) VALUES ('$note', '$created_at', NOW())";
        mysqli_query($dbconn, $query);
        $_SESSION['msg'] = "Saved";
        header('location: ../home.php'); // redirect to home page after inserting
    }
}
?>

这是home.php,其中显示结果

<?php
    ob_start();
    session_start();
  error_reporting(E_ALL);
    require_once 'config/database.php';
  include 'config/server.php';

    // if session is not set this will redirect to login page
    if( !isset($_SESSION['user']) ) {
        header("Location: index.php");
        exit;
    }
    // select loggedin users detail
    $res=mysqli_query($dbconn, "SELECT * FROM users WHERE Id=".$_SESSION['user']);
    $userRow=mysqli_fetch_array($res);
?>
 <div class="container" style="margin-top: 100px;">
 <div class="row">
    <div class="col-sm-6">
      <div class="wrap-status100">
        <form method="post" class="login100-form validate-form" action="config/server.php" autocomplete="off">
          <span class="login100-form-title p-b-26">
          <?php if (isset($_SESSION['msg'])): ?>
          <div class="form-group">
          <div class="alert alert-<?php echo $_SESSION['msg']; unset($_SESSION['msg']); ?>">
          <span class="glyphicon glypicon-info-sign"></span>
          </div>
          </div>          
          <?php endif ?>
            What's up <?php echo $userRow['fname']; ?>?
          </span>
            <div class="wrap-input100 validate-input">
            <textarea name="note" class="input100" value="<?php echo $note; ?>"></textarea>
            <span class="focus-input100" data-placeholder="Write note here."></span>
          </div>
          <div class="container-login100-form-btn">
            <div class="wrap-login100-form-btn">
              <div class="login100-form-bgbtn"></div>
                <?php if ($edit_state == false): ?>
              <button name="save" class="login100-form-btn">
                Save
              </button>
      <?php else: ?>
              <button name="update" class="login100-form-btn">
                Update
              </button>
    <?php endif ?> 
            </div>
          </div>
          </div>
        </form>   

    </div>
    <div class="col-sm-6">
        <?php if (isset($_SESSION['msg'])): ?>
    <div class="msg">
      <?php
        echo $_SESSION['msg'];
        unset($_SESSION['msg']);
      ?>
    </div>
  <?php endif ?>
  <table>
    <thead>
      <tr>
        <th>Note</th>
        <th>created</th>
        <th>Updated</th>
        <th colspan="2">Action</th>
      </tr>
    </thead>
    <tbody>
      <?php while ($row = mysqli_fetch_array($results)) { ?>
      <tr>
        <td><?php echo $row['note']; ?></td>
        <td><?php echo $row['created_at']; ?></td>
        <td><?php echo $row['updated_at']; ?></td>
          <td><a class="edit_btn" href="home.php?update=<?php echo $row['id']; ?>">Update</a>
          </td>
        <td>
          <a class="del_btn" href="config/server.php?del=<?php echo $row['id']; ?>">Delete</a>
        </td>
      </tr>
    <?php } ?>
    </tbody>
  </table>
      </div>
  </div>
</div>

2 个答案:

答案 0 :(得分:1)

您需要在posts表中再添一列,即user_id。您必须存储创建Post的用户的ID。

在获取结果时,您可以获取登录用户的ID。并创建一个像

这样的查询
"Select * from posts where user_id=".$_SESSION['user'];

通过这个你可以获得该特定用户创建的所有帖子。

希望这有帮助。

感谢。

答案 1 :(得分:0)

您需要以这种方式执行此操作: 首先,在添加帖子时,您必须插入发布帖子的用户的用户ID。确保在db中的posts表中添加名为user_id的字段。请尝试此查询:

$user_id=trim($_SESSION['user']);
$query = "INSERT INTO posts (note,user_id, created_at, updated_at) VALUES ('$note', '".$user_id."' , '$created_at', NOW())";

现在在用户的仪表板中,您需要根据user_id获取帖子,然后在结果数组中循环tr:

"Select * from posts where user_id=".$_SESSION['user'];
相关问题