2015-10-13 71 views
-2

我得到這些錯誤:我得到一個未定義的索引錯誤

注意:未定義指數:FIRST_NAME在/Applications/XAMPP/xamppfiles/htdocs/Assesment/add_student.php在線13

注意:未定義指數:姓氏在/Applications/XAMPP/xamppfiles/htdocs/Assesment/add_student.php線14

注意:未定義指數:年齡在/Applications/XAMPP/xamppfiles/htdocs/Assesment/add_student.php第15行

注意:未定義索引:m obile在/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 18

說明:未定義指數:club_role在/Applications/XAMPP/xamppfiles/htdocs/Assesment/add_student.php在線19 錯誤:重複條目''爲'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> 

`

+0

我們可以看到您的HTML表單請匹配$ _POST輸入嗎? –

+0

剛剛添加了HTML代碼 –

回答

0

你因爲數組$_POST沒有,所以得到這個通知first_name鑰匙像另一個。他們只會在您提交表單或進行AJAX調用時纔會出現POST request

1.用於GET和POST請求的一個文件。

在你的代碼片段中,我可以看到你有一個* .php文件,用於GET方法和POST方法。爲了避免這種情況,你可以讓兩個不同的文件:indexPost.phpindex.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。重複輸入密鑰主

其中一個列的主鍵不能在一個表中複製。假設列last_name有主鍵。然後在整個表中,您可以在last_name列中只有一行值爲LoremIpsum

0

您需要在form指定methodPOST,即:

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

剛剛添加,它仍然給我相同的錯誤 –

+0

您是否在'''裏面發佈任何值?它似乎並不如此。確保你發佈了任何數據。 –