【问题标题】:I am getting an undefined index error [duplicate]我收到未定义的索引错误 [重复]
【发布时间】:2016-01-11 19:00:21
【问题描述】:

我收到以下错误:

注意:未定义的索引:第 13 行 /Applications/XAMPP/xamppfiles/htdocs/Assesment/add_student.php 中的 first_name

注意:未定义索引:第 14 行 /Applications/XAMPP/xamppfiles/htdocs/Assesment/add_student.php 中的 last_name

注意:未定义的索引:第 15 行 /Applications/XAMPP/xamppfiles/htdocs/Assesment/add_student.php 中的年龄

注意:未定义的索引:第 16 行 /Applications/XAMPP/xamppfiles/htdocs/Assesment/add_student.php 中的移动设备

注意:未定义索引:第 17 行 /Applications/XAMPP/xamppfiles/htdocs/Assesment/add_student.php 中的电子邮件

注意:未定义索引:地址在 /Applications/XAMPP/xamppfiles/htdocs/Assesment/add_student.php 第 18 行

注意:未定义的索引:第 19 行 /Applications/XAMPP/xamppfiles/htdocs/Assesment/add_student.php 中的 club_role 错误:键 'PRIMARY' 的重复条目 ''

这是我的代码:`

    <h1> Add a new student</h1>
    <p>Add a new student to the system using the form below</p>

    <?php

    /*information posted from booking form*/
    $first_name=$_POST['first_name'];
    $last_name=$_POST['last_name'];
    $age=$_POST['age'];
    $mobile=$_POST['mobile'];
    $email=$_POST['email'];
    $address=$_POST['address'];
    $club_role=$_POST['club_role'];


    /*connect to DBMS*/
    $con=mysql_connect("localhost", "root", "");
    if (!$con)
    {
        die ('Could not connect: ' .mysql_error());
    }

    /*connect to Database*/
    mysql_select_db("91368_jurgen_glee",$con);

    /*insert into records*/
    $sql="INSERT INTO glee (`first_name`, `last_name`, `age`, `mobile`, `email`, `address`, `club_role`)
    VALUES ('$first_name', '$last_name', '$age', '$mobile', '$email', '$address', '$club_role')";

    mysql_Query($sql, $con) or die ('Error: '.mysql_error());

    $result=mysql_query("SELECT *
        WHERE `first_name` = '$first_name'
        AND `last_name` = '$last_name'
        AND `age` = '$age'
        AND `mobile` = '$mobile'
        AND `email` = '$email'
        AND `address` = '$address' 
        AND `club_role` = $club_role'");
        while ($row = mysql_fetch_array($reslut))

    /*close DBMS Connection*/
    mysql_close($con);

    ?>      
` 这是html代码:`
                <div id="body"> 
                    <h1> Add a new student</h1>
                    <p>Add a new student to the system using the form below</p>

                    <form name="add_student" action="add_student.php">

                        <table class="add_student">
                            <tr>
                                <td class="form">
                                    <b>First Name</b></br>
                                    <input type="text" name="first_name" size="30" value="">
                                </td>

                                <td class="form">
                                    <b>Last Name</b></br>
                                    <input type="text" name="last_name" size="30" value="">
                                </td>
                            </tr>   
                            <tr>
                                <td class="form">
                                    <b>Mobile</b></br> 
                                    <input type="text" size="10" name="mobile" min="1" max="10" value="">
                                </td>

                                <td class="form">
                                    <b>Email</b></br>
                                    <input type="text" name="email" size="15" value="">
                                </td>

                                <td class="form">
                                    <b>Age</b></br>
                                    <input type="number" name="age" size="2" min="1" max="30" value="">
                                </td>
                            </tr>



                                    <td class="form">
                                        <br>
                                    </h1><b>Address</b></h1>
                                    <select name="address">
                                        <option value="01">Please select one</option>
                                        <option value="02">Red Beach</option>
                                        <option value="03">Orewa</option>
                                        <option value="04">Silverdale</option>
                                        <option value="05">Dairy Flat</option>
                                    </select>
                                </td>
                                <br>

                                    <td class="form">
                                        <br>
                                    </h1><b>Club Role</b></h1>
                                    <select name="club-role">
                                        <option value="01">Please select one</option>
                                        <option value="02">Lead</option>
                                        <option value="03">Backstage</option>
                                        <option value="04">Chorus</option>
                                        <option value="05">Orchestra</option>
                                    </select>
                                </td>

                            </tr>

                            <tr>
                                <td colspan="2" class="submit">
                                    <br>
                                    <input type="submit" value="SUBMIT">
                                </td>
                            </tr>


                        </table>

                    </form>

            </div> 


            </div>
    </body> 
</html> 

`

【问题讨论】:

  • 我们可以查看您的 HTML 表单以匹配 $_POST 输入吗?
  • 刚刚添加了 HTML 代码

标签: php mysql apache


【解决方案1】:

您收到此通知是因为数组 $_POST 不像其他数组那样具有 first_name 键。它们只会在您发送POST request(例如提交表单或进行 AJAX 调用)时出现。

1。一个用于 GET 和 POST 请求的文件。

在您的 sn-p 中,我可以看到您有一个用于 GET 方法和 POST 方法的 *.php 文件。为避免这种情况,您可以制作两个不同的文件:indexPost.php 和 index.php 并更改 HTML 代码:

<form action="indexPost.php" method="POST">

或为您的密钥设置条件:

<?php
    if (isset($_POST['first_name']) && !empty($_POST['first_name'])) {
        // Your code
    }
?>

每个键都需要自己的条件语句。

2。 GET 和 POST 请求的单独文件

如果我对一个 *.php 文件有误。

使用条件语句(查看上面的代码)删除所有列出的通知。您还可以改进它以验证表单和 mysql 查询。

3。键 Primary 的重复条目

您的一列具有不能在一个表中重复的主键。假设列last_name 具有主键。然后在整个表中,last_name 列中只能有一行值为LoremIpsum。

【讨论】:

    【解决方案2】:

    您需要在form上指定method到POST,即:

    <form method="POST" name="add_student" action="add_student.php">
    

    【讨论】:

    • 刚刚添加了它,它仍然给我同样的错误
    • 您是否在 &lt;input type="text" name="first_name" size="30" value=""&gt; 中发布任何值?似乎并非如此。确保您发布任何数据。
    猜你喜欢
    • 2015-09-08
    • 2012-04-23
    • 2012-09-30
    • 1970-01-01
    • 2018-04-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多