【问题标题】:Geting error while using $_POST to insert data to database使用 $_POST 将数据插入数据库时​​出错
【发布时间】:2017-11-20 18:16:05
【问题描述】:

我正在尝试使用 php 制作一个 webapp。在那个应用程序中,我需要创建批处理、批处理主题等。我已经编译了这个应用程序的主要部分。尽管它正在工作,但我收到如下错误通知:

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']))

当我按下 [add} 按钮时,它会生成错误通知,但数据已成功插入数据库。现在我想删除错误。 有什么问题?当数据插入代码将数据发布到数据库时, $_GET 是否尝试获取另一个值? 注意:$_GET 和 $_POST 在同一页面中。

【问题讨论】:

    标签: php mysql web-applications


    【解决方案1】:

    如果我没听错的话,我认为你需要改变代码的顺序..

    <?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
    }
    

    希望这是有道理的

    【讨论】:

    • 嗨,戴尔,非常感谢。正如你所说,我已经重新排列了代码。 exit() 效果很好。再次感谢您。
    猜你喜欢
    • 2014-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-25
    • 2014-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多