PHP ile Dizileri MYSQL tablosuna kaydetmek
Aynı amaca hizmet eden <input> etiketlerinin her birine ayrı ayrı isim vermek yerine [] kullanarak bir dizi oluşturmuş oluruz.
HTML Sayfası
<form action="?" method="post" > <table width="30%" border="0" align="center" cellpadding="2" cellspacing="2"> <tr> <th>TATIL ISMI * </th> <th>TARIH</th> </tr> <tr> <td><input name="isim[]" type="text"/></td> <td><input type="text" name="tarih[]"/></td> </tr> <tr> <td><input type="text" name="isim[]"/></td> <td><input type="text" name="tarih[]"/></td> </tr> <tr> <td><input type="text" name="isim[]"/></td> <td><input type="text" name="tarih[]"/></td> </tr> <tr> <td><input type="text" name="isim[]"/></td> <td><input type="text" name="tarih[]"/></td> </tr> <tr> <td><input type="text" name="isim[]"/></td> <td><input type="text" name="tarih[]"/></td> </tr> <tr> <td><input type="text" name="isim[]"/></td> <td><input type="text" name="tarih[]"/></td> </tr> </table> <div style="text-align:center; margin-top:25px"><input name="btnsubmit" type="submit" /></div> </form>
PHP Sayfası
<?PHP /* Burada veritabanına bağlantı kurmayı unutmayın $db_host = "localhost"; //Host adres $db_name = "veritabani"; //veritabani ismi $db_user = "root"; //veritabani kullanici ismi $db_pass = "sifre"; //sifre $mysqli = new mysqli($db_host, $db_user, $db_pass, $db_name); $mysqli->query("SET NAMES utf8"); if(mysqli_connect_errno()) { echo "Hata Oluştu: " . mysqli_connect_errno(); exit(); } */ if(isset($_POST['btnsubmit'])) { $isim = $_POST['isim']; foreach ($isim as $k => $val) { if ($val != ''){ $sql ="INSERT INTO tatiller SET tatil_tarih = '".$_POST['tarih'][$k]."', tatil_isim = '".$mysqli->escape_string($_POST['isim'][$k])."'"; $mysqli->query($sql); } } } ?>
Hepsi bir arada
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Bilgisayar.me</title> </head> <body> <?PHP if(isset($_POST['btnsubmit'])) { $isim = $_POST['isim']; foreach ($isim as $k => $val) { if ($val != ''){ $sql ="INSERT INTO tatiller SET tatil_tarih = '".$mysqli->escape_string($_POST['tarih'][$k])."', tatil_isim = '".$mysqli->escape_string($_POST['isim'][$k])."'"; $mysqli->query($sql); } } } ?> <form action="?" method="post" > <table width="30%" border="0" align="center" cellpadding="2" cellspacing="2"> <tr> <th>TATIL ISMI * </th> <th>TARIH</th> </tr> <tr> <td><input name="isim[]" type="text"/></td> <td><input type="text" name="tarih[]"/></td> </tr> <tr> <td><input type="text" name="isim[]"/></td> <td><input type="text" name="tarih[]"/></td> </tr> <tr> <td><input type="text" name="isim[]"/></td> <td><input type="text" name="tarih[]"/></td> </tr> <tr> <td><input type="text" name="isim[]"/></td> <td><input type="text" name="tarih[]"/></td> </tr> <tr> <td><input type="text" name="isim[]"/></td> <td><input type="text" name="tarih[]"/></td> </tr> <tr> <td><input type="text" name="isim[]"/></td> <td><input type="text" name="tarih[]"/></td> </tr> </table> <div style="text-align:center; margin-top:25px"><input name="btnsubmit" type="submit" /></div> </form> </body> </html>