SyntaxError:JSON.parse:JSON数据第2行第1列的意外数据结尾

时间:2018-04-12 06:34:05

标签: php mysql json

我正在尝试运行一个脚本,该脚本将使用“GET”接收3个变量,然后将结果返回给调用脚本的活动。我已经编写了我的PHP代码,我正在尝试使用postman测试它,但每次我给它参数时它返回= unexpected''

这是我的PHP代码:

<?php
require_once 'connection.php';
header('Content-Type: application/json');


class User{
    private $db; 
    private $connection;
    function __construct(){
    $this->db = new DB_connection();
    $this->connection = $this->db->get_connection();
    }
    public function transaction($emailval, $merchantid, $amount){
        $query= "select amount from wallet_user where email = '$emailval'";
        $result = mysqli_query($this->connection, $query);
        $row=$result->fetch_assoc();
        $resultval = floatval($row['amount']);
        //echo resultval;
        if ($resultval >= $amount){
            $sql = "Select * from wallet_merchant where merchant_id = '$merchantid'";
            $resultm = mysqli_query($this->connection, $sql);

            if (mysqli_num_rows($resultm) > 0) { 
                $query2 = "UPDATE finger.wallet_merchant SET amount = amount + $amount WHERE merchant_id= '$merchantid' ;";
                $query3 = "UPDATE finger.wallet_user SET amount = amount - $amount WHERE email= '$emailval' ;";

                $result2 = mysqli_query($this->connection, $query2);
                $result3 = mysqli_query($this->connection, $query3);

                $json['tsuccess'] = 'Transaction successful!';
                echo json_encode ($json);
                mysqli_close($this->connection);
            }
        }
        else{
            $json['amounterr'] = 'Please add more amount to your wallet!';
            echo json_encode ($json);
            mysqli_close($this ->connection);
            }
    }
}

$user = new User();

if (isset ($_GET['emailval'], $_GET ['merchant_id'], $_GET['amount'] )){
    $emailval = $_GET['emailval'];
    $merchantid = $_GET['merchant_id'];
    $amount = floatval($_GET['amount']);
    if (!empty ($emailval) && !empty($merchantid) && !empty($amount)){
        $user->transaction($emailval, $merchantid, $amount);
    } 
    else{
        echo json_encode("Please enter both fields!");
    }
}

?>

我不明白我在这里缺少什么。这不是给我任何错误的connection.php脚本,因为我正在运行其他脚本并且运行正常,我缺少什么?

1 个答案:

答案 0 :(得分:0)

问题在这里echo resultval;它将创建无效的json。从代码中删除此回显。永远不要在ant json返回之前添加输出,否则会产生问题

<?php
require_once 'connection.php';
header('Content-Type: application/json');


class User{
    private $db; 
    private $connection;
    function __construct(){
    $this->db = new DB_connection();
    $this->connection = $this->db->get_connection();
    }
    public function transaction($emailval, $merchantid, $amount){
        $query= "select amount from wallet_user where email = '$emailval'";
        $result = mysqli_query($this->connection, $query);
        $row=$result->fetch_assoc();
        $resultval = floatval($row['amount']);       
        if ($resultval >= $amount){
            $sql = "Select * from wallet_merchant where merchant_id = '$merchantid'";
            $resultm = mysqli_query($this->connection, $sql);

            if (mysqli_num_rows($resultm) > 0) { 
                $query2 = "UPDATE finger.wallet_merchant SET amount = amount + $amount WHERE merchant_id= '$merchantid' ;";
                $query3 = "UPDATE finger.wallet_user SET amount = amount - $amount WHERE email= '$emailval' ;";

                $result2 = mysqli_query($this->connection, $query2);
                $result3 = mysqli_query($this->connection, $query3);

                $json['tsuccess'] = 'Transaction successful!';
                echo json_encode ($json);
                mysqli_close($this->connection);
            }
        }
        else{
            $json['amounterr'] = 'Please add more amount to your wallet!';
            echo json_encode ($json);
            mysqli_close($this ->connection);
            }
    }
}

$user = new User();

if (isset ($_GET['emailval'], $_GET ['merchant_id'], $_GET['amount'] )){
    $emailval = $_GET['emailval'];
    $merchantid = $_GET['merchant_id'];
    $amount = floatval($_GET['amount']);
    if (!empty ($emailval) && !empty($merchantid) && !empty($amount)){
        $user->transaction($emailval, $merchantid, $amount);
    } 
    else{
        echo json_encode("Please enter both fields!");
    }
}

?>