加载页面时未定义的索引

时间:2014-04-16 17:04:14

标签: php html mysql

当页面加载时,我得到7次枚举的未定义索引错误消息,我假设它是每个变量的1条消息。

当我点击提交时,所有表单数据仍然会提交给数据库。

提交表单后,Undefined Index错误消失了!在页面重新加载。

怪异

<!DOCTYPE html>
<?php
$con=mysqli_connect("localhost","root","","project1");
// Check connection
        if (mysqli_connect_errno())
          {
          echo "Failed to connect to MySQL: " . mysqli_connect_error();
          }
// check variables set
if (isset($_POST['submit']))
{
    $site_code = $_POST['site_code'];
    $site_name = $_POST['site_name'];
    $site_address = $_POST['site_address'];
        $site_city = $_POST['site_city'];
    $site_postalcode = $_POST['site_postalcode'];
    $province = $_POST['province'];
        $country = $_POST['country'];
}
// Query from Countries table
$query_countries = "select * from countries";
$country_results = mysqli_query($con,$query_countries);
$number_of_returns_country = mysqli_num_rows($country_results);
// Query from Provinces Table
$query_provinces = "select * from provinces";
$provinces_results = mysqli_query($con,$query_provinces);
$number_of_returns_province = mysqli_num_rows($provinces_results);

//insert form values into sites table
$sql_site="INSERT INTO sites (site_code, site_name, site_address, site_city, site_postalcode, id_province, id_country)
        VALUES
        ('$_POST[site_code]','$_POST[site_name]','$_POST[site_address]','$_POST[site_city]','$_POST[site_postalcode]',$_POST[province],$_POST[country])";
mysqli_query($con,$sql_site);        
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="site.css">
</head>
<body>
<h1>Insert Site into DB</h1>
    <h2 class="button"><a href=/index.html>Home</a></h2>
    <h2 class="button"><a href=/insert.php>add site</a></h2>
    <h2 class="button"><a href=/delete.html>delete site</a></h2>
    <h2 class="button"><a href=/search.html>search site</a></h2>
        <form class="insert" action="insert.php" method="post">
                <h3>Site Info</h3>
                        Site code: <input type="text" name="site_code"><br>
                        Site name: <input type="text" name="site_name"><br>
                        Address: <input type="text" name="site_address"><br>
                        City: <input type="text" name="site_city"><br>
                        Postal code: <input type="text" name="site_postalcode"><br>
                        Province: <select name="province">
                                        <?php while($row = mysqli_fetch_assoc($provinces_results)){ ?>
                                        <option value="<?php echo $row['id'];?>"><?php echo $row['province'];?></option>
                                        <?php } ?>
                                </select><br>
                        Country: <select name="country">
                                        <?php while($row = mysqli_fetch_assoc($country_results)){ ?>
                                        <option value="<?php echo $row['id'];?>"><?php echo $row['country'];?></option>
                                        <?php } ?>
                                </select><br>
                <h3>Site Contact Info</h3>
                        Site contact name: <input type="text" name="site_contact_name"><br>
                        Phone number 1: <input type="number" name="site_contact_number1"><br>
                        Phone number 2: <input type="number" name="site_contact_number2"><br>
                        Email address: <input type="email" name="site_contact_email"><br> 
                        <input type="submit">
        </form>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

这是因为,当您第一次加载页面时,未设置表单的数据。当您编译并发送它时,错误不会仅仅因为现在设置了数据而显示。

答案 1 :(得分:0)

您收到错误,因为$ _POST变量中没有数据。要解决此问题,您必须在提交按钮中添加名称:

<input type="submit" name="form_posted">

并将您的PHP代码包含在以下内容中:

if(isset($_POST['form_posted'])) {

}

或者,您可以在PHP之上添加此项,以排除警告:

error_reporting(E_ALL ^ E_WARNING);