Bu yazıda HTML radio ve checkbox nesnelerini veritabanı kullanarak güncellemeyi göstereceğiz. Bu uygulama küçük bir örneği içerecektir. Youtube hesabındaki PHP Veritabanı İşlemleri (Select, Insert, Update, Delete) konusuna sorulan soru üzerine hazırlanmıştır.
Uygulamada cinsiyete ait iki radio buton ve hobileri içeren checkbox nesneleri mevcuttur. Bu nesnelerden radio butonlarından bir tanesi seçimi sağlanmıp, checkbox nesnelerinden istediğimiz kadar seçmek imkanı verilmiştir.(Not: checkbox nesneleri için hazırlanan tabloda normalizasyon göz ardı edilmiştir.)
NOT: uygulamada GET yöntemi ile gelen ve id=1 olan kayıt güncellenmiştir. Sorgu test etmek isterseniz. URL adresine şuna benzer bir yol yazmanı gerekir. http://localhost/klasor/index.php?id=1
Uygulama ekran görüntüsü:

baglan.php
1 2 3 4 5 6 7 8 9 10 11 | <?php $dsn = 'mysql:dbname=calisma;host=localhost'; $user = 'root'; $password = ''; $baglan = new PDO($dsn, $user, $password); ?> |
guncelle.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <?php include 'baglan.php'; $id = $_POST['id']; $ad = $_POST['isim']; $cinsiyet =$_POST['cins']; $hobiler = implode(';', $_POST['hobiler']); $sorgu = $baglan->prepare("UPDATE ornek SET ad = ?, cinsiyet = ?, hobiler = ? WHERE id = ?"); $sorgu->bindParam(1, $ad, PDO::PARAM_STR); $sorgu->bindParam(2, $cinsiyet, PDO::PARAM_STR); $sorgu->bindParam(3, $hobiler, PDO::PARAM_STR); $sorgu->bindParam(4, $id, PDO::PARAM_INT); $sorgu->execute(); if ($sorgu->rowCount() > 0) { echo $sorgu->rowCount() . " kayıt güncellendi."; } else { echo "Herhangi bir kayıt güncellenemedi."; } |
index.php
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 | <?php include 'baglan.php'; ?> <!DOCTYPE html> <html lang="tr"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Yazılım Kodlama</title> </head> <body> <?php if(isset($_GET["id"])){ $sth = $baglan->prepare('SELECT * FROM ornek WHERE id=:id'); $sth->bindValue('id', $_GET['id'], PDO::PARAM_INT); $sth->execute(); $satir =$sth->fetch(); $hobiler = explode(';',$satir['hobiler']); ?> <form action="guncelle.php" method="post"> <input type="hidden" name="id" value="<?=$satir['id']?>"><br> İsim: <input type="text" name="isim" value="<?=$satir['ad']?>"><br> Cinsiyet: <br> <!-- cinisyet alanında iki veriden biri olacağı için veritabanındaki kayıt ile eşleşen alan dolu gelecek --> Erkek: <input type="radio" <?=$satir['cinsiyet']=='E' ? 'checked' : ''?> name="cins" value="E"> Kadın: <input type="radio" <?=$satir['cinsiyet']=='K' ? 'checked' : ''?> name="cins" value="K"> <hr> Yüzme: <input type="checkbox"<?=in_array('Yuzme',$hobiler) ? 'checked' : ''?> name="hobiler[]" value="Yuzme"><br> Koşu: <input type="checkbox"<?=in_array('Koşu',$hobiler) ? 'checked' : ''?> name="hobiler[]" value="Koşu"><br> Kitap Okuma: <input type="checkbox"<?=in_array('Kitap',$hobiler) ? 'checked' : ''?> name="hobiler[]" value="Kitap"><br> Futbol: <input type="checkbox"<?=in_array('Futbol',$hobiler) ? 'checked' : ''?> name="hobiler[]" value="Futbol"><br> <button type="submit">Güncelle</button> </form> <?php } ?> </body> </html> |
Çeşitli Arama Terimleri:
- Radio button php mysql,
- How to get radio button value in php,
- Checkbox php mysql,
- CRUD checkbox php mysql,
- Update multiple rows in php mysql with checkbox,
- Get multiple radio button value php,
- Radio button sql,
Kullanılan Kaynaklar:
Yorum yap