php 如何将字符串/变量传递给函数?

发布于 2024-11-06 00:24:58 字数 676 浏览 0 评论 0原文

我有一个名为 function.php 的文件,其中包含如下函数:

public function issue_challenge () 
{
    $conn= fns_connect(13);
    mysql_select_db("et_games",$conn);
    $sql = "insert into challengers (challenger,defender,stake,matching_game_id) values (".$this->talentnum_self.",".$this->talentnum_other.",".$this->stake.",'".$this->matching_str()."')";
    $result = mysql_query($sql);
    echo $sql; mysql_error();

    if($result){
        echo "ok.\n";return true;                
    }else{
        return false;                    
    }
}

另一个名为 test.php 的文件,其中包含一个表单。

如何将值从该表单传递到该函数中?

我正在寻找有关如何将字符串/变量传递给函数的一些通用答案。

谢谢

I have a file called function.php that has a function like this:

public function issue_challenge () 
{
    $conn= fns_connect(13);
    mysql_select_db("et_games",$conn);
    $sql = "insert into challengers (challenger,defender,stake,matching_game_id) values (".$this->talentnum_self.",".$this->talentnum_other.",".$this->stake.",'".$this->matching_str()."')";
    $result = mysql_query($sql);
    echo $sql; mysql_error();

    if($result){
        echo "ok.\n";return true;                
    }else{
        return false;                    
    }
}

and another file called test.php that has a form in it.

How do I pass the values from that form into that function?

I am looking for some generic answer on how to pass a string/variable to a function.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

静若繁花 2024-11-13 00:24:58

以这种方式调用您的函数:

 if(isset($_GET['form_name'])) {
     $val = $_GET['form_name'];
     public function issue_challenge($val);
}

另外:将声明更改为:

public function issue_challenge($formValue) {

...function contents...

}

Call your function this way:

 if(isset($_GET['form_name'])) {
     $val = $_GET['form_name'];
     public function issue_challenge($val);
}

Also: Change the declaration to:

public function issue_challenge($formValue) {

...function contents...

}
话少情深 2024-11-13 00:24:58

增加值的通用答案:

函数 inc($x) {

 echo "x = " . $x;
 $x++;
 返回$x; 

}

函数 main() {

<前><代码>$x = '1';
$y = $this->inc($x);
回声 $y;

}

Generic answer that increments a value:

function inc($x) {

 echo "x = " . $x;
 $x++;
 return $x; 

}

function main() {

$x = '1';
$y = $this->inc($x);
echo $y; 

}

独﹏钓一江月 2024-11-13 00:24:58

确保表单提交调用该函数,然后从 POST 数组中获取值。如果使用 php 框架,这会更容易,许多框架会为您提供清理输入的方法。在这种情况下,由于您可能使用数据库查询的输入,因此您需要确保它被正确转义以避免 sql 注入。

mysql_real_escape_string 的 PHP 手册

<form action="issue_challenge" method="POST">
<input type="text" name="testInput" id="testInput"/>
<input type="submit" name="submit" value="submit"/>
</form>

public function issue_challenge () {
    $conn= fns_connect(13);
    $inputText = $_POST['testInput']
mysql_select_db("et_games",$conn);
    $sql = "insert into challengers (challenger,defender,stake,matching_game_id) values (".$this->talentnum_self.",".$this->talentnum_other.",".$this->stake.",'".$this->matching_str()."')";
    $result = mysql_query($sql);
    echo $sql; mysql_error();
        if($result){
            echo "ok.\n";return true;                
            }else{
                return false;                    
            }
}

Make sure the form submission calls the function, then grab the value from the POST array. This is easier if using a php framework, many will offer you methods to clean the input. In this case since you are possibly using the input for a db query you will need to make sure it is properly escaped to avoid sql injection.

PHP manaul for mysql_real_escape_string

<form action="issue_challenge" method="POST">
<input type="text" name="testInput" id="testInput"/>
<input type="submit" name="submit" value="submit"/>
</form>

public function issue_challenge () {
    $conn= fns_connect(13);
    $inputText = $_POST['testInput']
mysql_select_db("et_games",$conn);
    $sql = "insert into challengers (challenger,defender,stake,matching_game_id) values (".$this->talentnum_self.",".$this->talentnum_other.",".$this->stake.",'".$this->matching_str()."')";
    $result = mysql_query($sql);
    echo $sql; mysql_error();
        if($result){
            echo "ok.\n";return true;                
            }else{
                return false;                    
            }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文