[英]Geting error while using $_POST to insert data to database
我正在嘗試使用php制作一個webapp。 在該應用程序中,我需要創建“批處理”,“批處理主題”等。我已對該應用程序的主要部分進行了整理。 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在同一頁面中。
如果我正確地關注您,我認為您需要更改代碼的順序。
<?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
}
希望有道理
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.