doulei1965 2014-04-16 17:04
浏览 49
已采纳

加载页面时未定义的索引

When the page loads i get the undefined index error message enumerated 7 times, I assume it's 1 message per variable.

When I click submit all the form data still get submitted to the DB.

Once I submit the form the Undefined Index error goes away! on page reload.

Weird

<!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条回答 默认 最新

  • doulin8374 2014-04-16 17:07
    关注

    This happens because, when you load your page for the first time, data of the form isn't set. When you compile and send it, the error doesn't show up simply because now data is set.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口