2016-03-01 143 views
0

我試圖創建一個插入表單,用戶可以將新產品(甜)的詳細信息插入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> 

回答

0

的錯誤是在準備SQL或至少在execute功能。您不要將所有請求的值綁定到SQL字符串中定義的參數。

具體是:product_title中的SQL和:title在您執行method

$insert_pro->execute(array(
    ":product_image" => $product_image, 
    ":product_title" => $product_title, 
    ":product_desc" => $product_desc, 
    ":product_keywords" => $product_keywords, 
    ":product_price" => $product_price, 
    ":product_cat" => $product_cat, 
    ":product_brand" => $product_brand 
)); 

它應該是:

if($results){ 
    // product has been successfully added. 
} 
+0

我所做的更改到指定的代碼,但是我現在收到此新的錯誤:解析錯誤:語法錯誤,意想不到的「[」,預計「)」 – Mani

+0

也許你正在使用舊版本的PHP。將'[arraydata]'更改爲'array(arraydata)' – Xorifelse

+0

這是我使用的版本:當前PHP版本:5.2.5。我也做了更改,但錯誤仍然存​​在。 – Mani

0

我的表單名稱不匹配我的數據庫表-items-名稱。我需要做的只是調整名稱以匹配-items表中的名稱。瘋狂的錯誤但簡單的解決方案。 :)無論如何謝謝你的幫助@Xorifelse。

+0

我真的不明白這一點,** HTML表單名稱**不匹配數據庫**中的**列名稱?這兩種語言甚至不相互溝通。正確的問題是'$ _POST ['title']'爲空,因爲您的HTML表單輸入使用了不同的名稱'name =「product_title」'so'$ _POST ['product_title']'就是解決方案。 – Xorifelse

相關問題