douqing0713 2015-02-19 21:06
浏览 100
已采纳

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

I have a problem in my code. I post some field data for addsms.php file.

  <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;

    }




?>

I can Get value of variable "$rf_cd" at any where in if condition. while not able to get value in any function, "$rf_cd" I wanna use this value in my functions to get info from database.

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

Above query shows the result that "Select ref_id from user_data where ref_id = '' ". while this should show the value of variable $rf_cd.

  • 写回答

3条回答 默认 最新

  • dongliang1941 2015-02-19 21:15
    关注

    You can just declare $rf_cd as argument of those functions and pass it when calling them (best solution).

    Also you can use "global" keyword to get an access to a $rf_cd variable from global scope.

    function checkref_id_from_user(){
        global $rf_cd;
        ...
    }
    
     function checkref_id_from_user_data() {
        global $rf_cd;
        ...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程