PHP:Variable在Function中不显示值,而直接显示


PHP : Variable does not show value in Function while shows directely

我的代码有问题。我发布了一些addsms.php文件的字段数据。

  <form id="signup" Name="signup" method="post" action="addsms.php?act=add"> <h1>SMS Detail Here!</h1>
    User Refrence Code:<input name="rfcd" type ="text" class="a" id="rfcd" placeholder="Enter Refrence Code" /><br>
    Total SMS Count: &nbsp &nbsp <input name="smsc" type ="text" class="a" id="smsc" placeholder="Enter SMS Count" /><br>
            Total SMS Amount: <input name="amnt" type ="text" class="a" id="amnt" placeholder="Enter SMS Amount" /><br>
<input type ="submit" Name="submit"  value="Submit" class="outset" />
</form>

addsms.php

   <?php
 require_once("./include/connect.php");
 require_once("./include/fg_membersite.php");

 if($_GET['act'] == 'add')
{
$rf_cd = $_POST['rfcd'];
$sms_c = $_POST['smsc'];
$am_nt = $_POST['amnt'];

  // echo $rf_cd;
  // exit;
    //I can Get value of variable  "$rf_cd"  at above line. while not able to get value in any function,


if ($rf_cd == NULL || $sms_c == NULL || $am_nt == NULL  )
{
    header("Location:sms.php?field=miss");
}


    else
        if(checkref_id_from_user() == true && checkref_id_from_user_data() == true )
            {
                  $get_detail = "select total_sms, total_sms_amount from user_data where ref_id = '$rf_cd'";
                  $res1 = Run($get_detail);
                    if(mysql_num_rows($res1) > 0){
                    while ($rows = mysql_fetch_object($res1)) {
                    $srn++;
                    $sms_oldcont = $rows->total_sms;
                    $sms_amnt_oldcont = $rows->total_sms_amount;
                    }
                    }
                    $inqry = "UPDATE user_data SET total_sms = '$sms_c' + '$sms_oldcont', total_sms_amount ='$am_nt'+ '$sms_amnt_oldcont' where ref_id = '$rf_cd' ";
                    $resinqry = Run($inqry);
                    if(!$resinqry)
                    {
                        echo $resinqry;
                    }
                    else
                    {
                        header("Location:sms.php?add=done");
                    }

            }
        else 
        if(checkref_id_from_user() == true && checkref_id_from_user_data() == false)
            {
                $inqry = "Insert into user_data (ref_id,total_sms,total_sms_amount) VALUES('$rf_cd','$sms_c','$am_nt')";
                $resinqry = Run($inqry);
                if(!$resinqry)
                {
        echo $resinqry;
                }
                else
                {
        header("Location:sms.php?add=done");
                }
            }
         // if($result && mysql_num_rows($result) > 0 && )
        else
        {
            header("Location:sms.php?refid=notavailable");
        }
    }
    else 
    {
        echo 'oh';
        //header("Location:sms.php?refid=notavailable");
    }


  function checkref_id_from_user(){
        $qry = "select ref_id from user where ref_id='".$rf_cd."'";
        echo $qry;
        $result = Run($qry);
        echo $result;
        exit;
        if($result && mysql_num_rows($result) > 0)
                {
                    return true;
                }
                return false; 
    }
    function checkref_id_from_user_data()
    {
        $checkrefid = "select ref_id from user_data where ref_id = '$rf_cd'";
        $res = Run($checkrefid);
        if(mysql_num_rows($res) > 0)
        {
        return true;
        }
        return false;
    }


?>

我可以在if条件中的任意位置获取变量"$rf_cd"的值。虽然不能在任何函数中获取值,"$rf_cd"我想在函数中使用这个值来从数据库中获取信息。

 $checkrefid = "select ref_id from user_data where ref_id = '$rf_cd'";
 echo  $checkrefid;
 exit

上面的查询显示了"从user_data中选择ref_id,其中ref_id=''"的结果。而这应该显示变量$rf_cd的值。

在读取函数之前关闭if语句}。为了获得良好的形式,我总是在脚本的顶部列出函数。

您只需声明$rf_cd为这些函数的参数,并在调用它们时传递它(最佳解决方案)。

此外,您还可以使用"global"关键字从全局范围访问$rf_cd变量。

function checkref_id_from_user(){
    global $rf_cd;
    ...
}
 function checkref_id_from_user_data() {
    global $rf_cd;
    ...
}

您尝试过在函数中使用global吗?

如果希望函数能够访问被动变量,则需要定义它们。

fx

myFunction() {
global $LoggedUser, $OtherGlobalVars
}