【问题标题】:how can i insert data to database using json php mysqli?如何使用 json php mysqli 将数据插入数据库?
【发布时间】:2014-03-28 16:28:52
【问题描述】:

我的问题是我得到的响应都是空的..
在我的订阅者文件中,我有一个类,其中包含一个函数和一个选择所有用户的查询。

在我的 cmets 文件中,我有一个类,其中包含一个函数和一个插入注释的查询

  if(isset($_POST['task']) && $_POST['task']=='comment_insert'){    
    $userId = (int)$_POST ['userId'];
    $comment = addslashes($_POST ['comment']);

    $std = new stdClass();
    $std->user= null;
    $std->userId= null;
    $std->comment= null;
    $std->error=false ; 

    require_once( "subscribers.php" );
    require_once( "comments.php" );
    require_once( "db_conx.php" );
    if ( class_exists( 'Comments ' ) && class_exists( 'Subscribers'))

    {   
        $userInfo = Subscribers::getSubscriber($userId);

        if($userId == null)
        {
            $std-> error = true ;
        }


        $commentInfo = Comments::insert($comment, $userId);

        if($commentsInfo != null )
        {
            $std->error = true ;
        }


        $std->user = $userInfo;
        $std->comment = $commentInfo;



    }   
        echo json_encode($std); 



}

else

{
    die;
}

?>

【问题讨论】:

  • 你有一个错字 $std-> error = true ;并不是说它与您的问题有任何关系,只是提醒一下。

标签: php json mysqli


【解决方案1】:

只需将您的对象转换为数组

$array =  (array) $std;
echo json_encode($array); 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-27
    • 2013-08-24
    • 1970-01-01
    • 2016-05-15
    • 2017-05-18
    • 2016-07-17
    相关资源
    最近更新 更多