product aanmaken met meerdere fotos

Overzicht Reageren

Sponsored by: Vacatures door Monsterboard

Jens waelkens

jens waelkens

16/12/2019 15:56:30
Quote Anchor link
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
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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();

        }
    }
}

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
    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   ;
        }
    }
Gewijzigd op 16/12/2019 17:40:36 door Jens waelkens
 
PHP hulp

PHP hulp

28/02/2020 20:17:04
 
- Ariën -
Beheerder

- Ariën -

16/12/2019 16:29:36
Quote Anchor link
Kan je jouw code tussen code tags plaatsen, zoals wordt uitgelegd in de Veelgestelde Vragen bij de opmaakcodes? Dan is de boel beter leesbaarder.

Lees ook eens: https://www.php.net/manual/en/features.file-upload.multiple.php
Let wel op de veiligheid, en zorg voor goede foutafhandeling.
 
Jens waelkens

jens waelkens

16/12/2019 17:41:03
Quote Anchor link
ok excuses alvast en thx voor de link bekijk dit zo even
Gewijzigd op 16/12/2019 17:53:56 door - Ariën -
 



Overzicht Reageren

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.