使用$ _SESSION和PDO从MySQL获取ID错误:数组到字符串转换

时间:2016-11-22 22:35:37

标签: php mysql session pdo

所以我从头开始创建一个博客,并且无法解决这个问题。基本上,一旦用户注册,他们就可以使用post_action.php从dashboard.php发帖。

这是post_action.php的代码:

<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL); 

session_start();
require 'connection_post.php';
require 'connection.php';

    $user = $_SESSION['user'];

    $title = $_POST['title'];
    $body = $_POST['body'];

    $author = $con->prepare("SELECT id FROM userdata WHERE user=:user");
    $author->bindParam(':user',$user);
    $author->execute();
    $authorResult = $author->fetch(PDO::FETCH_ASSOC);

    $stmt = $conn->prepare("INSERT INTO posts (title, body, author, blog_time) VALUES (:title, :body, :author, now())");
    $stmt->bindParam(':title',$title);
    $stmt->bindParam(':body',$body);
    $stmt->bindParam(':author',$user);
    $stmt->execute();

    echo 'hi';

?>

问题是我收到错误:

  

注意:第23行/var/www/html/blog/post_action.php中的数组到字符串转换

我尝试将原始字符串输入到有效的数据库中,但使用用户的用户名而不是ID会限制更改用户姓名的能力。

有什么想法吗?谢谢!

0 个答案:

没有答案