Contoh Query Insert <?php $pdo = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass); $stmt = $pdo->prepare('INSE...
<?php
$pdo = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
$stmt = $pdo->prepare('INSERT INTO tabel_pengguna (id, nama) VALUES (:id, :nama)');
$id = filter_input(INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT);
$nama = filter_input(INPUT_GET, 'nama', FILTER_SANITIZE_STRING)
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$stmt->bindParam(':nama', $nama, PDO::PARAM_STR);
$stmt->execute();
?>
Contoh Query Update
<?php
$pdo = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass);
$stmt = $pdo->prepare('UPDATE tabel_pengguna SET nama = :nama WHERE id = :id');
$id = filter_input(INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT);
$nama = filter_input(INPUT_GET, 'nama', FILTER_SANITIZE_STRING)
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$stmt->bindParam(':nama', $nama, PDO::PARAM_STR);
$stmt->execute();
?>
Cara mencoba kode-kode di atas, langsung ketik saja di address bar.
Contoh:
localhost/namafile.php?id=10&nama=ronaldo.
Jangan lupa isi variabel-variabel $host, $dbname, $user, dan $pass.
Tidak ada komentar