dtup3446
2016-03-08 11:50
浏览 107

解析数据时格式错误的JSON响应

Using Postman to see if this section of php works as intended, however Postman returns the error Malformed JSON: Unexpected 'U' and the database is not updated, can't get my head around this error as it looks fine to me?

Code:

function placeVoteForCandidate()
{
    global $connect;

    $username = $_POST["username"];

    $query = "UPDATE User SET votesAttained=votesAttained+1 WHERE USER_NAME = $username";

    mysqli_query($connect, $query) or die (mysqli_error($connect));
    mysqli_close($connect);

    $message['success'] = 'Vote added';
    echo json_encode($message);
}

图片转代码服务由CSDN问答提供 功能建议

使用Postman查看php的这一部分是否按预期工作,但Postman返回错误格式错误的JSON: 意外的'U'和数据库没有更新,无法理解这个错误,因为它看起来不错?

代码:

  function placeVoteForCandidate()
 {
 global $ connect; 
 
 $ username = $ _POST [“username”]; 
 
 $ query =“UPDATE User SET votesAttained = votesAttained  +1 WHERE USER_NAME = $ username“; 
 
 mysqli_query($ connect,$ query)或die(mysqli_error($ connect)); 
 mysqli_close($ connect); 
 
 $ message ['success']  ='投票已添加'; 
 echo json_encode($ message); 
} 
   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douwuli4512 2016-03-08 11:57
    已采纳

    When executing just

    echo json_encode('Vote added');
    

    there should be no problem. Is there any exception thrown causing this error? Try to change the SQL statement to

    $query = "UPDATE User SET votesAttained=votesAttained+1 WHERE USER_NAME = " . $username;
    
    点赞 评论

相关推荐 更多相似问题