# CakePHP 2.x how to store encrypted data in mysql?

**URL:** <https://discourse.cakephp.org/t/cakephp-2-x-how-to-store-encrypted-data-in-mysql/749>\
**Category:** Need Help\
**Created:** [July 18, 2016, 8:45am UTC](https://discourse.cakephp.org/t/cakephp-2-x-how-to-store-encrypted-data-in-mysql/749 "2016-07-18T08:45:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![loind](https://avatars.discourse-cdn.com/v4/letter/l/9fc29f/32.png) [@loind](https://discourse.cakephp.org/u/loind)\
**Post date:** [July 18, 2016, 8:45am UTC](https://discourse.cakephp.org/t/cakephp-2-x-how-to-store-encrypted-data-in-mysql/749/1 "2016-07-18T08:45:13Z")

</div>

I am working on a Cakephp 2.6 I’m trying to encrypt my data which i am storing into db  
For example i have a function in controller which is saving data:

```auto
$data = $this->request->data;
$data['card_serial'] = Security::encrypt($data['card_serial'], SECURITY_CODE);
$this->Card->save($data);

```

And view, I’m trying to decrypt:

```auto
<td> <?php echo Security::decrypt($record['Card']['card_serial'], SECURITY_CODE); ?> </td>

```

I have done this but didnt work

```auto
Error: [CakeException] The data to decrypt cannot be empty.

```

---

<div class="post-metadata">

**Author:** ![calzone](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/calzone/32/305_2.png) [@calzone](https://discourse.cakephp.org/u/calzone)\
**Post date:** [July 18, 2016, 6:40pm UTC](https://discourse.cakephp.org/t/cakephp-2-x-how-to-store-encrypted-data-in-mysql/749/2 "2016-07-18T18:40:28Z")

</div>

You’ve probably already tried this, but just in case, make sure the data you expect is actually there, getting saved, and coming back out:

after your save:  
`debug($data['card_serial']);`

and in your view, again:  
`debug($record['Card']['card_serial']);`
