准备好的语句没有插入带有图像文件的数据库

时间:2015-06-09 16:11:27

标签: php html mysql image

我正在尝试使用数据和img文件向我的数据库发送插入查询。这是我第一次尝试发送img文件和准备好的声明。无论我做什么都不会发送,我已经坚持了大约一个月。截至目前,没有任何内容插入我的数据库。

我只是尝试将基本信息发送到我的数据库并将img文件发送到我称为productpics的文件夹,然后将文件名移到我的数据库。

我甚至只是尝试为预处理语句的查询部分添加else语句,并且在提交表单后我无法显示echo部分。我之前已经对数据的某些部分进行了验证(与img上传无关)。我测试了这个,验证工作正常,而不是停止查询。我认为这个问题存在于我准备好的陈述或表格中。

我不确定img上传字段的额外按钮是否会停止播放?但是之前我有一个简单的插入查询和除了img文件之外的所有内容都发送到我的数据库之前。现在什么也没发送。

有没有人看到我做错了什么阻止了这种工作?

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

        }else {
            foreach($validation->errors() as $error) {
                echo $error, '<br>';
            }
            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();
            //if(!$stmt->execute()){trigger_error("there was an error....".$con->error, E_USER_WARNING);}


        /* close statement */
        mysqli_stmt_close($stmt);

                  echo "Success!";
        } else {
                   echo "Failed!";
                    }
                   }
    }
 }
 }   
 ?>

表格。我不确定我的img文件上传部分是否搞砸了?我不知道如何做到这一点。

<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 value="Not Selected">Not Selected</option>
                <option value="a href='./viewProduct.php?view_product=$id">Standard</option>
                <option 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>

0 个答案:

没有答案