Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/77.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_Sql_Forms_Variables - Fatal编程技术网

是否将表单输入保存到php变量?

是否将表单输入保存到php变量?,php,sql,forms,variables,Php,Sql,Forms,Variables,我正在编写一个PHP脚本来获取变量的内容并将其插入到我的MYSQL数据库中,当我定义值时,它工作得很好,但使用变量只会产生错误,有人能告诉我将表单输入保存到变量的正确方法吗。(表单与sql脚本位于同一文件中,不包括登录名,因此使用$\u POST不起作用) 表格: 给你的输入一个名字 <input placeholder="Placeholder" id="email" name="email" type="text"> 然后在您的查询中使用$email您正在询问该脚本的许多问题

我正在编写一个
PHP
脚本来获取变量的内容并将其插入到我的
MYSQL
数据库中,当我定义值时,它工作得很好,但使用变量只会产生错误,有人能告诉我将表单输入保存到变量的正确方法吗。(表单与sql脚本位于同一文件中,不包括登录名,因此使用
$\u POST
不起作用)

表格:


给你的输入一个名字

<input placeholder="Placeholder" id="email" name="email" type="text">

然后在您的查询中使用$email

您正在询问该脚本的许多问题。让我们浏览一下所有这些:

1)设置表单属性 告诉浏览器如何将表单数据发送到服务器非常重要。否则,您将不得不依赖超级全局
$\u请求
。引自PHP官方网站:

$\u请求中的变量通过GET提供给脚本, POST和COOKIE输入机制,因此可以通过 无法信任远程用户和

因此,您应该将
方法
属性添加到表单中。您可能还需要添加字符编码,以确保在有人使用非utf-8字符时不会混淆脚本:

<form method="POST" action="" accept-charset="utf-8">
超级全局
$\u POST
现在可以使用
名称
属性值作为键访问该输入字段的值:
$\u POST['email']
。但这仅在发送表单后有效

3)提交您的表格
您不能神奇地期望服务器上的所有表单数据都由您的网站访问者填写。您需要首先提交:

<input type="submit" value="Register email" />
4)设置PHP
在开始使用POST数据之前,我们需要确保用户提供了以下数据:

if(isset($_POST['email']) && !empty($_POST['email'])){
    //...
}
这将验证
$\u POST['email']
是否存在,并确保它不是空的

5)安全地处理用户数据:准备好的语句 作为一名开发人员,您学到的第一件事就是永远不要相信用户数据。将数据输入用户提交的数据库而不进行验证会带来很多麻烦。尤其是

使用,您可以保护自己不受此影响:

//$link will be the connection to your database
//For example: $link = mysqli_connect("localhost", "db_user", "db_pass", "db_name");
if ($stmt = mysqli_prepare($link, "INSERT INTO news (news) VALUES (?)")) {
    /* bind parameters for markers */
    mysqli_stmt_bind_param($stmt, "s", $_POST['email']);

    /* execute query */
    mysqli_stmt_execute($stmt);

    /* close statement */
    mysqli_stmt_close($stmt);

    /* print success message */
    echo "Email successfull registered!";
} else {
    /* print errors */
    printf("MySQL Error: %s\n", mysqli_error($link));
}
将其全部包装在一起:

<?php

if(isset($_POST['email']) && !empty($_POST['email'])){
    //$link will be the connection to your database
    //For example: $link = mysqli_connect("localhost", "db_user", "db_pass", "db_name");
    if ($stmt = mysqli_prepare($link, "INSERT INTO news (news) VALUES (?)")) {
        /* bind parameters for markers */
        mysqli_stmt_bind_param($stmt, "s", $_POST['email']);

        /* execute query */
        mysqli_stmt_execute($stmt);

        /* close statement */
        mysqli_stmt_close($stmt);

        /* print success message */
        echo "Email successfull registered!";
    } else {
        /* print errors */
        printf("MySQL Error: %s\n", mysqli_error($link));
    }
}

?>

<!-- Your HTML here -->
<div class="row">
    <form class="col s12" method="POST" action="" accept-charset="utf-8">
        <div class="row">
            <div class="input-field col s6">
                <input placeholder="Placeholder" id="email" type="text" name="email" />
                <input type="submit" value="Register email" />
            </div>
        </div>
    </form>
</div>
<!-- Your HTML here -->


请与我们分享您尝试过的代码-这很难帮助您。即使您在同一页面上使用,
$\u POST[''']
数组也应该可以工作。当然,除非你一开始就没有寄出你的表格。你能给我们看看你的密码吗?这将让我们了解阵列不工作的原因以及您需要做些什么来解决它。也许您的
需要使用
$\u GET
而不是
$\u POST
,但请确实给我们一些代码。对不起,我是有意的,我的浏览器崩溃了…发布的代码写得不好。@ozzie我正在寻找一个更好的答案,因为依赖
$\u REQUEST
是个坏主意。你能发布你所有的代码、html和php吗。。。我将能够帮助您调试。好的,我一定是做错了什么,这也不起作用,我认为表名后面的()代表行,对吗?谢谢对不起,我是个笨蛋!这是一个很好的答案。你能详细说明一下
标签的
操作属性吗?@PaulSpiegel当然,我现在时间不多,但我很快会更新这个答案。我在这里也看到了一些其他过时的信息,所以也需要解决这个问题。
<input type="submit" value="Register email" />
<form method="POST" action="" accept-charset="utf-8">
    <input placeholder="Placeholder" id="email" type="text" name="email" />
    <input type="submit" value="Register email" />
</form>
if(isset($_POST['email']) && !empty($_POST['email'])){
    //...
}
//$link will be the connection to your database
//For example: $link = mysqli_connect("localhost", "db_user", "db_pass", "db_name");
if ($stmt = mysqli_prepare($link, "INSERT INTO news (news) VALUES (?)")) {
    /* bind parameters for markers */
    mysqli_stmt_bind_param($stmt, "s", $_POST['email']);

    /* execute query */
    mysqli_stmt_execute($stmt);

    /* close statement */
    mysqli_stmt_close($stmt);

    /* print success message */
    echo "Email successfull registered!";
} else {
    /* print errors */
    printf("MySQL Error: %s\n", mysqli_error($link));
}
<?php

if(isset($_POST['email']) && !empty($_POST['email'])){
    //$link will be the connection to your database
    //For example: $link = mysqli_connect("localhost", "db_user", "db_pass", "db_name");
    if ($stmt = mysqli_prepare($link, "INSERT INTO news (news) VALUES (?)")) {
        /* bind parameters for markers */
        mysqli_stmt_bind_param($stmt, "s", $_POST['email']);

        /* execute query */
        mysqli_stmt_execute($stmt);

        /* close statement */
        mysqli_stmt_close($stmt);

        /* print success message */
        echo "Email successfull registered!";
    } else {
        /* print errors */
        printf("MySQL Error: %s\n", mysqli_error($link));
    }
}

?>

<!-- Your HTML here -->
<div class="row">
    <form class="col s12" method="POST" action="" accept-charset="utf-8">
        <div class="row">
            <div class="input-field col s6">
                <input placeholder="Placeholder" id="email" type="text" name="email" />
                <input type="submit" value="Register email" />
            </div>
        </div>
    </form>
</div>
<!-- Your HTML here -->