简体   繁体   中英

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

the backend part:

$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
    ));

Front end:

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

So, this is my code above. It's for some reason giving me an error but I just can't undertand why. I've scoured the internet, and all I can find is something doesn't match something. I've gone through this so many times, its starting to annoy me now.

Any ideas?

尝试用:product_title替换:product-title

When in doubt, break your query into as many lines as possible so that you can sniper the issue rather than having MySQL always report an error on line 1:

Turn

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

Into

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

Also, is :product-title a valid placeholder?

Beyond the sql placeholder name problem, you have multiple OTHER bugs:

Field names are case sensitive:

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

$_FILES is an array of arrays:

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

You can NOT bind array to a query placeholder. No idea what you're trying to do here - insert the actual file contents, or just the name of the file? Either way, it should be something like

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

to bind only one particular value out of the array.

instead of having the array as a parameter for the execute function you should bind all the values separately using bindValue. If you passed them as a parameter every value would be treated as a string, with bindValue you maintain the type of the variable. The code would look something like this:

$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();

I hope that this helps.

Succes gewenst

looks like you have $category = $_POST[' Category ']; when you are sending 'category'. This is causing an undefined index. lowercase the c. Along with Alex answer and you should be good

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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