-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.php
72 lines (65 loc) · 1.93 KB
/
example.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
// Veritabanı bağlantısı
$host = "localhost";
$dbname = "innerjoin";
$user = "root";
$pass = "";
try{
$db = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8",$user,$pass);
}catch(PDOException $e){
echo '<strong>Veritabanı Bağlantısı Başarısız Oldu</strong> : ' . $e->getMessage();
exit;
}
// Tabloları birleştirmekte kullanacağımız fonksiyon
function multiple($sql){
global $db;
$sorgu = $db->prepare($sql);
$sorgu->execute();
$liste = $sorgu->fetchAll(PDO::FETCH_ASSOC);
return $liste;
}
// Gönderme işlemi
if(isset($_POST['send'])){
$sorgu = $db->prepare("INSERT INTO items SET
itemName=:itemName,
itemBrand=:itemBrand
");
$sonuc = $sorgu->execute(array(
'itemName' => $_POST['itemName'],
'itemBrand' => $_POST['itemBrand']
}
// Ürün yükleme formu
<form action="" method="post">
<label>Ürün Adı</label>
<input type="text" name="itemName">
<label>Marka</label>
<select name="itemBrand">
<?php
$sorgu = $db->prepare("SELECT * FROM brands");
$sorgu->execute();
while($brand = $sorgu->fetch(PDO::FETCH_ASSOC)){ ?>
<option value="<?= $brand['id']?>"><?= $marka['brandName'] ?></option>
<?php }
?>
<button type="submit" name="send">Gönder</button>
</select>
</form>
// Tüm ürünleri bir tabloda görelim
<table class="table table-striped" id="table">
<thead>
<tr>
<th> Ürün Adı </th>
<th> Marka </th>
</tr>
</thead>
<tbody>
<?php
// Markanın id bilgisi ile ürünün marka bilgisini eşleştirdik. <select value="marka id">Marka Adı</select> gibi.
foreach (multiple("SELECT * FROM items INNER JOIN brands ON brands.id=items.itemBrand) as $key => $items): ?>
<tr>
<td><?= $items['itemName'] ?></td>
<td><?= $items['itemBrand'] ?></td>
</tr>
<?php endforeach ?>
<tbody>
?>