通过post方法将字符串传递给php时出错

时间:2015-06-01 17:43:54

标签: php arrays string mysqli

我正在尝试以

形式传递字符串参数param
   number,number,number.... for example 1,3,5,4,8,2

并将其拆分为数字数组,然后从每个数字上的Check_Bets.php文件中执行checkuserbets函数,并将结果作为数组返回。但是,当我在REST客户端中运行它时,我得到了

{"error":true,"error_msg":"the response is null!"} 

好像我没有传递正确的参数或者没有使用POST方法。这就是我要传递的内容: enter image description here

Check_Bets_Handler.php

<?php
if (isset($_POST['param'])) {
    // get tag
   $param= $_POST['param'];
   $id = explode(",",$param);
   $arrlength = count($id);
    // include db handler
     require_once 'include/Check_Bets.php';


    $db = new Check_Bets();
    $response["bet"] = array();


   for($x = 0; $x < $arrlength; $x++) {

    $result= $db->checkuserbets($id[$x]);
    array_push($response["bet"], $result);
}
    echo json_encode($response);
}
else {
$response["error"] = TRUE;
    $response["error_msg"] = "the response is null!";
    echo json_encode($response);
}
?>
 

Check_Bets.php

<?php

class Check_Bets {
  
 

   function __construct() {

	require_once 'DB_Connect.php';
	$this->db = new DB_Connect();

	$this->db->connect();


}



function __destruct() {
   
  }

  public function checkuserbets($id) {

   $conn=mysqli_connect("****", "******", "****","****");

   $result = mysqli_query($conn,"SELECT Result FROM gamelist WHERE gid = '$id'");
 $no_of_rows = mysqli_num_rows($result);
     
    if ($no_of_rows > 0) {

 return mysqli_fetch_array($result,MYSQLI_ASSOC);
    
}
}
}
?>

enter image description here

2 个答案:

答案 0 :(得分:1)

好的,我刚刚运行了一个测试脚本,它似乎按预期工作。您遇到的问题似乎是if (isset($_POST['param'])) {没有捕获。但是,使用您截断的代码,这对我来说没有意义。

我没有为此设置数据库,所以我必须创建自己的函数,但这是我运行的代码,它对我来说很好。

<?php

// SET SOME INFORMATION
$_POST = array("param" => '3,4,5,6,7,8,9');

if (isset($_POST['param'])) {

    $param = $_POST['param'];
    print '<br>PARAM: '.$param;

    $id = explode(",",$param);
    print "<pre><font color=blue>"; print_r($id); print "</font></pre>";

    $arrlength = count($id);
    // include db handler
    //require_once 'include/Check_Bets.php'; // DON'T HAVE THIS


    //$db = new Check_Bets();
    $response["bet"] = array();


    for($x = 0; $x < $arrlength; $x++) {

        //$result= $db->checkuserbets($id[$x]);
        $result= checkuserbets($id[$x]); // MADE MY OWN FUNCTION BECAUSE I DO NOT HAVE A DATABASE
        array_push($response["bet"], $result);
    }

    echo json_encode($response);
}
else {
    $response["error"] = TRUE;
    $response["error_msg"] = "the response is null!";
    echo json_encode($response);
}

// DUMMY FUNCTION
function checkuserbets($id) {
    return $id * 5;    
}

这就是它的回报:

PARAM: 3,4,5,6,7,8,9
Array
(
    [0] => 3
    [1] => 4
    [2] => 5
    [3] => 6
    [4] => 7
    [5] => 8
    [6] => 9
)
{"bet":[15,20,25,30,35,40,45]}

因此,如果您在$_POST['params']print_r取回isset,而不是empty,那么我会提出更多建议。您可以尝试使用// CHECK TO MAKE SURE $_POST['param'] IS NOT EMPTY if (!empty($_POST['param'])) { 或不等于任何内容。

// CHECK TO MAKE SURE $_POST['param'] IS NOT EQUAL TO NOTHING
if ($_POST['param'] != '') {

OR

onCreate()

希望其中一个会为你解决。

Here is a working demo

答案 1 :(得分:0)

检查值可以在url中传递。如果值无法在url中传递你在发布值时犯了错误