PHP and MySQL 20: query insert
Tags: php, mysql, mysqli, pdo
Published: 2019-12-22 07:30:04

___

<center>Happy Coding</center>
___


## `Home` <a name="home"> [`Previous`](https://dev.to/antelove19/php-and-mysql-11-connection-database-2mm)

In this post, we will learn how to use query **INSERT** in PHP for store data into database MySQL, with 3 ways:

  1. [MySQLi procedural](#procedural) 
  2. [MySQLi object-oriented](#object)
  3. [PDO](#pdo)

> For known, how to connect to database, can be learned in [here](https://dev.to/antelove19/php-and-mysql-11-connection-database-2mm)

Insert query is used for insert data or record into database

Syntax:
```php
    INSERT INTO table_name (column1, column2, column3, ...)
    VALUES (value1, value2, value3, ...);
```

Example:

```php
    $sql  = "INSERT INTO users ";
    $sql .= "( ";
    $sql .= "firstname ";
    $sql .= "lastname ";    
    $sql .= ") ";

    $sql .= "VALUES ";
    $sql .= "( ";
    $sql .= "'John' "; // string, so we must add quote
    $sql .= "'Doe' "; // string    
    $sql .= ")";
```
___


And, for execute the query, we will use 3 ways:


### 1. MySQLi[^1] procedural <a name="procedural"></a>

[`Back to Home`](#home)

Execute query using **mysqli::query**[^2] and get a **`$result`**:

```php
    $result = mysqli_query( $connection, $sql );
```

Check the **`$result`**, 
if **`TRUE`**, call **mysqli::$insert_id**[^3] function for get last id inserted and display a success message, 
else display error messages using **mysqli::$error**[^4]:

```php
    if ( $result ) // if TRUE
    {

        $id = mysqli_insert_id($connection);

        echo "Data has been <b>CREATED</b> with ID {$id} ..";

    }
    else 
    {

        echo "Error: {$sql}";
        echo "<br />";
        echo "Error: " . mysqli_error($connection);

    }
```

Close connection using **mysqli::close**[^5]:

```php
    mysqli_close($connection);
```

Source Code:

* [repl.it/antelove19/MySQLi/procedural/insert](https://repl.it/@antelove19/MySQLi-procedural-20-insert)

___


### 2. MySQLi[^1] object-oriented <a name="object"></a>

[`Back to Home`](#home)

Execute query using **mysqli::query**[^2] and get a **`$result`**:

```php
    $result = $connection->query($sql);
```

Check the **`$result`**:

```php
    if ( $result ) // if TRUE
    {

        $id = $connection->insert_id;

        echo "Data has been <b>CREATED</b> with ID {$id} ..";

    } 
    else 
    {

        echo "Error: {$sql}";
        echo "<br />";
        echo "Error: {$connection->error}";

    }
```

Close connection using **mysqli::close**[^5]:

```php
     $connection->close();
```

Source Code:

* [repl.it/antelove19/MySQLi/object-oriented/insert](https://repl.it/@antelove19/MySQLi-object-oriented-20-insert)

___


### 3. PDO[^7] <a name="pdo"></a>

[`Back to Home`](#home)

Execute query and get a **`$result`**:

```php
    $result = $connection->query($sql);
```

Check the **`$result`**:

```php
    if ( $result ) // if TRUE
    {

        /* last id insert */
        $id = $connection->lastInsertId();

        echo "Data has been <b>CREATED</b> with ID {$id} ..";

    } 
    else 
    {

        echo "Error: {$sql}";
        echo "<br />";
        echo "Error: {$connection->error}";

    }
```

Close connection:

```php
    $connection = null;
```

Source Code:

* [repl.it/antelove19/PDO/insert](https://repl.it/@antelove19/PDO-20-insert)

___


## [`Back to Home`](#home) | [`Next`](https://dev.to/antelove19/php-and-mysql-30-query-select-31ig)

___

<center>Thank for reading :)</center>
___


[^1]: **php.net**, "MySQL Improved Extension", accessed on date 21 december 2019 and from [_https://www.php.net/manual/en/book.mysqli.php_] (https://www.php.net/manual/en/mysqli.construct.php)

[^2]: **php.net**, "mysqli::query", accessed on date 22 december 2019 and from [_https://www.php.net/manual/en/mysqli.query.php_] (https://www.php.net/manual/en/mysqli.query.php)

[^3]: **php.net**, "mysqli::$insert_id", accessed on date 22 december 2019 and from [_https://www.php.net/manual/en/mysqli.insert-id.php_] (https://www.php.net/manual/en/mysqli.insert-id.php)

[^4]: **php.net**, "mysqli::$error", accessed on date 22 december 2019 and from [_https://www.php.net/manual/en/mysqli.connect-error.php_] (https://www.php.net/manual/en/mysqli.connect-error.php)

[^5]: **php.net**, "mysqli::close", accessed on date 21 december 2019 and from [_https://www.php.net/manual/en/mysqli.close.php_] (https://www.php.net/manual/en/mysqli.close.php)

[^6]: **php.net**, "PHP Data Objects", accessed on date 21 december 2019 and from [_https://www.php.net/manual/en/book.pdo.php_] (https://www.php.net/manual/en/book.pdo.php) 

[^7]: **php.net**, "PDO::__construct", accessed on date 21 december 2019 and from [_https://www.php.net/manual/en/pdo.construct.php_] (https://www.php.net/manual/en/pdo.construct.php)