繁体   English   中英

使用$ _POST将数据插入数据库时​​出错

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM