Online training and video tutorials
Enter     Registration
     Contacts

 
     
 
Max Azimov
St. Petersburg

Articles [97]

User's video [259]





PHP Insert Multiple Records Into MySQL
 
Insert Multiple Records Into MySQL Using MySQLi and PDO
Multiple SQL statements must be executed with the mysqli_multi_query() function.
 
The following examples add three new records to the "MyGuests" table:
 
Example (MySQLi Object-oriented)
  1. <?php
  2. $servername = "localhost";
  3. $username = "username";
  4. $password = "password";
  5. $dbname = "myDB";
  6. // Create connection
  7. $conn = new mysqli($servername, $username, $password, $dbname);
  8. // Check connection
  9. if ($conn->connect_error) {
  10.     die("Connection failed: " . $conn->connect_error);
  11. $sql = "INSERT INTO MyGuests (firstname, lastname, email)
  12. VALUES ('John', 'Doe', 'john@example.com');";
  13. $sql .= "INSERT INTO MyGuests (firstname, lastname, email)
  14. VALUES ('Mary', 'Moe', 'mary@example.com');";
  15. $sql .= "INSERT INTO MyGuests (firstname, lastname, email)
  16. VALUES ('Julie', 'Dooley', 'julie@example.com')";
  17. if ($conn->multi_query($sql) === TRUE) {
  18.     echo "New records created successfully";
  19. } else {
  20.     echo "Error: " . $sql . "<br>" . $conn->error;
  21. }
  22. $conn->close();
  23. ?>
Note that each SQL statement must be separated by a semicolon.
 
 
Example (MySQLi Procedural)
  1. <?php
  2. $servername = "localhost";
  3. $username = "username";
  4. $password = "password";
  5. $dbname = "myDB";
  6. // Create connection
  7. $conn = mysqli_connect($servername, $username, $password, $dbname);
  8. // Check connection
  9. if (!$conn) {
  10.     die("Connection failed: " . mysqli_connect_error());
  11. }
  12. $sql = "INSERT INTO MyGuests (firstname, lastname, email)
  13. VALUES ('John', 'Doe', 'john@example.com');";
  14. $sql .= "INSERT INTO MyGuests (firstname, lastname, email)
  15. VALUES ('Mary', 'Moe', 'mary@example.com');";
  16. $sql .= "INSERT INTO MyGuests (firstname, lastname, email)
  17. VALUES ('Julie', 'Dooley', 'julie@example.com')";
  18. if (mysqli_multi_query($conn, $sql)) {
  19.     echo "New records created successfully";
  20. } else {
  21.     echo "Error: " . $sql . "<br>" . mysqli_error($conn);
  22. }
  23. mysqli_close($conn);
  24. ?>
 
The PDO way is a little bit different:
 
Example (PDO)
  1. <?php
  2. $servername = "localhost";
  3. $username = "username";
  4. $password = "password";
  5. $dbname = "myDBPDO";
  6. try {
  7.     $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  8.     // set the PDO error mode to exception
  9.     $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  10.     // begin the transaction
  11.     $conn->beginTransaction();
  12.     // our SQL statements
  13.     $conn->exec("INSERT INTO MyGuests (firstname, lastname, email) 
  14.     VALUES ('John', 'Doe', 'john@example.com')");
  15.     $conn->exec("INSERT INTO MyGuests (firstname, lastname, email) 
  16.     VALUES ('Mary', 'Moe', 'mary@example.com')");
  17.     $conn->exec("INSERT INTO MyGuests (firstname, lastname, email) 
  18.     VALUES ('Julie', 'Dooley', 'julie@example.com')");
  19.     // commit the transaction
  20.     $conn->commit();
  21.     echo "New records created successfully";
  22.     }
  23. catch(PDOException $e)
  24.     {
  25.     // roll back the transaction if something failed
  26.     $conn->rollback();
  27.     echo "Error: " . $e->getMessage();
  28.     }
  29. $conn = null;
  30. ?>


2016-12-10
583 views




 
     
 
Map      Distance education, online learning courses. Project PLANETSTUDY - distance learning online.
© 2012 PlanetStudy

Catalog.299.ru

The site is a media provider
Яндекс.Метрика
 
English USA
Australia Русский