准备好的声明不发送到数据库。思考img文件上传可能是问题的一部分

时间:2015-05-30 03:31:06

标签: php html mysql image prepared-statement

我正在尝试创建我的第一个预备语句,并且在将数据发送到数据库时遇到了麻烦。我正在尝试发送产品信息并将img文件发送到文件夹,然后将永久文件名移动到我的数据库。

截至目前,没有数据发送到我的数据库。唯一发送的是我的图片文件到我的productpics文件夹。我尽可能地尝试自己这样做,并且我已经坚持了很长时间。

有没有人在我的代码中看到准备好的语句有什么问题,或者我是怎么发送导致这个不发送的img文件?

我唯一的错误是我在这下面的$ date变量。我不是太担心,除非它导致这不起作用。我得到的错误是从php错误报告中获取的。

我验证除了此代码之上的img文件以外的所有内容。

//Create
 $date = date('Y-m-d H:i:s');

    if($validation->passed()) {
        if(isset($_POST['create'])){ 
            $product_id = trim( $_POST['product_id'] );
            $name = trim( $_POST['name'] );
            $price = trim( $_POST['price'] );
            $saleprice = trim( $_POST['saleprice'] );
            $final_price = trim( $_POST['final_price'] );
            $shippingprice = trim( $_POST['shippingprice'] );
            $category = trim( $_POST['category'] );
            $item_details = trim( $_POST['item_details'] );
            $item_details2 = trim( $_POST['item_details2'] );
            $description = trim( $_POST['description'] );
            $viewproduct_type = trim( $_POST['viewproduct_type'] );
            $date = ( $_POST['date_created'] );
            $file = "productpics/". $_FILES['file']['name']; // save the filename

            move_uploaded_file($_FILES['file']['tmp_name'], $file); // move the file

        //Connection
        $con = mysqli_connect("localhost","root","","bfb");
        /* check connection */
            if (mysqli_connect_errno()) {
                printf("Connect failed: %s\n", mysqli_connect_error());
                exit();
            }



        /* create a prepared statement */
        if ($stmt = mysqli_prepare($con, "INSERT INTO products ('product_id', 'name', 'price', 'saleprice', 'final_price', 'shippingprice', 'category', item_details', 'item_details2', 'description', 'viewproduct_type', 'date', 'img') VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")) {


            /* bind parameters for markers */
            $stmt->bind_param('isiiiisssssis', $product_id, $name, $price, $saleprice, $final_price, $shippingprice, $category, $item_details, $item_details2, $description, $viewproduct_type, $date, $file);

            /* execute query */
            $stmt->execute();


        /* close statement */
        mysqli_stmt_close($stmt);
            }
        }
    }
 }
}   
?>

表格

<form action="" method="POST" enctype="multipart/form-data">
    <div class="field">
        <label for="product_id">Product ID</label>
        <input type="text" name="product_id" class="smallinputbar" required>
    </div>
    <div class="field">
        <label for="name">Product Name</label>
        <input type="text" class="inputbar" name="name" required>
    </div>
    <div class="field">
        <label for="price">Product Price</label>
        <input type="text" class="smallinputbar" name="price" required>
    </div>
    <div class="field">
        <label for="saleprice">Sale Price</label>
        <input type="text" class="smallinputbar" name="saleprice">
    </div>
    <div class="field">
        <label for="final_price">Final Price</label>
        <input type="text" class="smallinputbar" name="final_price" required>
    </div>
    <div class="field">
        <label for="shippingprice">Shipping Price</label>
        <input type="text" class="smallinputbar" name="shippingprice" required>
    </div>
    <div class="field">
        <label for="category">Category</label>
        <input type="text" class="inputbar" name="category" required>
    </div>
    <div class="field">
        <label for="item_details">Item Details</label>
        <input type="message" class="messageinput" name="item_details" required>
    </div>
    <div class="field">
        <label for="item_details2">Item Details 2</label>
        <input type="message" class="messageinput" name="item_details2">
    </div>
    <div class="field">
        <label for="description">Description</label>
        <input type="message" class="messageinput" name="description" required>
    </div>
    <div class="field">
        <label for="viewproduct_type">View Product Type</label>
        <select class="optionbar" name="viewproduct_type">
            <option name="notSelected">Not Selected</option>
            <option name="viewproduct_type" value="a href='./viewProduct.php?view_product=$id">Standard</option>
            <option name="viewproduct_type" value="Option">Option</option>
        </select>
    </div>

            <input type="hidden" name="token" value="<?php echo Token::generate(); ?>">
            <label for="button">
            <input type="submit" id="button" name="create" value="Create New Product">
            </label>

        <span class="floatright">
            <input type="file" name="file" class="inputbarfile">
            <input type="submit" name="create" id="signinButton" value="Upload">
        </span>
</form>

为什么这不发送的任何想法?

更新代码:

//Create

    if($validation->passed()) {
        if(isset($_POST['create'])){ 
            $product_id = trim( $_POST['product_id'] );
            $name = trim( $_POST['name'] );
            $price = trim( $_POST['price'] );
            $saleprice = trim( $_POST['saleprice'] );
            $final_price = trim( $_POST['final_price'] );
            $shippingprice = trim( $_POST['shippingprice'] );
            $category = trim( $_POST['category'] );
            $item_details = trim( $_POST['item_details'] );
            $item_details2 = trim( $_POST['item_details2'] );
            $description = trim( $_POST['description'] );
            $viewproduct_type = trim( $_POST['viewproduct_type'] );
            $file = "productpics/". $_FILES['file']['name']; // save the filename

            move_uploaded_file($_FILES['file']['tmp_name'], $file); // move the file

        //Connection
        $con = mysqli_connect("localhost","root","","bfb");
        /* check connection */
            if (mysqli_connect_errno()) {
                printf("Connect failed: %s\n", mysqli_connect_error());
                exit();
            }



        /* create a prepared statement */
        if ($stmt = mysqli_prepare($con, "INSERT INTO products (product_id, name, price, saleprice, final_price, shippingprice, category, item_details, item_details2, description, viewproduct_type, date, img) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), ?)")) {


            /* bind parameters for markers */
            $stmt->bind_param('isiiiissssss', $product_id, $name, $price, $saleprice, $final_price, $shippingprice, $category, $item_details, $item_details2, $description, $viewproduct_type, $file);

            /* execute query */
            $stmt->execute();


        /* close statement */
        mysqli_stmt_close($stmt);
            }
        }
    }
 }
}   
?>

然后我删除了选项中的名称部分。              查看产品类型                      未选中的             标准             选项              

1 个答案:

答案 0 :(得分:0)

您的列名称使用了不正确的标识符限定符,即单引号'

删除它们,或用刻度线替换它们。

Ticks示例:

(`product_id`, `name`, `price`, `saleprice`, `final_price`,  
`shippingprice`, `category`, item_details`, `item_details2`, 
`description`, `viewproduct_type`, `date`, `img`)

检查错误会发出语法错误信号。

$stmt->execute();替换为

if(!$stmt->execute()){trigger_error("there was an error....".$con->error, E_USER_WARNING);}您将看到语法错误。

参考文献:

另外,只有<select>具有name属性而不是<option><option>应包含“value”`。