PHP işlemini form aracılığıyla yapmak

MKELORDS

Hectopat
Katılım
2 Nisan 2019
Mesajlar
4
Daha fazla  
Cinsiyet
Erkek
Selam arkadaşlar, aşağıdaki kod her F5 işleminde görevini yerine getirip veritabanına işlemi gerçekleştiriyor, ben bunu sadece form aracılığıyla yapmak istiyorum ama sürekli bir hata almaktayım, form ile işlem yaptırmasını sağlayabilmek adına yardımcı olabilecek var mıdır?

Özetle; ID ürün adı ve stok durumunu belirten bir veri tabanı sütunları var listeden ID'si 1 olana tıklayıp modal açılsın ve stok eklesin veya düşsün istiyorum. Yardımcı olacak arkadaşlara şimdiden teşekkür ederim :)

PHP:
<?php
include('veritabani.php');
?>
<?php

// Güncellenecek ürün ve miktar.
$urun_id = 1; // Güncellenecek ürünün ID'si.
$artan_stok = 2; // Eklenecek stok miktarı.

// Mevcut stok miktarını al.
$sqlMevcutStok = "SELECT * FROM stok WHERE id = :urun_id";
$stmt = $vtbaglanti->prepare($sqlMevcutStok);
$stmt->bindParam(':urun_id', $urun_id);
$stmt->execute();

$row = $stmt->fetch(PDO::FETCH_ASSOC);
$mevcut_stok = $row["urunstok"];
$urunadi = $row["urunadi"];

// Eski stok miktarıyla yeni stok miktarını topla.
$yeni_stok_miktari = $mevcut_stok + $artan_stok;

// Veritabanında stok miktarını güncelleme sorgusu.
$sqlGuncelleStok = "UPDATE stok SET urunstok = :yeni_stok_miktari WHERE id = :urun_id";
$stmt = $vtbaglanti->prepare($sqlGuncelleStok);
$stmt->bindParam(':yeni_stok_miktari', $yeni_stok_miktari);
$stmt->bindParam(':urun_id', $urun_id);

// Sorguyu çalıştırma.
$stmt->execute();

echo "Stok güncellendi! Yeni stok miktarı: $yeni_stok_miktari";

?>
 
Son düzenleyen: Moderatör:
Hoşgeldin :)

Bu içeriği görüntülemek için üçüncü taraf çerezlerini yerleştirmek için izninize ihtiyacımız olacak.
Daha detaylı bilgi için, çerezler sayfamıza bakınız.


PHP:
<?php
include('veritabani.php');
?>
<!doctype html>
<html lang="tr">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

    <title>Stok Düzenleme</title>
  </head>
  <body>
   
    <div class="container">
        <table class="table table-hover">
          <thead>
            <tr>
              <th scope="col">#</th>
              <th scope="col">Stok Adı</th>
              <th scope="col">Stok Adet</th>
              <th scope="col">İşlem</th>
            </tr>
          </thead>
          <tbody>
            <?php
              $query = $vtbaglanti->query("SELECT * FROM stok");
              $result = $query->fetchAll(PDO::FETCH_ASSOC);

              foreach ($result as $row) {
            ?>
            <tr>
              <th scope="row"><?=$row['id']?></th>
              <td><?=$row['code']?></td>
              <td><?=$row['piece']?></td>
              <td><button type="button" data-id="<?=$row['id']?>" class="btn btn-success editBtn" data-toggle="modal" data-target="#exampleModal">+ Stok Ekle</button></td>
             
            </tr>
          <?php } ?>

          </tbody>
        </table>
   



<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Ürün Stok Ekle</h5>
        <button type="button" class="close" id="closeModalBtn" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <p><span id="modalStokID"></span> nolu ürünün stoğu güncellenecek!</p>
        <form action="" method="POST">
          <div class="form-group">
            <label for="x">Eklenecek Stok Miktarı</label>
            <input type="hidden" name="id" id="id" value="">
            <input type="number" class="form-control" id="x" placeholder="Adet giriniz" name="stok">
          </div>
     
   
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Vazgeç</button>
        <input type="submit" class="btn btn-primary" name="ekle" value="Ekle">
      </div>
      </form>
    </div>
  </div>
</div>


<?php
if(@$_POST['ekle']) {

$urun_id = $_POST['id'];
$artan_stok = $_POST['stok'];

$sqlMevcutStok = "SELECT * FROM stok WHERE id = :urun_id";
$stmt = $vtbaglanti->prepare($sqlMevcutStok);
$stmt->bindParam(':urun_id', $urun_id);
$stmt->execute();

$row = $stmt->fetch(PDO::FETCH_ASSOC);
$mevcut_stok = $row["urunstok"];
$urunadi = $row["urunadi"];

$yeni_stok_miktari = $mevcut_stok + $artan_stok;

$sqlGuncelleStok = "UPDATE stok SET urunstok = :yeni_stok_miktari WHERE id = :urun_id";
$stmt = $vtbaglanti->prepare($sqlGuncelleStok);
$stmt->bindParam(':yeni_stok_miktari', $yeni_stok_miktari);
$stmt->bindParam(':urun_id', $urun_id);
$stmt->execute();

echo '<div class="alert alert-success" role="alert">Stok güncellendi! Yeni stok miktarı:'.$yeni_stok_miktari.'</div>';
header("Refresh:1");
}
?>

</div>

<script src="script.js"></script>
    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
  </body>
</html>

script.js
JavaScript:
document.querySelectorAll('.editBtn').forEach(function(editBtn) {
  editBtn.addEventListener('click', function() {
    var stokID = this.getAttribute('data-id');
    openEditModal(stokID);
  });
});

function openEditModal(stokID) {
  document.getElementById('modalStokID').textContent = stokID;
  $("#id").val(stokID);
  document.getElementById('exampleModal').style.display = 'block';
}

document.getElementById('closeModalBtn').addEventListener('click', function() {
  document.getElementById('exampleModal').style.display = 'none';
});
 
Son düzenleme:
Hoşgeldin :)

Bu içeriği görüntülemek için üçüncü taraf çerezlerini yerleştirmek için izninize ihtiyacımız olacak.
Daha detaylı bilgi için, çerezler sayfamıza bakınız.


PHP:
<?php
include('veritabani.php');
?>
<!doctype html>
<html lang="tr">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

    <title>Stok Düzenleme</title>
  </head>
  <body>
 
    <div class="container">
        <table class="table table-hover">
          <thead>
            <tr>
              <th scope="col">#</th>
              <th scope="col">Stok Adı</th>
              <th scope="col">Stok Adet</th>
              <th scope="col">İşlem</th>
            </tr>
          </thead>
          <tbody>
            <?php
              $query = $vtbaglanti->query("SELECT * FROM stok");
              $result = $query->fetchAll(PDO::FETCH_ASSOC);

              foreach ($result as $row) {
            ?>
            <tr>
              <th scope="row"><?=$row['id']?></th>
              <td><?=$row['code']?></td>
              <td><?=$row['piece']?></td>
              <td><button type="button" data-id="<?=$row['id']?>" class="btn btn-success editBtn" data-toggle="modal" data-target="#exampleModal">+ Stok Ekle</button></td>
           
            </tr>
          <?php } ?>

          </tbody>
        </table>
 



<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Ürün Stok Ekle</h5>
        <button type="button" class="close" id="closeModalBtn" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <p><span id="modalStokID"></span> nolu ürünün stoğu güncellenecek!</p>
        <form action="" method="POST">
          <div class="form-group">
            <label for="x">Eklenecek Stok Miktarı</label>
            <input type="hidden" name="id" id="id" value="">
            <input type="number" class="form-control" id="x" placeholder="Adet giriniz" name="stok">
          </div>
   
 
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Vazgeç</button>
        <input type="submit" class="btn btn-primary" name="ekle" value="Ekle">
      </div>
      </form>
    </div>
  </div>
</div>


<?php
if(@$_POST['ekle']) {

$urun_id = $_POST['id'];
$artan_stok = $_POST['stok'];

$sqlMevcutStok = "SELECT * FROM stok WHERE id = :urun_id";
$stmt = $vtbaglanti->prepare($sqlMevcutStok);
$stmt->bindParam(':urun_id', $urun_id);
$stmt->execute();

$row = $stmt->fetch(PDO::FETCH_ASSOC);
$mevcut_stok = $row["urunstok"];
$urunadi = $row["urunadi"];

$yeni_stok_miktari = $mevcut_stok + $artan_stok;

$sqlGuncelleStok = "UPDATE stok SET urunstok = :yeni_stok_miktari WHERE id = :urun_id";
$stmt = $vtbaglanti->prepare($sqlGuncelleStok);
$stmt->bindParam(':yeni_stok_miktari', $yeni_stok_miktari);
$stmt->bindParam(':urun_id', $urun_id);
$stmt->execute();

echo '<div class="alert alert-success" role="alert">Stok güncellendi! Yeni stok miktarı:'.$yeni_stok_miktari.'</div>';
header("Refresh:1");
}
?>

</div>

<script src="script.js"></script>
    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
  </body>
</html>

script.js
JavaScript:
document.querySelectorAll('.editBtn').forEach(function(editBtn) {
  editBtn.addEventListener('click', function() {
    var stokID = this.getAttribute('data-id');
    openEditModal(stokID);
  });
});

function openEditModal(stokID) {
  document.getElementById('modalStokID').textContent = stokID;
  $("#id").val(stokID);
  document.getElementById('exampleModal').style.display = 'block';
}

document.getElementById('closeModalBtn').addEventListener('click', function() {
  document.getElementById('exampleModal').style.display = 'none';
});


Vay, hoşbuldum hocam. :) Dünden beri uğraşıyorum seni rahatsız etmek istemedim tekrar tekrar, denemediğim yöntem kalmadı son olarak aşağıdaki kodları denedim ama onda da garip bi' hata aldım çözemedim bir türlü. Datatable'den dolayı sorun yaşadığımı düşündümi basic table kullandım yine sonuc hüsran :D

PHP:
 <table id="datatable-buttons" class="table table-hover table-bordered dt-responsive nowrap" style="border-collapse: collapse; border-spacing: 0; width: 100%;">
                                        <thead>
                                        <tr>
                                                    <th>#</th>
                                                    <th><i class="mdi mdi-format-list-bulleted-type"></i> Görsel</th>
                                                    <th><i class="mdi mdi-artstation"></i> Ürün Adı</th>
                                                    <th><i class="mdi mdi-puzzle"></i> Stok Durumu</th>
                                                    <th><i class="fas fa-cogs"></i> İşlem</th>
                                        </tr>
                                        </thead>


                                        <tbody>
                            <?php

                             $stoktakip = $vtbaglanti->prepare("SELECT * FROM stok");
                              $stoktakip->execute();
               
                             while ($cek = $stoktakip->fetch(PDO::FETCH_OBJ)) {?>
                       
                    <tr>
                            <th scope="row"><?=$cek->id?></th>
                            <td><center><img width="25" src="assets/images/stokgorsel/<?=$cek->urungorsel?>" class="foto-zoom" alt=""></center></td>
                            <td><?=$cek->urunadi?></td>
                            <td><?php

                                                $urunstok = $cek->urunstok;

                                                if (empty($urunstok) || $urunstok == 0) {

                                                    echo "<center><button class='btn btn-outline-danger btn-xs'>Mevcut Stok Yok!</button></center>";

                                                } else {

                                                    echo "<center><button class='btn btn-outline-success btn-xs'>$urunstok Adet Stok Mevcut</button></center>";

                                                }

                                                ?></td>
                            <td><a href="stok-duzenle.php?id=<?=$cek->id?>"><button type="button" class="btn btn-outline-info btn-xs" data-toggle="tooltip" data-placement="top" title="Düzenle"><i class="fas fa-pencil-alt"></i></button></a>
                                <a href="stok-sil.php?id=<?=$cek->id?>" onclick="return confirm('Kayıt kalıcı olarak silinecek, emin misiniz?');">
                                <button type="button" class="btn btn-outline-danger btn-xs" data-toggle="tooltip" data-placement="top" title="Sil"><i class="fas fa-trash-alt"></i> </button></a></td>
                                <td>
    <button type="button" class="btn btn-outline-primary btn-xs" data-toggle="modal" data-target="#stokEkleModal<?=$cek->id?>">
        <i class="fas fa-plus"></i> Stok Ekle
    </button>

    <!-- Stok Ekleme Modalı -->
    <div class="modal fade" id="stokEkleModal<?=$cek->id?>" tabindex="-1" role="dialog" aria-labelledby="stokEkleModalLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="stokEkleModalLabel">Stok Ekleme Formu</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <!-- Stok Ekleme Formu -->
                    <form id="stokEkleForm<?=$cek->id?>">
                        <div class="form-group">
                            <label for="ekleme_miktari">Eklenecek Stok Miktarı:</label>
                            <input type="text" class="form-control" id="ekleme_miktari" required>
                        </div>
                        <button type="button" class="btn btn-primary" onclick="stokEkle(<?=$cek->id?>)">Stok Ekle</button>
                    </form>
                </div>
            </div>
        </div>
    </div>
</td>
                    </tr>
                 
                     <?php } ?>
                                        </tbody>
                                    </table>

JavaScript:
<script>
    function stokGuncelle() {
        var urun_id = $("#urun_id").val();
        var artan_stok = $("#artan_stok").val();

        $.ajax({
            type: "POST",
            url: "stok_guncelle.php",
            data: { urun_id: urun_id, artan_stok: artan_stok },
            success: function(data) {
                // SweetAlert ile bildirim gösterme
                Swal.fire({
                    title: 'Başarılı!',
                    text: 'Stok eklendi, ' + data,
                    icon: 'success',
                    showCancelButton: false,
                    confirmButtonColor: '#3085d6',
                    confirmButtonText: 'Tamam',
                    }).then(function() {
                    window.location = "stok-tables.php";
                });
               
                // Modalı kapat
                $('#stokGuncelleModal').modal('hide');
            },
            error: function(error) {
                console.log(error);
                alert("Bir hata oluştu. Lütfen tekrar deneyin.");
            }
        });
    }
</script>
 
Son düzenleyen: Moderatör:
Akşam biraz CS2 ile kafa dağıtıp sonrasında biraz da buna kafa patlatacağım, artık bugün çok karıştı kafam :D
 
Son düzenleyen: Moderatör:

Technopat Haberler

Yeni konular

Geri
Yukarı