我收到一个未定义的索引错误

时间:2015-10-13 21:06:47

标签: php mysql apache

我收到了这些错误:

注意:未定义索引:第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中的未定义索引:mobile

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

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

注意:未定义的索引:第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> 

`

2 个答案:

答案 0 :(得分:0)

您需要在method上指定POSTform,即:

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

答案 1 :(得分:0)

您收到此通知,因为数组$_POST没有first_name键,就像其他键一样。只有当您POST request提交表单或进行AJAX调用时,它们才会出现。

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

在您的代码段中,我可以看到您有{.1}}方法和GET方法的* .php文件。要避免这种情况,您可以制作两个不同的文件:POSTindexPost.php并更改HTML代码:

index.php

或为您的钥匙设置条件:

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

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

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

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

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

3。密钥主要

的重复条目

您的一个列具有主键,该键不能在一个表中重复。假设列<?php if (isset($_POST['first_name']) && !empty($_POST['first_name'])) { // Your code } ?> 具有主键。然后在整个表中,last_name列中只能有一行值LoremIpsum

相关问题