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

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;
        ...
    }
    
    打赏 评论
  • doukun8670 2015-02-19 21:14

    Have you tried using global in the function?

    If you wish for a function to have access to passive variables, you need to define them.

    fx

    myFunction() {
    global $LoggedUser, $OtherGlobalVars
    }
    
    打赏 评论
  • dourui7186 2015-02-19 21:40

    Close your if statement } before your functions are read. And for good form, I always list functions at the top of my scripts.

    打赏 评论

相关推荐 更多相似问题