php - 加载页面时未定义索引

标签 php html mysql

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

当我单击“提交”时,所有表单数据仍然会提交到数据库。

一旦我提交表单,未定义索引错误就会消失!页面重新加载时。

奇怪

<!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>

最佳答案

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

关于php - 加载页面时未定义索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23115795/

相关文章:

php - 编码为 json 对象并通过 ajax 发送

php - Foreach 分解字符串停止

jquery - 对齐 CSS Drop Down 的恼人问题

mysql - 如何在查询中设置IF条件?

mysql - MYSQL 中的空绑定(bind)变量

c# - PHP 中的 MACTripleDES

php - 如何在连接类中使用 PDO 时显示 mysql 错误?

html - 杰基尔 : Using links to internal markdown files

javascript - JQuery:将字符串附加到 id 作为内容,而不是 html

php - 返回给定时间/日期的可用性