我試圖創建一個插入表單,用戶可以將新產品(甜)的詳細信息插入MYSQL數據庫。無效的參數編號:參數未定義錯誤
產物圖像被插入到指定的目錄item_images/$product_image
。
然而,該項目沒有插入到數據庫中,併產生以下錯誤:
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
數據庫連接是罰款,我的其他報表等元素的工作。
我沒有與PDO很多經驗,我一直都在這裏,並在網上尋找解決方案。
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>";
}
}
(編輯)這是我的形式:
<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>
我所做的更改到指定的代碼,但是我現在收到此新的錯誤:解析錯誤:語法錯誤,意想不到的「[」,預計「)」 – Mani
也許你正在使用舊版本的PHP。將'[arraydata]'更改爲'array(arraydata)' – Xorifelse
這是我使用的版本:當前PHP版本:5.2.5。我也做了更改,但錯誤仍然存在。 – Mani