php 如何将字符串/变量传递给函数?
我有一个名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
以这种方式调用您的函数:
另外:将声明更改为:
Call your function this way:
Also: Change the declaration to:
增加值的通用答案:
Generic answer that increments a value:
确保表单提交调用该函数,然后从 POST 数组中获取值。如果使用 php 框架,这会更容易,许多框架会为您提供清理输入的方法。在这种情况下,由于您可能使用数据库查询的输入,因此您需要确保它被正确转义以避免 sql 注入。
mysql_real_escape_string 的 PHP 手册
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