2017-06-18 99 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()很好。再次感谢你。 –