dsegw3424 2014-10-09 03:17
浏览 46

php使用变量作为php函数中的实际参数

Below is my code. In the first half of my code you will see the variables $question and $row. When I echo $question and/or $row[0] I get values returned. However, at the bottom of the code I try to use these to variables as actual arguments in the add_textbox function. However, the add_textbox function is acting like those variables are null. Can someone tell me what I am doing wrong.

<?php


$dbhost = 'localhost';

$uname = $_POST["uname"];

//***create connection object
$connection = mysql_connect($dbhost, "bc572482", "dfdfdfs")
    or die (mysql_error());

$dbname = "bc57";
mysql_select_db($dbname)
    or die (mysql_error());

//***select a random security question

//*** need this to import session variables
 session_start();

// echo ($_SESSION["ValidUser"] . '<br>');
 $uname =$_SESSION["ValidUser"];
  //echo ($uname . '<br>');

$rq = array('q1','q2','q3');
  $rand_key = array_rand($rq,1);
 // echo $rq[$rand_key];

 $question = $rq[$rand_key];

 $qtoanswer = mysql_query("select $question from users where uname = '$uname'");
 if(!$qtoanswer)
 {
    echo "Could not run query:"  . mysql_error();
    exit;
  }

 //echo ($qtoanswer . '<br>');
 $row = mysql_fetch_row($qtoanswer);

 //echo ($row[0] . abc);

 display_security();


 //functions to create security page
 function myprint($text)
 {
    print("$text 
");
 }

 //*** a bit of style
function add_style() 
{
  myprint("<link rel=\"stylesheet\" type=\"text/css\" href= \"main.css\" />");
 //myprint("<style type=text/css> * {font-family:arial;}   h3 {color:orange} </style>");
 }

//*** add a text box
function add_textbox($label, $name) 
{
   myprint("$label <br><input type=text name=$name><br>");
 }

   function add_submit($label) 
{
   myprint("<input type=submit name=submit value=$label>");
   myprint("<input type=reset value=Reset><br>");

}

//create the dynamic security page
function display_security()
{
    myprint("<html><head>");
    add_style();
    myprint("</head><body>");
    myprint("<h3> Feeding our Brothers and Sisters <br> Security Check</h3>"); 
    // get the security form created
    myprint("<form name=securityForm method=post action=login.php>");

     add_textbox($row[0],$question);
     add_submit("Login");


 myprint("</form>");

    myprint("</body></html>");

}

?>

  • 写回答

2条回答 默认 最新

  • dqcd84732 2014-10-09 03:22
    关注

    Pass $row[0] and $question as function parameter when call the display_security() function like this

    display_security($row[0], $question);
    

    In display_security() use the parameter variables $p & $question

    function display_security($p, $question)
    {
        myprint("<html><head>");
        add_style();
        myprint("</head><body>");
        myprint("<h3> Feeding our Brothers and Sisters <br> Security Check</h3>");
        // get the security form created
        myprint("<form name=securityForm method=post action=login.php>");
        add_textbox($p,$question);
                    ^^^ ^^^^^^^^
        add_submit("Login");
        myprint("</form>");
        myprint("</body></html>");
    
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了