Php Veritabanı Kayıt Silme

İyi günler bu yazımda Php ile veritabanından kayıt silmeyi yapacağız.Senaryomuz şu şekilde olacak tabloya veritabanında verileri listeleyeceğiz ve satıra sil butonu ekleyeceğiz.Sil butonuna tıklayınca tıklanan satırdaki id değerini alıp veritabanından sileceğiz.

Veritabanı ekleme ve listeme nasıl yapılır öğrenmek için aşağıdaki yazıma bakabilirsiniz.

Php Mysql Kayıt Ekleme ve Listeleme

Bu örneğimizi yaparken Php’nin yanı sıra Javascript’den de faydalanacağız.Butona tıklama ve tıklanan id alma işini Javascript ile yapacağız.

Tabloya veriler çekme işlemini farklı bir dersimde anlattığım için orasını geçiyorum.Direk olarak veri silme işini anlatacağım.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<table class="table">
<thead>
<tr>
<th>
Ad
</th>
<th>
Soyad
</th>
</tr>
</thead>
<tbody>
<?php
$con=mysqli_connect("localhost","dbKullanici","dbSifre","dbAdi") or die("Hata");
mysqli_set_charset($con,"utf8");
$sql="select * from bilgiler";
$res=mysqli_query($con,$sql);
while ($row=mysqli_fetch_assoc($res)) {
echo "<tr><td>".$row["ad"]."</td>";
echo "<td>".$row["soyad"]."</td>";
echo "<td onclick='veriSil(".$row["id"].")'><button class='btn btn-danger'>Sil</button></td></tr>";
}
mysqli_close($con);
?>
</tbody>
</table>
<table class="table"> <thead> <tr> <th> Ad </th> <th> Soyad </th> </tr> </thead> <tbody> <?php $con=mysqli_connect("localhost","dbKullanici","dbSifre","dbAdi") or die("Hata"); mysqli_set_charset($con,"utf8"); $sql="select * from bilgiler"; $res=mysqli_query($con,$sql); while ($row=mysqli_fetch_assoc($res)) { echo "<tr><td>".$row["ad"]."</td>"; echo "<td>".$row["soyad"]."</td>"; echo "<td onclick='veriSil(".$row["id"].")'><button class='btn btn-danger'>Sil</button></td></tr>"; } mysqli_close($con); ?> </tbody> </table>
    <table class="table">
        <thead>
        <tr>
            <th>
                Ad
            </th>
            <th>
                Soyad
            </th>
        </tr>
        </thead>
        <tbody>
        <?php
$con=mysqli_connect("localhost","dbKullanici","dbSifre","dbAdi") or die("Hata");
        mysqli_set_charset($con,"utf8");
        $sql="select * from bilgiler";
        $res=mysqli_query($con,$sql);

        while ($row=mysqli_fetch_assoc($res)) {
            echo "<tr><td>".$row["ad"]."</td>";
            echo "<td>".$row["soyad"]."</td>";
            echo "<td onclick='veriSil(".$row["id"].")'><button class='btn btn-danger'>Sil</button></td></tr>";
        }
        mysqli_close($con);

        ?>
        </tbody>
    </table>

Tabloya bu şekilde verileri alıyoruz.Burda mysqli_connect fonksiyonun parametlerini kendi bilgilerinize göre değiştiriniz.Burda önceki anlattığımız dersten farklı olarak yeni bir satır ekledim ve o satıra da bir sil butonu ekledim.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
echo "<td onclick='veriSil(".$row["id"].")'><button class='btn btn-danger'>Sil</button></td></tr>";
echo "<td onclick='veriSil(".$row["id"].")'><button class='btn btn-danger'>Sil</button></td></tr>";
echo "<td onclick='veriSil(".$row["id"].")'><button class='btn btn-danger'>Sil</button></td></tr>";

Bu satıra ise Javascript ile tıklanıp tıklanmadığını kontrol etmek için veriSil adında bir fonksiyon ekledim.Bu fonksiyon parametre olarak silmek istediğimiz satırın id değerini alıyor.

veriSil fonksiyonunu script etiketleri içine ekliyoruz.Fonksiyonumuz bir adet parametre aldığı için parantez içinde parametre değişkenimizi yazıyoruz.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<script>
function veriSil(id)
{
}
</script>
<script> function veriSil(id) { } </script>
<script> 
function veriSil(id)
 { 
 } 
</script>

Burdan sonra yapacağımız şey veriSil fonksiyonu içinde ajax ile bir Php sayfasına istek göndermek olacaktır.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$.ajax({
url:"sil.php",
method:"POST"
data:{"id":id},
success:function (response) {
alert(response);
window.location.reload(true);
},
error:function (response) {
alert(response);
}
});
$.ajax({ url:"sil.php", method:"POST" data:{"id":id}, success:function (response) { alert(response); window.location.reload(true); }, error:function (response) { alert(response); } });
            $.ajax({
                url:"sil.php",
                method:"POST"
                data:{"id":id},
                success:function (response) {
                    alert(response);
                    window.location.reload(true);
                },
                error:function (response) {
                    alert(response);
                }
            });

Url kısmına verileri göndereceğimiz sayfayı yazıyoruz.

Data kısmına bir array yazıyoruz ve verileri bu array ile gönderiyoruz.

Method kısmında ise verileri Get veya Post mu ile göndereceğiz onları belirtiyoruz.Put,Delete gibi metodlarda kullanabilirsiniz.

Success ve Error ise bize geri dönüşleri sağlıyor.Başarılı olduğunda ve hata verdiğinde bu fonksiyonlardan bize bir cevap dönüyor.Bizde bu cevaba göre kullanıcıyı bilgilendiriyoruz.

Şimdi de sil.php sayfamızı hazırlayalım.Bu sayfada ise delete sql cümlesini yazıyoruz ve sorguyu çalıştırıyoruz.Burda dikkat edilmesi gereken nokta veriyi ne ile gönderdiysek onunla alıyoruz.Bu örnekte POST ile gönderdim Php sayfasında da POST ile veriyi alıyorum.

Kamu spotu : Ayrıca delete sorgusunda where şartını kesinlikle unutmamak gerekiyor.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
$con=mysqli_connect("localhost","dbKullanici","dbSifre","dbAdi") or die("Hata");
$id=$_POST["id"];
$sql="delete from bilgiler where id='$id'";
$res=mysqli_query($con,$sql);
if ($res)
echo "Veri Silindi.";
else
echo "Veri Silinemedi.";
mysqli_close($con);
?>
<?php $con=mysqli_connect("localhost","dbKullanici","dbSifre","dbAdi") or die("Hata"); $id=$_POST["id"]; $sql="delete from bilgiler where id='$id'"; $res=mysqli_query($con,$sql); if ($res) echo "Veri Silindi."; else echo "Veri Silinemedi."; mysqli_close($con); ?>
<?php
$con=mysqli_connect("localhost","dbKullanici","dbSifre","dbAdi") or die("Hata");

$id=$_POST["id"];

$sql="delete from bilgiler where id='$id'";
$res=mysqli_query($con,$sql);

if ($res)
    echo "Veri Silindi.";
else
    echo "Veri Silinemedi.";
mysqli_close($con);

?>

Kodların Tamamı

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<html>
<head>
<meta charset="utf-8">
<title></title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.6/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js"></script>
<script>
function veriSil(id) {
$.ajax({
url:"sil.php",
method:"POST",
data:{"id":id},
success:function (response) {
alert(response);
window.location.reload(true);
},
error:function (response) {
alert(response);
}
});
}
</script>
</head>
<body>
<div class="container">
<table class="table">
<thead>
<tr>
<th>
Ad
</th>
<th>
Soyad
</th>
</tr>
</thead>
<tbody>
<?php
$con=mysqli_connect("localhost","dbKullanici","dbSifre","dbAdi") or die("Hata");
mysqli_set_charset($con,"utf8");
$sql="select * from bilgiler";
$res=mysqli_query($con,$sql);
while ($row=mysqli_fetch_assoc($res)) {
echo "<tr><td>".$row["ad"]."</td>";
echo "<td>".$row["soyad"]."</td>";
echo "<td onclick='veriSil(".$row["id"].")'><button class='btn btn-danger'>Sil</button></td></tr>";
}
mysqli_close($con);
?>
</tbody>
</table>
</div>
</body>
</html>
<html> <head> <meta charset="utf-8"> <title></title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.6/umd/popper.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js"></script> <script> function veriSil(id) { $.ajax({ url:"sil.php", method:"POST", data:{"id":id}, success:function (response) { alert(response); window.location.reload(true); }, error:function (response) { alert(response); } }); } </script> </head> <body> <div class="container"> <table class="table"> <thead> <tr> <th> Ad </th> <th> Soyad </th> </tr> </thead> <tbody> <?php $con=mysqli_connect("localhost","dbKullanici","dbSifre","dbAdi") or die("Hata"); mysqli_set_charset($con,"utf8"); $sql="select * from bilgiler"; $res=mysqli_query($con,$sql); while ($row=mysqli_fetch_assoc($res)) { echo "<tr><td>".$row["ad"]."</td>"; echo "<td>".$row["soyad"]."</td>"; echo "<td onclick='veriSil(".$row["id"].")'><button class='btn btn-danger'>Sil</button></td></tr>"; } mysqli_close($con); ?> </tbody> </table> </div> </body> </html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.6/umd/popper.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js"></script>

    <script>
        function veriSil(id) {
            $.ajax({
                url:"sil.php",
                method:"POST",
                data:{"id":id},
                success:function (response) {
                    alert(response);
                    window.location.reload(true);
                },
                error:function (response) {
                    alert(response);
                }
            });
        }
    </script>
</head>
<body>
<div class="container">
    <table class="table">
        <thead>
        <tr>
            <th>
                Ad
            </th>
            <th>
                Soyad
            </th>
        </tr>
        </thead>
        <tbody>
        <?php
$con=mysqli_connect("localhost","dbKullanici","dbSifre","dbAdi") or die("Hata");
        mysqli_set_charset($con,"utf8");
        $sql="select * from bilgiler";
        $res=mysqli_query($con,$sql);

        while ($row=mysqli_fetch_assoc($res)) {
            echo "<tr><td>".$row["ad"]."</td>";
            echo "<td>".$row["soyad"]."</td>";
            echo "<td onclick='veriSil(".$row["id"].")'><button class='btn btn-danger'>Sil</button></td></tr>";
        }
        mysqli_close($con);

        ?>
        </tbody>
    </table>
</div>
</body>
</html>

sil.php

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
$con=mysqli_connect("localhost","dbKullanici","dbSifre","dbAdi") or die("Hata");
$id=$_POST["id"];
$sql="delete from bilgiler where id='$id'";
$res=mysqli_query($con,$sql);
if ($res)
echo "Veri Silindi.";
else
echo "Veri Silinemedi.";
mysqli_close($con);
?>
<?php $con=mysqli_connect("localhost","dbKullanici","dbSifre","dbAdi") or die("Hata"); $id=$_POST["id"]; $sql="delete from bilgiler where id='$id'"; $res=mysqli_query($con,$sql); if ($res) echo "Veri Silindi."; else echo "Veri Silinemedi."; mysqli_close($con); ?>
<?php
$con=mysqli_connect("localhost","dbKullanici","dbSifre","dbAdi") or die("Hata");

$id=$_POST["id"];

$sql="delete from bilgiler where id='$id'";
$res=mysqli_query($con,$sql);

if ($res)
    echo "Veri Silindi.";
else
    echo "Veri Silinemedi.";
mysqli_close($con);

?>

Kodları kendinize göre düzenleyebilir veya geliştirebilirsiniz.Farklı senaryolar ile projelerinizde de kullanabilirsiniz.

 

 

Yukarı Çık
Size nasıl yardımcı olabilirim ?