我正在尝试运行一个脚本,该脚本将使用“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条答案
按热度按时间a1o7rhls1#
问题就在这里
echo resultval;
它将创建无效的json。从代码中删除此回音。在antjson返回之前不要添加输出,否则会产生问题