PHP:变量在函数中不显示直接显示的值

时间:2015-02-19 21:06:21

标签: php mysql post

我的代码中存在问题。我发布了一些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;

    }




?>

我可以获得变量的价值&#34; $ rf_cd&#34;在if条件下的任何地方。虽然无法在任何函数中获得价值,&#34; $ rf_cd&#34;我想在我的函数中使用这个值来从数据库中获取信息。

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

以上查询显示的结果是&#34;从user_data中选择ref_id,其中ref_id =&#39;&#39; &#34 ;. 虽然这应该显示变量$ rf_cd的值。

3 个答案:

答案 0 :(得分:-1)

在阅读函数之前关闭if语句}。好的形式,我总是在我的脚本顶部列出函数。

答案 1 :(得分:-2)

您是否尝试在函数中使用global?

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

FX

myFunction() {
global $LoggedUser, $OtherGlobalVars
}

答案 2 :(得分:-2)

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

你也可以使用&#34; global&#34;用于从全局范围访问$ rf_cd变量的关键字。

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

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