I am trying to create an insert form where the user can insert the details of a new product (sweet) into a MYSQL database.
The product image is inserted into the specified directory item_images/$product_image
.
However, that item is not inserted into the database and following error is produced:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]:
Invalid parameter number: parameter was not defined' in /home/k1335948/www/loginregister-master/insert_product.php:168
Stack trace: #0 /home/k1335948/www/loginregister-master/insert_product.php(168): PDOStatement->execute(Array) #1 {main}
thrown in /home/k1335948/www/loginregister-master/insert_product.php on line 168
Database connection is fine as my other statements work for other elements.
I don't have much experience with PDO and I have been looking for solutions both here and online.
if(isset($_POST['insert_post'])) {
$product_title = $_POST['title'];
$product_cat = $_POST['item_cat'];
$product_brand = $_POST['item_brand'];
$product_price = $_POST['price'];
$product_desc = $_POST['description'];
$product_keywords = $_POST['keyword'];
// retrieving image
$product_image = $_FILES['product_image']['name'];
$product_image_tmp = $_FILES['product_image']['tmp_name'];
move_uploaded_file($product_image_tmp,"item_images/$product_image");
$insert_product = "INSERT INTO items(photo, title, description, keyword, price, item_category, item_brand) VALUES (:product_image, :product_title, :product_desc, :product_keywords, :product_price, :product_cat, :product_brand)";
$insert_pro = $db->prepare($insert_product);
$results = $insert_pro->execute(array(
":product_image" => $product_image,
":title" => $product_title,
":product_desc" => $product_desc,
":product_keywords" => $product_keywords,
":product_price" => $product_price,
":product_cat" => $product_cat,
":product_brand" => $product_brand));
if($insert_pro){
echo "<script>alert('Product has been added sucessfully')</script>";
echo "<script>window.open('insert_product.php','_self')</script>";
}
}
(Edited) Here is my form:
<form action ="insert_product.php" name="insert_form" method="post" enctype = "multipart/form-data">
<table align="center" width="100%" border="2" bgcolor="pink">
<tr>
<td align="right"><b>Product Title: </b></td>
<td>
<input type="text" name="product_title" /></td>
</tr>
<tr>
<td align="right"><b>Product Category: </b></td>
<td>
<select name="product_cat" />
<option>Select Category: <option>
<?php getCat(); ?>
</select>
</td>
</tr>
<tr>
<td align="right"><b>Product Brand: </b></td>
<td>
<select name="product_brand" />
<option>Choose Brand: <option>
<?php getBrand(); ?>
</select>
</td>
</tr>
<tr>
<td align="right"><b>Product Image: </b></td>
<td>
<input type="file" name="product_image" /></td>
</tr>
<tr>
<td align="right"><b>Product Price: </b></td>
<td>
<input type="text" name="product_price" /></td>
</tr>
<tr>
<td align="right"><b>Product Description: </b></td>
<td>
<textarea name="product_desc" cols="20" rows="10"></textarea></td>
</tr>
<tr>
<td align="right"><b>Product Keywords: </b></td>
<td>
<input type="text" name="product_keywords" /></td>
</tr>
<tr align="center">
<td colspan="8"><input type="submit" name="insert_post" value="Insert New Product" /></td>
</tr>
</form>