ik ben bezig met cursus php oop voor mijn eindwerk moet ik een webshop maken ik wil nu een product aanmaken en tegelijk meerdere fotos toevoegen iemand idee wat ik mis doe?

addproduct.php

if (isset($_POST['submit'])) {
    if ($product) {
        $product->product_name = $_POST['product_name'];
        $product->caption = $_POST['caption'];
        $product->description = $_POST['description'];
        $product->prijs = $_POST['prijs'];
        $product->category_id = $_POST['category_id'];
        $product->EAN = $_POST['EAN'];
        $product->release_date = $_POST['release_date'];
        $product->publisher = $_POST['publisher'];
        $product->dev = $_POST['developer'];

        $product->save();


        $image_array = ($_FILES['images']);


        for ($i=0;$i<count($image_array);$i++){
            $new_image->image_name = $_FILES['images']['name'[$i]];
            $new_image->type = $_FILES['type'[$i]];
            $new_image->tmp_name = $_FILES['tmp_name'[$i]];
            $new_image->size = $_FILES['size'[$i]];


            //create new product_image


            $new_image->set_file_product($_FILES['images']);
            $new_image->save_product_and_image();

        }
    }
}



form

       <form method="POST" data-parsley-validate novalidate enctype="multipart/form-data">
                            <div class="form-group">
                                <label for="product_name">Product Name*</label>
                                <input type="text" name="product_name" parsley-trigger="change" required placeholder="Enter product name" class="form-control" >
                            </div>
                            <div class="form-group">
                                <label for="caption">Caption*</label>
                                <input type="text" name="caption" parsley-trigger="change" required placeholder="Enter caption" class="form-control" >
                            </div>
                            <div class="form-group">
                                <label for="description">description*</label>
                                <input type="text" name="description" parsley-trigger="change" required placeholder="description" class="form-control" >
                            </div>
                            <div class="form-group">
                                <label for="prijs">prijs*</label>
                                <input type="text" name="prijs" parsley-trigger="change" required placeholder="Enter prijs" class="form-control" >
                            </div>


                            <h5 class="m-t-30"><b>Enter category*</b></h5>













                            <select class="select2 select2-multiple" name="category_id" multiple="multiple" multiple data-placeholder="Choose ...">
                                <optgroup label="categories">
                                <?php $categories = Categorie::find_all();
                                        foreach ($categories as $category) :                ?>
                                    <option value="<?= $category->category_id; ?>"><?= $category->category_name; ?></option>

                                    <?php endforeach;  ?>
                                </optgroup>

                            </select>

                            <div class="form-group">
                                <label for="EAN">EAN</label>
                                <input type="text" name="EAN" class="form-control">
                            </div>
                            <div class="form-group">
                                <label for="release_date">release_date</label>
                                <input type="date" name="release_date" class="form-control">
                            </div>
                            <div class="form-group">
                                <label for="publisher">Publisher</label>
                                <input type="text" name="publisher" class="form-control">
                            </div>
                            <div class="form-group">
                                <label for="developer">developer</label>
                                <input type="text" name="developer" class="form-control">
                            </div>
                            <div class="form-group">
                                <label for="images[]">add images</label>
                                <input type="file" name="images[]" multiple class="form-control">
                            </div>

                            <div class="form-group text-right m-b-0">
                                <input type="submit" name="submit" value="Add Product" class="btn btn-primary">

                            </div>


                        </form>



functies


    public function set_file_product($file){
        if(empty($file) || !$file || !is_array($file)){
            $this->errors[]= "No file uploaded";
            return false;
        }elseif($file['error'] !=0){
            $this->errors[] = $this->upload_errors_array[$file['error']];
            return false;
        }else{
            $this->product_image = basename($file['name']);
            $this->tmp_path = $file['tmp_name'];
            $this->type = $file['type'];
            $this->size = $file['size'];


        }
    }


    public function save_product_and_image(){



            if($this->id){
                move_uploaded_file($this->tmp_path);
                $this->update();
                unset($this->tmp_path);
                return true;
            }else {
                if (!empty($this->errors)) {
                    return false;
                }
                if (empty($this->product_image) || empty($this->tmp_path)) {
                    $this->errors[] = "File not available";
                    return false;
                }

                $target_path = SITE_ROOT . DS . "admin" . DS . $this->upload_directory . DS . $this->product_image;
                if (file_exists($target_path)) {
                    $this->update();
                    return true;
                }
                if (move_uploaded_file($this->tmp_path, $target_path)) {

                    if ($this->create()) {

                        unset($this->tmp_path);
                        return true;
                    }
                } else {
                    $this->errors[] = "This folder has no write rights";
                    return false;
                }

            }
    }



    public function create(){
        global $database;
        $properties = $this->clean_properties();

        $sql = " INSERT INTO " .static::$db_table . "(" .implode(",", array_keys($properties)) . ")";
        $sql .= "VALUES('" . implode("','", array_values($properties))   . "')";

        if($database->query($sql)){
            $this->id = $database->the_insert_id();

            return true;
        }else{
            return false   ;
        }
    }

ok excuses alvast en thx voor de link bekijk dit zo even

Reageren