参数号无效:参数未定义错误

时间:2016-03-01 22:15:00

标签: php mysql pdo

我正在尝试创建一个插入表单,用户可以将新产品(sweet)的详细信息插入到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>

2 个答案:

答案 0 :(得分:0)

错误在于准备SQL或至少在execute函数上。您不会将所有请求的值绑定到SQL字符串中定义的参数。

特别是SQL中的:product_title和执行: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.
}

答案 1 :(得分:0)

我的表单名称与我的数据库表中的名称不匹配--items-。我需要做的就是调整名称以匹配-items-表中的名称。疯狂的错误但简单的解决方案。 :)无论如何,感谢@Xorifelse的帮助。