使用帖子时未定义的索引

时间:2011-11-24 17:08:06

标签: php

我在$ _POST变量上遇到未定义的索引错误。 I.E. $ _ POST [ '主机']。我知道为什么我得到它们,但有没有办法将php和表单保存在同一个文件中而不会出现这些错误?

<html>
<head>
    <title>Install Forum</title>
</head>
<body>
<h1>Please enter your database information</h1>

<form action='install.php' method='post'>
Hostname: <input type='text' name='hostname'><br/>
MySQL User: <input type='text' name='dbuser'><br/>
MySQL Pass: <input type='password' name='dbpassword'><br/>
<input type='submit' name='submit' value='Submit'><br/>
</body>
</html>
<?php
include_once("config.php");
$date = date('Y-m-d');

//Database Variables
$dbhost = strip_tags($_POST['hostname']);
$dbuser = strip_tags($_POST['dbuser']);
$dbpass = strip_tags($_POST['dbpassword']);
$submit = isset($_POST['submit']);

1 个答案:

答案 0 :(得分:4)

将您的PHP代码包含在if isset条件下。这样,只有在提交表单后才会运行代码:

if(isset($_POST['submit']))
{
    include_once("config.php");
    $date = date('Y-m-d');

    //Database Variables
    $dbhost = strip_tags($_POST['hostname']);
    $dbuser = strip_tags($_POST['dbuser']);
    $dbpass = strip_tags($_POST['dbpassword']);
    $submit = isset($_POST['submit']);
}