PHP DATA ENCRYPTION INSERT RETRIEVE MYSQL DATABASE

PHP Data Encryption is important to safety and privacy. In this lesson, I am going to show you how to insert and retrieve encrypted data in PHP using a MySQL database. Using these methods will make your websites and applications more secure. After completing this tutorial then you will want to go over How To Search PHP Encrypted Database.

PHP DATA ENCRYPTION INSERT RETRIEVE MYSQL DATABASE

In an earlier PHP Data Encryption lesson, I showed you guys how to  use OpenSSL to encrypt and decrypt your data. In this lesson, I will show you how to store it and retrieve it from a MySQL Database.

Code Example

function encryptthis($data, $key) {
$encryption_key = base64_decode($key);
$iv = openssl_random_pseudo_bytes(openssl_cipher_iv_length('aes-256-cbc'));
$encrypted = openssl_encrypt($data, 'aes-256-cbc', $encryption_key, 0, $iv);
return base64_encode($encrypted . '::' . $iv);
}

function decryptthis($data, $key) {
$encryption_key = base64_decode($key);
list($encrypted_data, $iv) = array_pad(explode('::', base64_decode($data), 2),2,null);
return openssl_decrypt($encrypted_data, 'aes-256-cbc', $encryption_key, 0, $iv);
}

Create Database For Encryption

Create a database to insert your encrypted data.  Name the database “users” in your phpmyadmin dashboard. Then create a table called people. Next, make 4 columns in the table id, name, email, reg_date. Set the name and email fields to TEXT. Do not use varchar because varchar will only give you 255 characters and you may need more than that for your encrypted code. We will use this database to insert and retrieve the data.

Only cool people share!

CREATE TABLE `people` (
`id` int(11) NOT NULL,
`name` text NOT NULL,
`email` text NOT NULL,
`reg_date` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

Create A Config File

You need to create a config file to connect to your database. This will let us insert our encrypted data and retrieve our encrypted data.

Inserting PHP Encrypted Data

To insert your PHP Encrypted Data into the database you created from the information above, use this code.

mysqli_query($con,"INSERT INTO people(`name`, `email`)VALUES ('$name','$email')");
echo '<div class="alert alert-success"> You entered successfully</div>';

Header.php File

<?php
date_default_timezone_set('America/New_York');
include_once('functions.php');
?>
<html>
<head>
<title>PHP ENCRYPTION DECRYPTION MYSQL</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" type="text/css" >
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.2/animate.min.css">
</head>
<body>
<div class="container">

Footer.php File

</div>
</body>
</html>
<?php
$con->close();
?>

Copy all the files here and set them up on your server. Follow the video instructions and if you have any questions, ask them below.

PHP DATA ENCRYPTION INSERT RETRIEVE MYSQL DATABASE was last modified: November 5th, 2021 by Maximus Mccullough
Summary
PHP DATA ENCRYPTION INSERT RETRIEVE MYSQL DATABASE
Article Name
PHP DATA ENCRYPTION INSERT RETRIEVE MYSQL DATABASE
Description
How to insert and retrieve encrypted data in PHP using a MySQL database
Author
Publisher
A1WEBSITEPRO LLC
Logo
PHP DATA ENCRYPTION INSERT RETRIEVE MYSQL DATABASE

14 Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.