我的GET语句正常工作但我的POST语句不适用

时间:2017-03-02 07:49:16

标签: php

我已经花了几个小时摆弄这个并且无法弄清楚为什么$_GET陈述正确执行,但$_POST陈述不是。

如果$ stock以dB为单位,则在表单中显示值,如果表单已提交,请提交UPDATE这些值,IF $stock不以dB为单位且表单已提交INSERT into table$_POST语句似乎都不起作用,但没有丢失任何错误,只需在按下提交按钮时重定向回同一页面。

    include_once ('../helper_content/sql_Connect.php');
    $error = array();

    $KBB_Low = "";
    $KBB_High = "";
    $KBB_Fair = "";
    $KBB_Retail = "";
    $KBB_URL = "";
    $TrueCar_Great = "";
    $TrueCar_Average = "";
    $TrueCar_Above = "";
    $TrueCar_URL = "";
    $NADA_Trade = "";
    $NADA_Loan = "";
    $NADA_Retail = "";

# Was the form submitted via POST?
    if(isset($_POST['Submit'])) {
    # Yes

    # Is this a new stock item?
        if(empty($_POST['stock'])) {
        # Yes - insert
            $kbb_low = filter_var($_POST['kbb_low'], FILTER_SANITIZE_STRING);
            $kbb_high = filter_var($_POST['kbb_high'], FILTER_SANITIZE_STRING);
            $kbb_fair = filter_var($_POST['kbb_fair'], FILTER_SANITIZE_STRING);
            $kbb_retail = filter_var($_POST['kbb_retail'], FILTER_SANITIZE_STRING);
            $kbb_url = filter_var($_POST['kbb_url'], FILTER_SANITIZE_STRING);
            $truecar_great = filter_var($_POST['truecar_great'], FILTER_SANITIZE_STRING);
            $truecar_average = filter_var($_POST['truecar_average'], FILTER_SANITIZE_STRING);
            $truecar_above = filter_var($_POST['truecar_above'], FILTER_SANITIZE_STRING);
            $truecar_url = filter_var($_POST['truecar_url'], FILTER_SANITIZE_STRING);
            $nada_trade = filter_var($_POST['nada_trade'], FILTER_SANITIZE_STRING);
            $nada_loan = filter_var($_POST['nada_loan'], FILTER_SANITIZE_STRING);
            $nada_retail = filter_var($_POST['nada_retail'], FILTER_SANITIZE_STRING);

            if ($stmt = $conn->prepare("INSERT INTO `Inventory_Valuations` (`stock`, 
                `kbb_low`, `kbb_high`, `kbb_fair`, `kbb_retail`, `kbb_url`, 
                `truecar_great`, `truecar_average`, `truecar_above`, `truecar_url`, 
                `nada_trade`, `nada_loan`, `nada_retail`
            ) VALUES (?,?,?,?,?,?)")) {
                $stmt->bind_param('iiiisiiisiii', $stock, 
                    $kbb_low, $kbb_high, $kbb_fair, $kbb_retail, $kbb_url, 
                    $truecar_great, $truecar_average, $truecar_above, $truecar_url, 
                    $nada_trade, $nada_loan, $nada_retail
                );

                if ($stmt->execute()) {
                    $stmt->close();
                    header('Location: ./?inserted=true'); 
                    exit(); 
                } else {
                    $error[] = "Error adding: " . $stmt->error;
                    $stmt->close();
                }
            }

        } else {
        # No - update
            $stock = $_POST['stock'];
            $kbb_low = $_POST['kbb_low'];
            $kbb_high = $_POST['kbb_high'];
            $kbb_fair = $_POST['kbb_fair'];
            $kbb_retail = $_POST['kbb_retail'];
            $kbb_url = $_POST['kbb_url'];
            $truecar_great = $_POST['truecar_great'];
            $truecar_average = $_POST['truecar_average'];
            $truecar_above = $_POST['truecar_above'];
            $truecar_url = $_POST['truecar_url'];
            $nada_trade = $_POST['nada_trade'];
            $nada_loan = $_POST['nada_loan'];
            $nada_retail = $_POST['nada_retail'];
        /*... get variables from the $_POST array */

            if ($stmt = $conn->prepare("UPDATE `Inventory_Valuations` SET 
                kbb_low=?, kbb_high=?, kbb_fair=?, kbb_retail=?, kbb_url=?, 
                truecar_great=?, truecar_average=?, truecar_above=?, truecar_url=?, 
                nada_trade=?, nada_loan=?, nada_retail=? 
            WHERE stock=?")) {
                $stmt->bind_param('iiiisiiisiii', 
                    $kbb_low, $kbb_high, $kbb_fair, $kbb_retail, $kbb_url, 
                    $truecar_great, $truecar_average, $truecar_above, $truecar_url, 
                    $nada_trade, $nada_loan, $nada_retail, 
                $stock);

                if ($stmt->execute()) {
                    $stmt->close();
                    header('Location: ./?updated=true');
                    exit();
                } 
                else {
                    $error[] = "Error updating: " . $stmt->error;
                    $stmt->close();
                }
            }
        }
    }
    else {
        # No - assume a GET
        $status = 'Active';
        $stock = $_GET['stock'];
        $cat = $_GET['cat'];

        if(isset($_GET['updated'])) {
            $message = "Record updated";
        }
        else if(isset($_GET['inserted'])) {
            $message = "Record added into database";
        }

        if($stock != "") {
        # Load the item?
            $query = "SELECT * FROM `Inventory_Valuations` WHERE stock=?";
            $stmt = $conn->prepare($query);
            $stmt->bind_param('i', $stock);

            if($stmt->execute()) { 
                $result = $stmt->get_result();
                if($result) {
                    $row = $result->fetch_assoc();
                    $KBB_Low = $row['kbb_low'];
                    $KBB_High = $row['kbb_high'];
                    $KBB_Fair = $row['kbb_fair'];
                    $KBB_Retail = $row['kbb_retail'];
                    $KBB_URL = $row['kbb_url'];
                    $TrueCar_Great = $row['truecar_great'];
                    $TrueCar_Average = $row['truecar_average'];
                    $TrueCar_Above = $row['truecar_above'];
                    $TrueCar_URL = $row['truecar_url'];
                    $NADA_Trade = $row['nada_trade'];
                    $NADA_Loan = $row['nada_loan'];
                    $NADA_Retail = $row['nada_retail'];
                }
            }
            $stmt->close(); 
        }
    }

?>
    <?php if(isset($message)) : ?>
    <div class="alert alert-success">
        <?= $message ?>
    </div>
<?php endif; ?>

<?php if(isset($error)) : ?>
    <div class="alert alert-danger">
        <ul>
        <?php foreach($error as $err): ?>
            <li><?= $err ?></li>
        <?php endforeach; ?>
        </ul>
    </div>
<?php endif; ?>

<form method="POST" action="<?= $_SERVER['PHP_SELF']; ?>?cat=Sales&stock=<?= $stock; ?>">
<section class="valuations">
    <h3>Valuations</h3>
    <input type="hidden" name="stock" value="<?= $stock; ?>">
    <div>
        <a target="_blank" href="<?=$KBB_Link; ?>"><img src="images/logos/KBB.png"></a>
        <p>
            <label for="kbb_low">Fair Market Range</label>
            <input type="number" class="dollars" id="kbb_low" name="kbb_low" placeholder="Low" value="<?= $KBB_Low; ?>"> - 
            <input type="number" class="dollars" id="kbb_high" name="kbb_high" placeholder="High" value="<?= $KBB_High; ?>">
        </p>
        <p>
            <label for="kbb_fair">Fair Price</label>
            <input type="number" class="dollars" id="kbb_fair" name="kbb_fair" placeholder="Fair" value="<?= $KBB_Fair; ?>">
        </p>
        <p>
            <label for="kbb_retail">Sug. Retail</label>
            <input type="number" class="dollars" id="kbb_retail" name="kbb_retail" placeholder="Retail" value="<?= $KBB_Retail; ?>">
        </p>
        <p class="clear">
            <label for="kbb_url">Report URL</label>
            <input type="url" id="kbb_url" name="kbb_url" size="20" spellcheck="false" placeholder="www.kbb.com/" value="<?= $KBB_URL; ?>"> 
            <i title="Copy KBB URL" data-clipboard-target="#kbb_url" data-clipboard-action="copy" class="fa fa-clipboard" aria-hidden="true"></i>
        </p>
    </div>
    <div>
        <img src="images/logos/TrueCar.png">
        <p><label for="truecar_great">Great Price</label> <input type="number" class="dollars" id="truecar_great" name="truecar_great" placeholder="Great" value="<?= $TrueCar_Great; ?>"></p>
        <p><label for="truecar_average">Average Price</label> <input type="number" class="dollars" id="truecar_average" name="truecar_average" placeholder="Average" value="<?= $TrueCar_Average; ?>"></p>
        <p><label for="truecar_above">High Price</label> <input type="number" class="dollars" id="truecar_above" name="truecar_above" placeholder="Above" value="<?= $TrueCar_Above; ?>"></p>
        <p class="clear">
            <label for="truecar_url">Report URL</label> <input type="url" id="truecar_url" name="truecar_url" size="20" spellcheck="false" placeholder="www.truecar.com/" value="<?= $TrueCar_URL; ?>"> 
            <i title="Copy TrueCar URL" data-clipboard-target="#truecar_url" data-clipboard-action="copy" class="fa fa-clipboard" aria-hidden="true"></i>
        </p>
    </div>
    <div>
        <a target="_blank" href="http://www.nadaguides.com/Cars/<?= $year; ?>/<?= $make; ?>/<?= $model; ?>"><img src="images/logos/NADA.png"></a>
        <p><label for="nada_trade">Trade</label> <input type="number" class="dollars" id="nada_trade" name="nada_trade" placeholder="Trade" value="<?= $NADA_Trade; ?>"></p>
        <p><label for="nada_loan">Loan</label> <input type="number" class="dollars" id="nada_loan" name="nada_loan" placeholder="Loan" value="<?= $NADA_Loan; ?>"></p>
        <p><label for="nada_retail">Retail</label> <input type="number" class="dollars" id="nada_retail" name="nada_retail" placeholder="Retail" value="<?= $NADA_Retail; ?>"></p>
    </div>

    <input type="submit" id="Submit" value="Submit">
</form>

<script src="include/js/clipboard.min.js"></script>
<script>
    var clipboard = new Clipboard('.fa-clipboard');
    clipboard.on('success', function(e) {console.log(e);});
    clipboard.on('error', function(e) {console.log(e);});
</script>

4 个答案:

答案 0 :(得分:1)

替换

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

if (!empty($_POST))

一般情况下检查是否已发布任何内容(如果POST请求不为空 - >执行此操作)

答案 1 :(得分:0)

您可以尝试通过

检查它是否是POST请求
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    // …
}

也许这有帮助。

您应该使用filter_input来处理POST和GET参数。不推荐使用$ _POST或$ _GET。

答案 2 :(得分:0)

请验证您的提交是否有此...

<input type="submit" value="Submit" name="submit" />

,您的表单方法是

<form method="POST" action="xyz"> ...

答案 3 :(得分:0)

你的代码有点偏。 你正在检查

if(isset($_POST['Submit'])) {

根本没有发布。这就是为什么if部分永远不会被执行的原因。