我不能把表格中的帖子值放在变量中?

时间:2013-05-31 18:07:14

标签: php variables http-post

在下面的代码中,我创建的变量($ host,$ username等)保持为空。我究竟做错了什么?当我用常规字符串创建变量时,它可以正常工作。

<form action="" method="post">
<input type="text" name="host" id="host-input" value="" />
<input type="text" name="dbname" id="db-input" value="" />
<input type="text" name="password" id="password-input" value="" />
<input type="text" name="username" id="username-input" value="" />
<input type="submit" name="submit" value="Submit">
</form>

<?php
if (isset($_POST['submit']))
{
        $host = $POST['host'];
        $username = $POST['username'];
        $dbname = $POST['dbname'];
        $password = $POST['password'];

        $file = 'testbestandje.php';
        // Open the file to get existing content
        $current = file_get_contents($file);
        // Append a new person to the file
        $current .= '<? $conn=mysql_connect("'.$host.'","'.$dbname.'","'.$password.'") or die("Kan geen verbinding maken met de DB server"); 
 mysql_select_db("'.$username.'",$conn) or die("Kan database niet selecteren"); ?>'; 
        // Write the contents back to the file
        file_put_contents($file, $current);
}
?>

1 个答案:

答案 0 :(得分:4)

$_POST是正确的变量名称

    $host = $_POST['host'];
    $username = $_POST['username'];
    $dbname = $_POST['dbname'];
    $password = $_POST['password'];

Documentation