ssjturtle
ssjturtle

Reputation: 11

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?

Upvotes: 0

Views: 65

Answers (5)

Marc B
Marc B

Reputation: 360862

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.

Upvotes: 1

Andrew Nolan
Andrew Nolan

Reputation: 2107

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

Upvotes: 0

MonkeyZeus
MonkeyZeus

Reputation: 20747

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?

Upvotes: 1

yarwest
yarwest

Reputation: 990

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

Upvotes: 0

Alex
Alex

Reputation: 566

try replacing :product-title with :product_title

Upvotes: 2

Related Questions