簡體   English   中英

如何使用json php mysqli將數據插入數據庫?

[英]how can i insert data to database using json php mysqli?

我的問題是我得到的響應都為空。
在我的訂閱者文件中,我有一個帶有函數的類和一個選擇所有用戶的查詢。

在我的評論文件中,我有一個帶有函數的類和一個插入評論的查詢

  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;
}

?>

只是將您的對象轉換為數組

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM