Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/253.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP获取函数的文本框值_Php_Html_Mysql - Fatal编程技术网

PHP获取函数的文本框值

PHP获取函数的文本框值,php,html,mysql,Php,Html,Mysql,我试图使用$\u GET方法将文本框值传输到函数,然后通过查询将其添加到mysql数据库中。我搜索了多个类似的问题,并相应地编辑了我以前的尝试,但碰巧在我当前的代码中没有找到错误。 该文件是database.php,我想在其上单击按钮向mysql数据库添加一个值。我怀疑if$\u GET子句中的错误是函数中$ep的回音,甚至if部分中也没有返回任何内容,因此,无论我在单击Insert ep按钮之前在textfield中输入了什么,都只会将一个带有UID的空条目添加到数据库中。也许我只是错过了或者

我试图使用$\u GET方法将文本框值传输到函数,然后通过查询将其添加到mysql数据库中。我搜索了多个类似的问题,并相应地编辑了我以前的尝试,但碰巧在我当前的代码中没有找到错误。 该文件是database.php,我想在其上单击按钮向mysql数据库添加一个值。我怀疑if$\u GET子句中的错误是函数中$ep的回音,甚至if部分中也没有返回任何内容,因此,无论我在单击Insert ep按钮之前在textfield中输入了什么,都只会将一个带有UID的空条目添加到数据库中。也许我只是错过了或者看不到一些琐碎的东西

    <title> Database </title>
    <body>
    <form action="database.php">
        <input type="text" name="setEp" id="ep" value="" />
        <input type="submit" class="button" name="setEp" value="Insert EP" />

    </form>
    </body>
    <?php
    //Getting the content of the textbox 
    if($_GET){
        if(isset($_GET['setEp'])){
            $ep = isset($_GET['ep']);
            setEp($ep);  //calling the desired function with the retrieved variable
         }
    }    
    //The function declaration
    function setEp($ep){
        //DB connection
    $db_host = "127.0.0.1:6543";
    $db_username = "root";
    $db_pass = "root";
    $db_name = "endpoints";
    $conn = mysql_connect("$db_host","$db_username","$db_pass") or die ("Could not connect to MySQL");

        //Table "enpoint" consists only of names and autoincrement UID
        $ep = mysql_real_escape_string($ep);
        $query = "INSERT INTO endpoint (name) VALUES ('$ep')";
        mysql_query($query);

        echo " SetEP called"; //echo function to see if it was called
        echo "$ep"; // this will never create an input no matter what i did
    ?>
$ep=isset$_GET['ep'];这将返回true,因为这是isset语句的返回值,如果您已验证$\u GET['ep']包含所需的值,请执行以下操作:

$ep = $_GET['ep'];
最好使用输入过滤器来获取这些数据,因为这可能容易受到各种攻击

希望这有帮助。

错误在这里:

$ep = isset($_GET['setEp']);
返回波伦。也许你应该这样做 $ep=mysql\u real\u escape\u string$\u GET['ep'];
防止sql注入并获取$\u get变量的值。

您在错误的位置查找文本框的值,因为setEp是文本框的名称,您确实应该重命名提交按钮,因为这将导致一些重大问题。名称应该是唯一的。完成后,您可以在检查是否已设置后调用该函数,如下所示-

setEp($_GET['setEp']);  
如果可以,你应该这样做。它们不再得到维护,而是在使用。学习替代,并考虑使用PDO,.< 此外,您应该在查询中添加错误检查,例如或DIEU error


最好使用单独的函数连接到数据库,然后将连接信息传递给查询函数。这样,您就不会在每次调用函数时都进行连接。它将使您的代码更干净,并可能消除多个连接的一些问题。

如果可以,您应该这样做。它们不再得到维护,而是在使用。相反,请学习使用PDO,.EP是一个布尔值,它是基于设置EP= ISSET$$GET[(EP)]的方式;不是文本框的值。谢谢您的输入。我知道这个漏洞,我计划实现一个正则表达式,只允许某些字符避免SQL注入,虽然这个应用程序可能永远不会进入在线版本,但感谢您的考虑。关于PDO:我已经实现了一个PDO版本,但是遇到了拒绝访问的问题,尽管旧函数使用相同的连接参数可以正常工作。Regex不足以阻止SQL注入。我相应地更改了$ep,现在我在函数调用时遇到了以下错误:注意:未定义的索引:epthrows相同的错误未定义索引:ep错误与@Abdul answer相同。我真的不知道为什么函数似乎无法识别文本框的id,它必须是“setEp”。请将其设置为$\u GET['setEp'],然后重试。表单中不应该有两个name=setEp。把其中一个换成不同的,你完全正确。使用相同名称的textfield和submit按钮是错误的。将textfield名称更改为其他名称后,我能够检索textfield的信息。