if(isset($ _ POST())总是false

时间:2016-03-15 04:35:22

标签: php mysql

$_POST数组由于某种原因没有更新,不确定原因。

这是我的代码,写在一个名为database.php的文件中:

$user = 'root';
$password = '';
$db = 'comments_schema';
$host = 'localhost:3306';

$mysqli = mysqli_connect('localhost', $user, $password, $db);
$field1_name = "";

if(isset($_POST['field1_name'])) {
    $field1_name = $_POST['field1_name'];
}
else {
    echo "something is wrong here";
}
$field_name = mysqli_real_escape_string($mysqli, $field1_name);

$sql = 'INSERT INTO parent_comment(commentid, comment) VALUES   
('.commentid.', '.$field_name.')';
$result = $mysqli->query($sql);

以下是该部分的index.html部分:

<form action="database.php" method="post">
    Comments: <input type="text" name="field1_name"/>
    <input type="Submit" name="Submit" value="submit query"/>
</form>

在这种情况下,为什么isset总是返回false?

1 个答案:

答案 0 :(得分:0)

编辑:我不知道是不是这样,但检查你的max_input_vars值是不是php.ini中的低号

php_value max_input_vars 6000 //6K is the value you need


//first if checks if the form was submitted first, so you don't always display the error.
if(isset($_POST['Submit'])){
    $user = 'root';
    $password = '';
    $db = 'comments_schema';
    $host = 'localhost:3306';

    $mysqli = mysqli_connect('localhost', $user, $password, $db);
    $field1_name = $_POST['field1_name'] ?? ""; //Use only if you are using PHP 7+ Otherwise leave the code as it was but wrap it inside the outer if. 

    if(empty($field_name)){
        echo "something is wrong here";
    }
    $field_name = mysqli_real_escape_string($mysqli, $field1_name);

    $sql = 'INSERT INTO parent_comment(commentid, comment) VALUES ('.commentid.', '.$field_name.')';
    $result = $mysqli->query($sql);
}

更多信息:https://lornajane.net/posts/2015/new-in-php-7-null-coalesce-operator