Paylaştığım Diğer Hazır Kodlar;
Süper Lig Puan Durumu (PHP)
Son Deprem Verileri (PHP)
Javascript ile Klavye Hız Testi
Javascript ile Vücut Kitle İndeksi Hesaplama
Javascript ile Kredi Hesaplama
Link Kısaltma Servisi (PHP)
Döviz Kurları Çevirici (PHP)
Resim Ölçeklendirme (Javascript)
Anket Uygulaması (PHP)
INDEX.PHP

YONETIM.PHP

INDEX.PHP
<!DOCTYPE html>
<html>
<head>
<title>Anket Uygulaması</title>
<style>
body {font-family: Arial, sans-serif;margin: 0;padding: 0;background-color: #f5f5f5;}
.container {max-width: 600px;margin: 0 auto;margin-top:15px;padding: 20px;background-color: #fff;box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);}
h1 {text-align: center;}
form {margin-top: 20px;}
label {display: block;margin-bottom: 10px;font-weight: bold;}
input[type="radio"] {margin-right: 5px;}
input[type="submit"] {background-color: #007bff;color: #fff;border: none;padding: 10px 20px;cursor: pointer;}
input[type="submit"]:hover {background-color: #0056b3;}
.results {margin-top: 30px;background-color: #f9f9f9;padding: 15px;border-radius: 4px;}
h2 {margin-top: 0;}
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.5.0/chart.min.js"></script>
</head>
<body>
<div class="container">
<?php
$servername = "localhost";
$username = "anket";
$password = "anket";
$dbname = "anket";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Veritabanı bağlantı hatası: " . $conn->connect_error);
}
$sql = "SELECT soru, secenek1, secenek1_oy, secenek2, secenek2_oy, secenek3, secenek3_oy, secenek4, secenek4_oy FROM sorular ORDER BY tarih DESC LIMIT 1";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$row = $result->fetch_assoc();
$soru = $row["soru"];
$secenek1 = $row["secenek1"];
$secenek1_oy = $row["secenek1_oy"];
$secenek2 = $row["secenek2"];
$secenek2_oy = $row["secenek2_oy"];
$secenek3 = $row["secenek3"];
$secenek3_oy = $row["secenek3_oy"];
$secenek4 = $row["secenek4"];
$secenek4_oy = $row["secenek4_oy"];
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (isset($_POST["secenek"])) {
$secilenSecenek = $_POST["secenek"];
$oy_sutun = "secenek" . $secilenSecenek . "_oy";
$sql = "UPDATE sorular SET $oy_sutun = $oy_sutun + 1 WHERE soru = '$soru'";
if ($conn->query($sql) === TRUE) {
echo "<p class='message'>Oyunuz başarıyla kaydedildi.</p>";
$secenek1_oy += ($secilenSecenek == 1) ? 1 : 0;
$secenek2_oy += ($secilenSecenek == 2) ? 1 : 0;
$secenek3_oy += ($secilenSecenek == 3) ? 1 : 0;
$secenek4_oy += ($secilenSecenek == 4) ? 1 : 0;
} else {
echo "<p class='error'>Hata: " . $sql . "<br>" . $conn->error . "</p>";
}
} else {
echo "<p class='error'>Lütfen bir seçenek işaretleyin!</p>";
}
}
echo "<form method='post'>";
echo "<p><strong>$soru</strong></p>";
echo "<label><input type='radio' name='secenek' value='1'> $secenek1</label>";
echo "<label><input type='radio' name='secenek' value='2'> $secenek2</label>";
echo "<label><input type='radio' name='secenek' value='3'> $secenek3</label>";
echo "<label><input type='radio' name='secenek' value='4'> $secenek4</label>";
echo "<input type='submit' value='Oy Ver'>";
echo "</form>";
echo "<div class='results'>";
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["secenek"])) {
echo "<h2>Sonuçlar:</h2>";
echo "<canvas id='myChart'></canvas>";
echo "<script>
var ctx = document.getElementById('myChart').getContext('2d');
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: ['$secenek1', '$secenek2', '$secenek3', '$secenek4'],
datasets: [{
label: 'Oylar',
data: [$secenek1_oy, $secenek2_oy, $secenek3_oy, $secenek4_oy],
backgroundColor: [
'rgba(75, 192, 192, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(255, 99, 132, 0.2)'
],
borderColor: [
'rgba(75, 192, 192, 1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(255, 99, 132, 1)'
],
borderWidth: 1
}]
},
options: {
scales: {
y: {
beginAtZero: true
}
}
}
});
</script>";
}
echo "</div>";
} else {
echo "<p class='error'>Anket bulunamadı.</p>";
}
$conn->close();
?>
</div>
</body>
</html>YONETIM.PHP<!DOCTYPE html>
<html>
<head>
<title>Anket Yönetimi</title>
<style>
body {font-family: Arial, sans-serif;margin: 0;padding: 0;background-color: #f5f5f5;}
.container {max-width: 1200px;margin: 0 auto;margin-top: 20px;padding: 20px;background-color: #fff;box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);}
h1 {text-align: center;}
table {width: 100%;border-collapse: collapse;margin-top: 20px;}
th, td {padding: 10px;text-align: left;border-bottom: 1px solid #ddd;font-size:12px;text-align:center;}
th {background-color: #f9f9f9;font-weight: bold;}
th, td:nth-child(3), td:nth-child(5), td:nth-child(7), td:nth-child(9) {text-align: center;}
td:nth-child(10) {white-space: nowrap;}
.error {color: red;}
.add-form-popup {display: none;position: fixed;top: 0;left: 0;width: 100%;height: 100%;background-color: rgba(0, 0, 0, 0.5);z-index: 1;}
.add-form-content {background-color: #f9f9f9;margin: 10% auto;padding: 20px;border: 1px solid #ddd;border-radius: 5px;width: 500px;}
.close {color: #aaa;float: right;font-size: 28px;font-weight: bold;}
.close:hover,
.close:focus {color: black;text-decoration: none;cursor: pointer;}
.form-group {margin-bottom: 15px;}
.form-group label {display: block;font-weight: bold;margin-bottom: 5px;}
.form-group input[type="text"] {width: 95%;padding: 10px;border: 1px solid #ddd;border-radius: 5px;}
.form-group input[type="submit"] {background-color: #007bff;color: #fff;border: none;padding: 10px 20px;cursor: pointer;}
.form-group input[type="submit"]:hover {background-color: #0056b3;}
</style>
<script>
function confirmDelete() {return confirm("EMİN MİSİNİZ? Anket silinecek!");}
function confirmReset() {return confirm("EMİN MİSİNİZ? Oy sayıları sıfırlanacak!");}
function openAddForm() {document.getElementById("addFormContainer").style.display = "block";}
function closeAddForm() {document.getElementById("addFormContainer").style.display = "none";}
</script>
</head>
<body>
<div class="container">
<h1>Anket Yönetimi</h1>
<button onclick="openAddForm()" class="add-button">Yeni Anket Ekle</button>
<?php
$servername = "localhost";
$username = "anket";
$password = "anket";
$dbname = "anket";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Veritabanı bağlantı hatası: " . $conn->connect_error);
}
if (isset($_GET["delete"])) {
$soru_id = $_GET["delete"];
$delete_sql = "DELETE FROM sorular WHERE id = '$soru_id'";
if ($conn->query($delete_sql)) {
echo "<p class='message'>Anket başarıyla silindi.</p>";
} else {
echo "<p class='error'>Anket silinirken bir hata oluştu.</p>";
}
}
if (isset($_GET["reset"])) {
$soru_id = $_GET["reset"];
$reset_sql = "UPDATE sorular SET secenek1_oy = 0, secenek2_oy = 0, secenek3_oy = 0, secenek4_oy = 0 WHERE id = '$soru_id'";
if ($conn->query($reset_sql)) {
echo "<p class='message'>Oy sayıları sıfırlandı.</p>";
} else {
echo "<p class='error'>Oy sayıları sıfırlanırken bir hata oluştu.</p>";
}
}
$sql = "SELECT id, soru, secenek1, secenek1_oy, secenek2, secenek2_oy, secenek3, secenek3_oy, secenek4, secenek4_oy, tarih FROM sorular ORDER BY tarih DESC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table>
<tr>
<th>Soru</th>
<th>Seçenek 1</th>
<th>Oylar</th>
<th>Seçenek 2</th>
<th>Oylar</th>
<th>Seçenek 3</th>
<th>Oylar</th>
<th>Seçenek 4</th>
<th>Oylar</th>
<th>Tarih</th>
<th>İşlemler</th>
</tr>";
while ($row = $result->fetch_assoc()) {
echo "<tr>
<td>{$row["soru"]}</td>
<td>{$row["secenek1"]}</td>
<td>{$row["secenek1_oy"]}</td>
<td>{$row["secenek2"]}</td>
<td>{$row["secenek2_oy"]}</td>
<td>{$row["secenek3"]}</td>
<td>{$row["secenek3_oy"]}</td>
<td>{$row["secenek4"]}</td>
<td>{$row["secenek4_oy"]}</td>
<td>{$row["tarih"]}</td>
<td>
<a href='?delete={$row["id"]}' onclick='return confirmDelete();' class='delete-button'>Sil</a>
<a href='?reset={$row["id"]}' onclick='return confirmReset();' class='reset-button'>Sıfırla</a>
</td>
</tr>";
}
echo "</table>";
} else {
echo "<p class='error'>Mevcut anket bulunamadı.</p>";
}
$conn->close();
?>
</div>
<div id="addFormContainer" class="add-form-popup">
<div class="add-form-content">
<span class="close" onclick="closeAddForm()">×</span>
<h2>Yeni Anket Ekle</h2>
<form method="post" action="">
<div class="form-group">
<label for="soru">Soru:</label>
<input type="text" id="soru" name="soru" required>
</div>
<div class="form-group">
<label for="secenek1">Seçenek 1:</label>
<input type="text" id="secenek1" name="secenek1" required>
</div>
<div class="form-group">
<label for="secenek2">Seçenek 2:</label>
<input type="text" id="secenek2" name="secenek2" required>
</div>
<div class="form-group">
<label for="secenek3">Seçenek 3:</label>
<input type="text" id="secenek3" name="secenek3" required>
</div>
<div class="form-group">
<label for="secenek4">Seçenek 4:</label>
<input type="text" id="secenek4" name="secenek4" required>
</div>
<div class="form-group">
<input type="submit" value="Ekle" name="addAnket">
</div>
</form>
</div>
<?php
$servername = "localhost";
$username = "anket";
$password = "anket";
$dbname = "anket";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Veritabanı bağlantı hatası: " . $conn->connect_error);
}
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST["addAnket"])) {
$soru = $_POST["soru"];
$secenek1 = $_POST["secenek1"];
$secenek2 = $_POST["secenek2"];
$secenek3 = $_POST["secenek3"];
$secenek4 = $_POST["secenek4"];
$tarih = date("Y-m-d H:i:s");
$insertSql = "INSERT INTO sorular (soru, secenek1, secenek2, secenek3, secenek4, secenek1_oy, secenek2_oy, secenek3_oy, secenek4_oy, tarih)
VALUES ('$soru', '$secenek1', '$secenek2', '$secenek3', '$secenek4', 0, 0, 0, 0, '$tarih')";
$getIdSql = "SELECT LAST_INSERT_ID() as last_id";
$result = $conn->query($getIdSql);
if ($result && $result->num_rows > 0) {
$row = $result->fetch_assoc();
$lastId = $row["last_id"];
$newId = $lastId + 1;
} else {
$newId = 1;
}
$sql = "ALTER TABLE sorular AUTO_INCREMENT = $newId";
if ($conn->query($insertSql) === TRUE && $conn->query($sql) === TRUE) {
echo "Anket başarıyla eklendi.";
} else {
echo "Hata: " . $sql . "<br>" . $conn->error;
}
}
$conn->close();
?>
</div>
</body>
</html>