Home » PHP » Insert Banyak Record Ke Database Dengan PHP MySQL

Insert Banyak Record Ke Database Dengan PHP MySQL

by Hanifah Nurbaeti
by Hanifah Nurbaeti

Masukkan Beberapa Record Ke MySQL Menggunakan MySQLi dan PDO

Beberapa pernyataan SQL harus dijalankan dengan fungsi mysqli_multi_query().

Contoh berikut menambahkan tiga record baru ke tabel “MyGuests”:

Contoh (MySQLi Object-oriented) :

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Buat Koneksi
$conn = new mysqli($servername, $username, $password, $dbname);
// Periksa Koneksi
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', '[email protected]');";
$sql .= "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('Mary', 'Moe', '[email protected]');";
$sql .= "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('Julie', 'Dooley', '[email protected]')";

if ($conn->multi_query($sql) === TRUE) {
  echo "New records created successfully";
} else {
  echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>

Jangan lupa pisahkan pernyataan SQL dengan titik koma !.

Contoh (MySQLi Prosedural) :

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Buat Koneksi
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Periksa Koneksi
if (!$conn) {
  die("Connection failed: " . mysqli_connect_error());
}

$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', '[email protected]');";
$sql .= "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('Mary', 'Moe', '[email protected]');";
$sql .= "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('Julie', 'Dooley', '[email protected]')";

if (mysqli_multi_query($conn, $sql)) {
  echo "New records created successfully";
} else {
  echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

mysqli_close($conn);
?>

Cara PDO sedikit berbeda:

Contoh (PDO) :

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDBPDO";

try {
  $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  // mengatur mode kesalahan PDO ke pengecualian
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

  // mulai transaksi
  $conn->beginTransaction();
  // statement SQL
  $conn->exec("INSERT INTO MyGuests (firstname, lastname, email)
  VALUES ('John', 'Doe', '[email protected]')");
  $conn->exec("INSERT INTO MyGuests (firstname, lastname, email)
  VALUES ('Mary', 'Moe', '[email protected]')");
  $conn->exec("INSERT INTO MyGuests (firstname, lastname, email)
  VALUES ('Julie', 'Dooley', '[email protected]')");

  // melakukan transaksi
  $conn->commit();
  echo "New records created successfully";
} catch(PDOException $e) {
  // memutar kembali transaksi jika ada yang gagal
  $conn->rollback();
  echo "Error: " . $e->getMessage();
}

$conn = null;
?>

You may also like