0

当我将值传递给在 PHP 中发布到文档中的函数时出现问题

这是函数定义,

function findstation($val){
   global $positions_table;
   var_dump($val);
   $id=$val;
   $query = array('uid'=> $id );
   $result = $positions_table->find($query);
   foreach ($result as $station){
      return $station['name'];
   }
}

此函数查询 mongoDB 数据库以从我有页面的数据库中查找并返回键名,该页面将id作为$_POST['id'].

这就是我在其中搜索的方式

if(isset($_POST['submit'])){
    echo "START!";
    $st = $_POST['station'];
    var_dump($t);
    findstation($st);
    echo "EXECUTED THE FUNCTION"; // Just added this to diagnose
}

当我这样搜索时,我没有得到任何结果,它只是显示START!EXECUTED THE FUNCTION

但是当我这样调用函数时

findstation(2);

我得到一个结果。当我将它作为变量传递时,我怎样才能让它工作?

PS:$_POST['id']作为<select>输入值传递。

4

1 回答 1

4
if(isset($_POST['submit'])){
    echo "START!'; // < == there you use a double to start string and a single quote to end
    $st = $_POST['station'];
    $t = (int)$st;
    var_dump($t);
    findstation($st);
    echo "EXECUTED THE FUNCTION"; // Just added this to diagnose
}

正确的:

if(isset($_POST['submit'])){
    echo "START!"; // corrected string;
    $st = $_POST['station'];
    $t = (int)$st;
    var_dump($t);
    findstation($st);
    echo "EXECUTED THE FUNCTION"; // Just added this to diagnose
}
于 2012-10-03T20:10:13.427 回答