繁体   English   中英

消息为“ SQLSTATE [HY093]”的未捕获异常“ PDOException”。 我不明白怎么了

[英]Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]. I don't understand what's wrong

后端部分:

$producttitle = $_POST['product-title'];
$price = $_POST['price'];
$category = $_POST['Category'];
$file = $_FILE['file_upload'];
$description = $_POST['description'];

$adq = "INSERT INTO Advertenties (Titel, Prijs, Categorie, Image, Beschrijving) VALUES (:product-title, :price, :category, :file_upload, :description);";

$query = $GLOBALS['$odb']->prepare($adq);


$results = $query->execute(array(       
    ":product-title" => $producttitle,
    ":price" => $price,
    ":category" => $category,
    ":file_upload" => $file,
    ":description" => $description
    ));

前端:

 <?php
require_once('../classes/layout_shared.php');

?>

<html lang="NL">
    <head>
        <meta charset="UTF-8">
        <link rel="stylesheet" href="../css/bootstrap.css">
    </head>
    <body>
        <div class="container">
            <div class="col-md-9">
                <form method="POST" action="../classes/upload.php" class="ad-form" enctype="multipart/form-data">
                    <lable>Titel</lable>
                        <input class="form-control" type="text" name="product-title"/>
                   <lable>Bedrag/Bieden vanaf:</lable>
                        <input class="form-control" type="text" name="price"/><br>
                        <lable>Categorie</lable>
                        <input class="form-control" type="" name="category"/><br>
                   <lable>Image</lable>
                        <input class="form-control" type="file" name="file_upload"/> <br>
                   <lable>Beschrijving:</lable>
                       <textarea class="form-control" placeholder="Voeg een beschrijving van het product toe" name="description"></textarea></br>
                   <input type="submit" name="toevoegen" value="Toevoegen"/>
                </form> 
            </div>
        </div>
    </body>
</html>

所以,这是我上面的代码。 由于某种原因给我一个错误,但我无法理解为什么。 我搜寻了互联网,发现的所有东西都不匹配。 我经历了这么多次,现在开始惹恼我。

有任何想法吗?

尝试用:product_title替换:product-title

如有疑问,请将查询分成尽可能多的行,以便您可以狙击问题,而不是让MySQL始终在第1行报告错误:

$adq = "INSERT INTO Advertenties (Titel, Prijs, Categorie, Image, Beschrijving) VALUES (:product-title, :price, :category, :file_upload, :description);";

$adq = "INSERT INTO Advertenties
        (Titel,
        Prijs,
        Categorie,
        Image,
        Beschrijving)
        VALUES
        (:product-title,
        :price,
        :category,
        :file_upload,
        :description);";

另外, :product-title是有效的占位符吗?

除了sql占位符名称问题之外,您还有多个其他错误:

字段名称区分大小写:

$category = $_POST['Category'];
                    ^---
<input class="form-control" type="" name="category"/><br>
                                          ^----

$ _FILES是一个数组数组:

$file = $_FILE['file_upload'];
":file_upload" => $file,

您不能将数组绑定到查询占位符。 不知道您要在这里做什么-插入实际文件内容,还是仅输入文件名? 无论哪种方式,它都应该像

":file_upload" => $file['tmp_name'],

只能将一个特定值绑定到数组之外。

而不是将数组作为execute函数的参数,您应该使用bindValue分别绑定所有值。 如果将它们作为参数传递,则每个值都将被视为字符串,使用bindValue可以维护变量的类型。 代码看起来像这样:

$adq = "INSERT INTO Advertenties (Titel, Prijs, Categorie, Image, Beschrijving) VALUES (:product-title, :price, :category, :file_upload, :description);";
$query = $GLOBALS['$odb']->prepare($adq);
$query->bindValue(":product-title", $producttitle);
$query->bindValue(":price", $price);
$query->bindValue(":category", $category);
$query->bindValue(":file_upload", $file);
$query->bindValue(":description", $description);
$result = $query->execute();

我希望这个对你有用。

成功gewenst

看起来您有$ category = $ _POST [' Category ']; 当您发送“类别”时。 这导致未定义的索引。 小写的c。 随着亚历克斯回答,你应该很好

暂无
暂无

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

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