Ürün ve görsel ekleme

mail@pastecode.io avatar
unknown
php
3 years ago
4.6 kB
14
Indexable
Never
<?php 
    if(isset($_POST['submit'])){

        if($_FILES){
            $maxBoyut       = 700000;
            $dosyaUzantisi  = substr($_FILES["p_img"]["name"],-4,4);
            $dosyaAdi       = rand(1,99999).$dosyaUzantisi;
            $dosyaYolu      = public_url("/uploads/product/".$dosyaAdi);
            
            
            if($_FILES["p_img"]["size"]>$maxBoyut){
                  
                echo "<h2>dosya boyutu 700kb'dan yuksek olamaz...</h2>";
                  
            }else {
                $dosya = $_FILES["p_img"]["type"];
                  
                if($dosya == "image/jpeg" || $dosya == "image/png"){
                  
                  
                    if(is_uploaded_file($_FILES["p_img"]["tmp_name"])){
                      
                      
                      $tasi = move_uploaded_file($_FILES["p_img"]["tmp_name"],$dosyaYolu);
                      
                      $resimTuru = $_FILES["p_img"]["type"];
                      $resimSize = $_FILES["p_img"]["size"];  
                    }    
                }  
            }
        }

        $p_name = isset($_POST['p_name']) ? $_POST['p_name'] : null;
        $p_img = isset($_POST['p_img']) ? $_POST['p_img'] : null;
        $p_barcode = isset($_POST['p_barcode']) ? $_POST['p_barcode'] : null;
        $p_price = isset($_POST['p_price']) ? $_POST['p_price'] : null;
        $p_stock = isset($_POST['p_stock']) ? $_POST['p_stock'] : null;

        $query = $db->prepare('INSERT INTO product SET
            p_name = ?,
            p_img = ?,
            p_barcode = ?,
            p_price = ?,
            p_stock = ?
        ');

        $result = $query->execute([
            $p_name, $p_img, $p_barcode, $p_price, $p_stock
        ]);

        if($result) {
            echo 'Ürün başarıyla eklendi.';
        } else {
            echo 'Ürün eklenemedi.';
        }
    }

?>

<div class="product-list w-1/2">
    <div class="product-list-area">
        <form action="" method="post" ectype="multipart/form-data">
            <div class="input-group flex flex-wrap">
                <div class="input-element w-full">
                    <label class="block" for="task-name">Ürün Görseli Ekle</label>
                    <div class="file-input w-full">
                        <label class="block">
                            <input type="file" id="file" name="p_img">
                            <div class="file-input-box flex flex-col items-center justify-center">
                                <svg viewBox="0 0 24 24" stroke="currentColor" stroke-width="1" fill="none" stroke-linecap="round" stroke-linejoin="round">
                                    <rect x="3" y="3" width="18" height="18" rx="2" ry="2"></rect>
                                    <circle cx="8.5" cy="8.5" r="1.5"></circle>
                                    <polyline points="21 15 16 10 5 21"></polyline>
                                </svg>
                                <span class="block"><strong>Dosya ve Görsel yüklemek için </strong>tıklayın veya sürükleyin</span>
                                <p>DOC, XLS, PNG, JPG maks. 10MB</p>
                            </div>
                        </label>
                    </div>
                </div>
                <div class="input-element w-1/2">
                    <label class="block" for="p_name">Ürün Adı</label>
                    <input type="text" name="p_name" placeholder="">
                </div>
                <div class="input-element w-1/2">
                    <label class="block" for="p_barcode">Ürün Barkod</label>
                    <input type="text" name="p_barcode" placeholder="">
                </div>
                <div class="input-element w-1/2">
                    <label class="block" for="p_price">Ürün Fiyatı (Türk Lirası Olarak Giriniz)</label>
                    <input type="text" name="p_price" placeholder="">
                </div>
                <div class="input-element w-1/2">
                    <label class="block" for="p_stock">Ürün Stok</label>
                    <input type="text" name="p_stock" placeholder="">
                </div>
                <div class="input-element flex justify-end w-full">
                    <input type="hidden" name="submit" value="1">
                    <button type="submit" class="focus:outline-none">Ürün Ekle</button>
                </div>
            </div>
        </form>
    </div>
</div>