简体   繁体   English

PostgreSQL INSERT语句不适用于带有PDO的PHP

[英]PostgreSQL INSERT statement not working in PHP with PDO

This one is leaving me scratching my head. 这个让我挠头。 I'm sure I'm missing something simple, but for the life of me I can't see what when I compare it to other INSERT statements I've written that work just fine. 我确定我缺少一些简单的东西,但是在我的一生中,当我将其与我编写的其他INSERT语句进行比较时,我看不到什么。 The parameters are being passed when I check the network tab in developer, but it won't forward to the page specified in the header, and the table is not updated with information when I fill in the form. 当我检查开发人员中的“网络”选项卡时,将传递参数,但不会将其转发到标题中指定的页面,并且当我填写表单时,表中的信息不会更新。

Here is the function I wrote in the model layer: 这是我在模型层中编写的函数:

function add_yarn($yarnbrand, $yarnamount, $yarnweight, $yarncolor) {
global $db;
$query = 'INSERT INTO yarn
             (yarnbrand, yarnamount, yarnweight, yarncolor)
          VALUES
             (:yarnbrand, :yarnamount, :yarnweight, :yarncolor)';

$statement = $db->prepare($query);
$statement->bindValue(':yarnbrand', $yarnbrand);
$statement->bindValue(':yarnamount', $yarnamount);
$statement->bindValue(':yarnweight', $yarnweight);
$statement->bindValue(':yarncolor', $yarncolor);
$statement->execute();
$statement->closeCursor();
}

Here is the control layer in for the action associated with that function in index.php: 这是index.php中与该函数关联的操作的控制层:

case 'yarn_add' :
    $yarnbrand = filter_input(INPUT_POST, 'yarnbrand');
    $yarnamount = filter_input(INPUT_POST, 'yarnamount', FILTER_VALIDATE_INT);
    $yarnweight = filter_input(INPUT_POST, 'yarnweight');
    $yarncolor = filter_input(INPUT_POST, 'yarncolor');

    if ($yarnbrand == NULL || $yarnamount == NULL ||
            $yarnweight == NULL || $yarncolor == NULL) {
        echo 'Empty or invalid data input.';
    } else { 
        add_yarn($yarnbrand, $yarnamount, $yarnweight, $yarncolor);
        header('Location: index.php?action=view_yarn');
    }
    break;

and here is the view layer with the add form: 这是带有添加表单的视图层:

<?php include '../view/header.php'; ?>

<main>
<h1>Add Yarn</h1>
    <form action="index.php" method="post" id="add_yarn_form">
        <input type="hidden" name="action" value="yarn_add">

        <label>Brand:</label>
        <input type="text" name="yarnbrand"> <br>

        <label>Weight:</label>
        <input type="text" name="yarnweight"><br>

        <label>Amount:</label>
        <input type="text" name="yarnamount"><br>

        <label>Color:</label>
        <input type="text" name="yarncolor"><br>

        <label>&nbsp;</label>
        <input type="submit" value="Save Changes"><br>
    </form>
<div class="bottomtext">
    <a href="index.php?action=view_yarn">View Yarns</a>
    <a href="index.php?action=list_supplies">View Supplies</a>
</div>

</main>
<?php include '../view/footer.php'; ?>

I appreciate any help! 感谢您的帮助!

Maybe a variable with an illegal caracter ? 也许带有非法角色的变量? You don't use filters in your filter_input, expected for the integer variable. 您不需要在filter_input中使用用于整数变量的过滤器。

Take a look on the manual : 看一下手册

If omitted, FILTER_DEFAULT will be used, which is equivalent to FILTER_UNSAFE_RAW. 如果省略,将使用FILTER_DEFAULT,它等效于FILTER_UNSAFE_RAW。 This will result in no filtering taking place by default. 默认情况下,这将导致不进行任何过滤。

Not really secure :/ 不是很安全:/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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