Online training and video tutorials
Enter     Registration
     Contacts

 
     
 
Max Azimov
St. Petersburg

Articles [97]

User's video [259]





PHP Insert Data Into MySQL
 
Insert Data Into MySQL Using MySQLi and PDO
 
After a database and a table have been created, we can start adding data in them.
 
Here are some syntax rules to follow:
  • The SQL query must be quoted in PHP
  • String values inside the SQL query must be quoted
  • Numeric values must not be quoted
  • The word NULL must not be quoted
 
The INSERT INTO statement is used to add new records to a MySQL table:
  1. INSERT INTO table_name (column1, column2, column3,...)
  2. VALUES (value1, value2, value3,...)
 
In the previous chapter we created an empty table named "MyGuests" with five columns: "id", "firstname", "lastname", "email" and "reg_date". Now, let us fill the table with data.
 
Note: If a column is AUTO_INCREMENT (like the "id" column) or TIMESTAMP (like the "reg_date" column), it is no need to be specified in the SQL query; MySQL will automatically add the value.
 
The following examples add a new record 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. if ($conn->query($sql) === TRUE) {
  14.     echo "New record created successfully";
  15. } else {
  16.     echo "Error: " . $sql . "<br>" . $conn->error;
  17. }
  18. $conn->close();
  19. ?>
 
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.  
  15. if (mysqli_query($conn, $sql)) {
  16.     echo "New record created successfully";
  17. } else {
  18.     echo "Error: " . $sql . "<br>" . mysqli_error($conn);
  19. }
  20. mysqli_close($conn);
  21. ?>
 
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.     $sql = "INSERT INTO MyGuests (firstname, lastname, email)
  11.     VALUES ('John', 'Doe', 'john@example.com')";
  12.     // use exec() because no results are returned
  13.     $conn->exec($sql);
  14.     echo "New record created successfully";
  15.     }
  16. catch(PDOException $e)
  17.     {
  18.     echo $sql . "<br>" . $e->getMessage();
  19.     }
  20. $conn = null;
  21. ?>


2016-12-10
474 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 Русский