Nasıl mysql bağlantısı yapabilirim?
2
●86
- 13-12-2023, 00:51:19Chatgpt'ye basit bir örnek yazdırdım.
Html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>MySQL'e Kayıt Formu</title>
</head>
<body>
<h2>Kullanıcı Kayıt Formu</h2>
<form action="kaydet.php" method="post">
<label for="ad">Adınız:</label>
<input type="text" name="ad" required><br>
<label for="soyad">Soyadınız:</label>
<input type="text" name="soyad" required><br>
<label for="email">E-posta Adresiniz:</label>
<input type="email" name="email" required><br>
<input type="submit" value="Kaydet">
</form>
</body>
</html>
PHP
<?php
try {
// Veritabanı bilgileri
$host = 'your_host';
$database = 'your_database';
$username = 'your_username';
$password = 'your_password';
$db = new PDO("mysql:host=$host;dbname=$database;charset=utf 8", $username, $password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$ad = $_POST['ad'];
$soyad = $_POST['soyad'];
$email = $_POST['email'];
$query = "INSERT INTO kullanicilar (ad, soyad, email) VALUES (:ad, :soyad, :email)";
$stmt = $db->prepare($query);
$stmt->bindParam(':ad', $ad);
$stmt->bindParam(':soyad', $soyad);
$stmt->bindParam(':email', $email);
$stmt->execute();
echo "Kayıt başarıyla eklendi.";
} catch (PDOException $e) {
echo "Hata:
" . $e->getMessage();
}
?>
