2017-06-18 28 views
0

我想使用php製作一個web應用程序。在那個應用程序中,我需要創建批處理,批處理主題等我已經complited這個應用程序的主要部分。 Althou這是工作,但我喜歡歌廳的錯誤通知:

Notice: Undefined index: currentBatchId in C:\wamp64\www\sp\addBatchSubject.php on line 4 

我已經通過了由「batchview.php」頁面中的批次ID使用此代碼添加了一批主題:

<a href="addBatchSubject.php?currentBatchId=<?php echo $s['batchId']; ?>">Add Subject</a> 

通過使用下面的代碼:

$currentBatchId=$_GET['currentBatchId']; 

我可以收到該值,並可以顯示在此頁面沒有任何問題。但是,當我想一些數據添加到使用此代碼數據庫:

if(isset($_POST['add'])) 

雖然我按下[添加}按鈕,它產生的錯誤通知,但數據插入到數據庫中成功。現在我想刪除錯誤。 是什麼問題?而數據插入代碼將數據發佈到數據庫,$ _GET是否嘗試獲取另一個值? 注意:$ _GET & $ _POST在同一頁。

回答

0

如果我跟着你的權利,我認爲你需要切換您的代碼周圍的秩序..

<?php 

if(isset($_POST['add'])) { 
    // handle adding new 
    // make sure to header("Location: xxx.php"); to remove post data 
    exit(); // because we don't need to continue 
} 

if(isset($_GET['currentBatchId'])) { 
    $currentBatchId=$_GET['currentBatchId']; 
} 

// then maybe you also need to handle the no post/get request 
if(!isset($_POST['add']) && !isset($_GET['currentBatchId'])) { 
    // handle this case 
    // maybe header("Location: blah.php"); 
    // maybe exit(); here too because we don't have enough information to render the page 
} 

希望是有道理的

+0

嗨戴爾非常感謝。如您所說,我重新安排了代碼。 exit()很好。再次感謝你。 –