簡體   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