在表单中使用enctype时,图像名称不占用

时间:2017-01-18 11:27:21

标签: php html

我为图片上传编写了单页代码,因此当我在表单字段上添加enctype时,图像名称和路径不会保存到数据库中。如果删除enctype只有我给出的路径文件夹名称保存到数据库。我想将完整的文件路径保存到数据库并将图像保存到特定文件夹。 我的代码如下。

      <?php
error_reporting( ~E_NOTICE );
$avalues['employee_id']=Zend_Auth::getInstance()->getStorage()->read()->employee_id;
$eid = $avalues['employee_id'];


// define variables and set to empty values
// define variables and set to empty values
$name_err='';
$name='';
$type_err='';
$type='';
$brand_err='';
$brand='';
$brand_err='';
$brand='';
$model_err='';
$model='';
$condition_err='';
$condition='';
$about_err='';
$about='';
$reserved_price_err='';
$reserved_price='';
$Purchased_price_err='';
$Purchased_price='';
$buy_now_price_err='';
$buy_now_price='';
$sale_end_date_err='';
$sale_end_date='';
$file_err='';
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if (isset($_POST['submit'])) {

        $imgFile = $_FILES['image']['name'];
        $tmp_dir = $_FILES['image']['tmp_name'];
        $imgSize = $_FILES['image']['size'];


        $Purchased_price = $_POST['Purchased_price'];
        $sale_end_date = $_POST['sale_end_date'];
        $listedon=$_POST['listedon'];
        $e = true;
        if (empty($_POST['name'])) {
            $name_err = "Please enter the Product Name";
            $e = false;
        } else {
            $name = $_POST['name'];
            // check if name only contains letters and whitespace
            if (!preg_match("/^[a-zA-Z ]*$/", $name)) {
                $name_err = "Only letters and white space allowed";
                $e = false;
            }
        }
        if (empty($_POST['brand'])) {
            $brand_err = "Please enter the Product Brand";
            $e = false;
        } else {
            $brand = $_POST['brand'];
            // check if name only contains letters and whitespace
            if (!preg_match("/^[a-zA-Z ]*$/", $brand)) {
                $brand_err = "Only letters and white space allowed";
                $e = false;
            }
        }
        if (empty($_POST['model'])) {
            $model_err = "Please enter the Product Brand";
            $e = false;
        } else {
            $model = $_POST['model'];
            // check if name only contains letters and whitespace

        }




        if (empty($_POST['type'])||$_POST['type']=="select") {
            $type_err = "Please select the Product Type";
            $e = false;
        } else {
            $type = $_POST['type'];

                   }


        if (empty($_POST['condition'])||$_POST['condition']=="select") {
            $condition_err = "Please select the Product Condition";
            $e = false;
        } else {
            $condition = $_POST['condition'];

        }


        if (empty($_POST['about'])) {
            $about_err = "Please enter the Product Brand";
            $e = false;
        } else {
            $about = $_POST['about'];
            // check if name only contains letters and whitespace
            if (!preg_match("/^[a-zA-Z ]*$/", $about)) {
                $about_err = "Only letters and white space allowed";
                $e = false;
            }
        }





        if (empty($_POST['Purchased_price'])) {
            $Purchased_price_err = "Please enter the Purchased Price";
            $e = false;
        } else {
            $Purchased_price = $_POST['Purchased_price'];
            // check if name only contains letters and whitespace
            if (!preg_match('/^-?[0-9]+(?:\.[0-9]{1,2})?$/', $Purchased_price)) {
                $Purchased_price_err = "Amount should be greater than 0";
                $e = false;
            }

        }

        if (empty($_POST['buy_now_price'])) {
            $buy_now_price_err = "Please enter the Buy Now Price";
            $e = false;
        } else {
            $buy_now_price = $_POST['buy_now_price'];
            // check if name only contains letters and whitespace
            if (!preg_match('/^-?[0-9]+(?:\.[0-9]{1,2})?$/', $buy_now_price)) {
                $buy_now_price_err = "Amount should be greater than 0";
                $e = false;
            }
        }

        if (empty($_POST['reserved_price'])) {
            $reserved_price_err = "Please enter the Minimum Price";
            $e = false;
        } else {
            $reserved_price = $_POST['reserved_price'];
            // check if name only contains letters and whitespace
            if (!preg_match('/^-?[0-9]+(?:\.[0-9]{1,2})?$/', $reserved_price)) {
                $reserved_price_err = "Amount should be greater than 0";
                $e = false;
            }
            if ($reserved_price >= $Purchased_price || $reserved_price >= $buy_now_price) {
                $reserved_price_err = "Minimum Price should be less than the Purchased and Buy Now Price";
                $e = false;
            }
        }
        if (empty($_POST['sale_end_date'])) {
            $sale_end_date_err = "Please select the Sales End Date";
            $e = false;
        } else {
            $sale_end_date = $_POST['sale_end_date'];

        }

        if (empty($_POST['image'])) {
            $file_err = "Please upload the Product Picture";
            //$e = false;
        } else {
            $ImageName = $_FILES['image']['name'];
            $fileElementName = 'image';
            $path = 'images/';
            $location = $path.$_FILES['image']['name'];
            move_uploaded_file($_FILES['image']['tmp_name'], $location);

        }



    $con = mysqli_connect("localhost", "root", "", "csr");
    if (!$con) {
        die('Could not connect: ' . mysql_error());
    }


}
//final check whether it is true the input will process

    if($e)
    {

        mysqli_select_db($con, "csr");

        mysqli_query($con, "insert into products(employee_id, name, type, brand, model, url, conditions, about, listedon, image_path, reserved_price, Purchased_price, buy_now_price, sale_end_date)
                                   values('$eid','$name','$type','$brand','$model','None','$condition','$about','$listedon','$location','$reserved_price','$Purchased_price','$buy_now_price','$sale_end_date')") or  die('Could not connect: ' . mysql_error());

        header('Location:/sellproduct/index/image');
        mysqli_close($con);

    }

}
?>
<style>
    .error{
        color:red;


    }
</style>



<div class="maincontent">
    <div class="contentinner content-dashboard">
        <div class="widgetcontent bordered shadowed nopadding">
            <h4 class="widgettitle nomargin shadowed">Sell Product</h4>
            <form action="<?php htmlspecialchars($_SERVER["PHP_SELF"]);?>"  method="post" class="stdform stdform2" enctype="multipart/form-data" autocomplete="off">

                    <p>
                             <input type="hidden" name="employee_id" value="" id="employee_id" />
                    </p>
                <p>
                    <label for="name" class="required">Product</label>
                    <span class="field">
                        <input type="text" name="name" id="name" value="<?php echo $name;?>" /><br><span class="error"><?php echo $name_err;?></span></span>

                </p>
                <p>
                    <label for="type" class="required">Product Type</label>
                    <span class="field">
                    <select name="type" id="type" minlength="2" maxlength="60">
                            <option value="select" label="Select" <?php echo ($type == "select" || empty($type) ? "selected='selected'" : "");  ?> selected disabled >Select</option>
                            <option value="Electronics" label="Electronics" <?php echo ($type == "Electronics" ? "selected='selected'" : ""); ?>>Electronics</option>
                            <option value="Appliances" label="Appliances" <?php echo ($type == "Appliances" ? "selected='selected'" : ""); ?>>Appliances</option>
                            <option value="Baby" label="Baby" <?php echo ($type == "Baby" ? "selected='selected'" : ""); ?>>Baby</option>
                            <option value="Beauty" label="Beauty" <?php echo ($type == "Beauty" ? "selected='selected'" : ""); ?>>Beauty</option>
                            <option value="Books" label="Books" <?php echo ($type == "Books" ? "selected='selected'" : ""); ?>>Books</option>
                            <option value="Cars_Motorbikes" label="Cars &amp; Motorbikes" <?php echo ($type == "Cars_Motorbikes" ? "selected='selected'" : ""); ?>>Cars &amp; Motorbikes</option>
                            <option value="Clothing_Accessories" label="Clothing &amp; Accessories" <?php echo ($type == "Clothing_Accessories" ? "selected='selected'" : ""); ?>>Clothing &amp; Accessories</option>
                            <option value="Computers_Accessories" label="Computers &amp; Accessories" <?php echo ($type == "Computers_Accessories" ? "selected='selected'" : ""); ?>>Computers &amp; Accessories</option>
                            <option value="Furniture" label="Furniture" <?php echo ($type == "Furniture" ? "selected='selected'" : ""); ?>>Furniture</option>
                            <option value="Garden_Outdoors" label="Garden &amp; Outdoors" <?php echo ($type == "Garden_Outdoors" ? "selected='selected'" : ""); ?>>Garden &amp; Outdoors</option>
                            <option value="Home_Kitchen" label="Home &amp; Kitchen" <?php echo ($type == "Home_Kitchen" ? "selected='selected'" : ""); ?>>Home &amp; Kitchen</option>
                            <option value="Jewellery" label="Jewellery" <?php echo ($type == "Jewellery" ? "selected='selected'" : ""); ?>>Jewellery</option>
                            <option value="KindleStore" label="Kindle Store" <?php echo ($type == "KindleStore" ? "selected='selected'" : ""); ?>>Kindle Store</option>
                            <option value="Luggage_Bags" label="Luggage &amp; Bags" <?php echo ($type == "Luggage_Bags" ? "selected='selected'" : ""); ?>>Luggage &amp; Bags</option>
                            <option value="Music" label="Music" <?php echo ($type == "Music" ? "selected='selected'" : ""); ?>>Music</option >
                            <option value="OfficeProducts" label="Office Products" <?php echo ($type == "OfficeProducts" ? "selected='selected'" : ""); ?>>Office Products</option>
                            <option value="PetsSupplies" label="Pets Supplies" <?php echo ($type == "PetsSupplies" ? "selected='selected'" : ""); ?>>Pets Supplies</option>
                            <option value="Shoes_Handbags" label="Shoes &amp; Handbags" <?php echo ($type == "Shoes_Handbags" ? "selected='selected'" : ""); ?>>Shoes &amp; Handbags</option>
                            <option value="Software" label="Software" <?php echo ($type == "Software" ? "selected='selected'" : ""); ?>>Software</option>
                            <option value="SportsFitness_Outdoors" label="Sports Fitness &amp; Outdoors"  <?php echo ($type == "SportsFitness_Outdoors" ? "selected='selected'" : ""); ?>>Sports Fitness &amp; Outdoors</option>
                            <option value="Toys_Games" label="Toys &amp; Games" <?php echo ($type == "Toys_Games" ? "selected='selected'" : ""); ?>>Toys &amp; Games</option>
                            <option value="VideoGames" label="Video Games" <?php echo ($type == "VideoGames" ? "selected='selected'" : ""); ?>>Video Games</option>
                            <option value="Watches" label="Watches" <?php echo ($type == "Watches" ? "selected='selected'" : ""); ?>>Watches</option>
                        </select><br><span class="error"><?php echo $type_err;?></span></span>                </p>
                <p>
                    <label for="brand" class="optional">Product Brand</label>
                    <span class="field">
                        <input type="text" name="brand" id="brand" value="<?php echo $brand;?>" /><br><span class="error"><?php echo $brand_err;?></span></span>
                </p>
                <p>
                    <label for="model" class="required">Product Model</label>
                    <span class="field">
                    <input type="text" name="model" id="model" value="<?php echo $model;?>" /><br><span class="error"><?php echo $model_err;?></span>

                    </span>
                </p>
                <p>
                    <label for="condition" class="required">Product Condition</label>
                    <span class="field">
                    <select name="condition" id="condition" minlength="2" maxlength="60">
                            <option value="select" label="Select" selected disabled <?php echo ($condition == "select" || empty($condition) ? "selected='selected'" : "");  ?> >Select</option>
                            <option value="Likely New" label="Likely New" <?php echo ($condition == "Likely New" ? "selected='selected'" : ""); ?>>Likely New</option>
                            <option value="Very Good" label="Very Good" <?php echo ($condition == "Very Good" ? "selected='selected'" : ""); ?>>Very Good</option>
                            <option value="Good" label="Good" <?php echo ($condition == "Good" ? "selected='selected'" : ""); ?>>Good</option>
                            <option value="Acceptable" label="Acceptable" <?php echo ($condition == "Acceptable" ? "selected='selected'" : ""); ?>>Acceptable</option>
                        </select><br><span class="error"><?php echo $condition_err;?></span></span>
                </p>
                <p>
                            <label for="about" class="required">Product Description</label>
                            <span class="field">
                                <input type="text" name="about" id="about" value="<?php echo $about;?>" /><br><span class="error"><?php echo $about_err;?></span>
                            </span>
                </p>
                <p>
                    <label for="listedon" class="optional">Listed On</label>
                    <span class="field">
                    <input type="text" name="listedon" id="listedon" value="" readonly="readonly" /></span>
                </p>

                <p>
                    <label for="Purchased_price" class="required">Purchased Price (₹)</label>
                    <span class="field">
                        <input type="text" name="Purchased_price" id="Purchased_price" value="<?php echo $Purchased_price; ?>" /><br><span class="error"><?php echo $Purchased_price_err;?></span></span>
                </p>
                <p>
                    <label for="buy_now_price" class="required">Buy Now Price (₹)</label>
                    <span class="field">
                    <input type="text" name="buy_now_price" id="buy_now_price" value="<?php echo $buy_now_price; ?>" /><br><span class="error"><?php echo $buy_now_price_err;?></span></span>
                </p>
                <p>
                    <label for="reserved_price" class="required">Minimum Price (₹)</label>
                    <span class="field">
                        <input type="text" name="reserved_price" id="reserved_price" value="<?php echo $reserved_price; ?>" /><br><span class="error"><?php echo $reserved_price_err; ?></span></span>
                </p>
                <p>
                    <label for="sale_end_date" class="required">Sale End Date</label>
                    <span class="field">
                         <input type="text" name="sale_end_date" id="sale_end_date" value="<?php echo $sale_end_date; ?>" class="input-large" /><br><span class="error" ><?php echo $sale_end_date_err;?></span>
                    </span>
                </p>
                <p>
                <label>Upload a Picture of Product</label>
                <span class="field">
                    <input type="file" name="image" /><br><span class="error"><?php echo $file_err; ?></span></span></p>

                <p class="stdformbutton">
                    <input type="submit" class="bluishBtn button_small" name="submit" value="SAVE" id="submit">

                    <button name="cancel" id="cancel" type="button" class="greyishBtn button_small">Cancel</button>                </p>



            </form>
        </div>
    </div>
</div>



<script type="text/javascript">
    $(document).ready(function(){
    });
    $('#listedon').val($.datepicker.formatDate( "yy-mm-dd", new Date()))
    $("#sale_end_date").datepicker( {minDate:0, "dateFormat": 'yy-mm-dd', changeMonth: true, changeYear: true, firstDay: 1 });



        $('#cancel').click(function() {
            location.href="/sellproduct/index";
        });
        $('#listedon').val($.datepicker.formatDate( "yy-mm-dd", new Date()))
        $("#sale_end_date").datepicker( {minDate:0, "dateFormat": 'yy-mm-dd', changeMonth: true, changeYear: true, firstDay: 1 });

    }); 
</script>

任何人都可以告诉这段代码中的错误是什么?为什么它不存储图像。

1 个答案:

答案 0 :(得分:0)

更新您的代码表单:

$con = mysqli_connect("localhost", "root", "", "csr");
 if(isset($_POST['submit']))
 {
 if (empty($_FILES['image']['name'])) {
            $file_err = "Please upload the Product Picture";
            //$e = false;
        } else {
            $ImageName = $_FILES['image']['name'];
            $fileElementName = 'image';
            $path = 'images/';
            $location = $path.$_FILES['image']['name'];
            move_uploaded_file($_FILES['image']['tmp_name'], $location);
            mysqli_query($con, "insert into products(image_path) values('$location')") or  die('Could not connect: ' . mysqli_error($con));
            header('Location:/sellproduct/index/image');
            mysqli_close($con);

        }

}