如何从$ _SESSION获取作者的id和名称到数据库?

时间:2017-10-16 20:54:52

标签: php mysql session

我的网站上有问题。我有文章编辑器,我想知道创建这篇文章的用户的每个文章名称和ID。我从$_SESSION提供的这些数据。在文件EditorController.php中我有这段代码:

$managerUsers = new ManagerUsers();
$user = $managerUsers->returnUser();
$this->data['author'] = $user['name'];
$this->data['author_id'] = $user['users_id'];
$review = array(
          'reviews_id' => '',
          'author_id' => '',
          'author' => '',
if ($_POST)
            {
             $keys = array($author_id, $author, 'title', 'content', 'url', 
'description', 'keywords');
             $review = array_intersect_key($_POST, array_flip($keys));

并在视图editor.phtml中我有这个:

<input type="hidden" name="author_id" value="<?= $review['author_id'] ?>" />
<input type="hidden" name="author" value="<?= $review['author'] ?>" />

但是当我将文章保存到数据库时,这两个参数不会被保存。我对此提出了看法:

<?= $author_id, $author ?>并且一切正常,视图是写入ID和现在登录用户的名称。所以我认为,那里:

$managerUsers = new ManagerUsers();
$user = $managerUsers->returnUser();
$this->data['author'] = $user['name'];
$this->data['author_id'] = $user['users_id'];

没有任何问题。请给我一些建议,问题是什么以及如何解决。对于所有建议,请提前感谢您。

1 个答案:

答案 0 :(得分:1)

1)quick'n'dirty

如果您使用隐藏字段在表单中传递authorauthor_id,则必须使用这些字段。

-$keys = array($author_id, $author, 'title', 'content', 'url', 'description', 'keywords');
+$keys = array('author_id', 'author', 'title', 'content', 'url', 'description', 'keywords');

2)更好的方式

将这两个密钥保留并直接添加

$keys = array('title', 'content', 'url', 'description', 'keywords');
$review = array_intersect_key($_POST, array_flip($keys));
$review['author'] = $user['name'];
$review['author_id'] = $user['users_id'];

3)也许是最好的方式

与2相同,但在之前添加“模板”$review并合并表单值。

$managerUsers = new ManagerUsers();
$user = $managerUsers->returnUser();
$this->data['author'] = $user['name'];
$this->data['author_id'] = $user['users_id'];
$review = array(
    'reviews_id' => '',
    'author_id' => $user['users_id'],
    'author' => $user['name'],
);
if ($_POST) {
    $keys = array('title', 'content', 'url', 'description', 'keywords');
    $review = array_merge(
        $review,
        array_intersect_key($_POST, array_flip($keys)
    );
    // do something with $review
}