2016-03-15 66 views
0

$_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

+0

删除$ field1_name =“ “; –

+3

供参考如果表格被提交,即使它是空的,它也会一直设置。 – AbraCadaver

+0

@DhavalPatel:什么!?!? – AbraCadaver

回答

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

+0

这并不回答他的问题,但我不知道新的'''操作符,因此,我一个人抵抗这种冲动的冲动:-) – billynoah

+0

@billynoah哦,我以为我回答了这个问题,我误解了它。感谢您的观察,我会尝试尽快编辑我的答案。 :d –

相关问题