How to Reset Mysql Auto Increment Field after Deleting Records

Sometime you may need to delete records from a table several times and also add new ones in between. This will create non sequential values for your auto increment field. You can avoid that by resetting the value of the auto-incrementing field by running the following two quires right after deleting records.

new_id = (SELECT MAX(id) FROM your_table_name)+1
ALTER TABLE your_table_name AUTO_INCREMENT = new_id

How to Preserve Request Data while Redirecting in CakePHP Controller

If you try to submit a form to different controller than the one you are on, all validation error messages and flash messages will be lost while the second controller redirects you back to the current one. To solve this problem, I modified this component from CakePHP bakery and made it compatible with CakePHP 2.x. I hope you find it useful. It will keep your request data and error validation messages intact while redirecting between two controllers.

 

Deleting multiple records in CackePHP App

Let’s say you are coding the user management part of your CakePHP app, and you wanted to introduce a feature that will allow you to delete multiple users at once. Here is one way you can achieve that:

Your View

Notice the dot at the end of the checkbox’s fieldName parameter “cid.”

The above code will create the following HTML code:

The extra square bracket at the end of the name field was created by the dot appended above in the php code. That’s something I had to find out after a bit of searching.

Your Controller Action

 

Whitelisting your Domain in mod_security Configuration File

I had trouble running a cron script using the following command after switching server for one of our websites.

The website was hosted on a shared hosting at hostgator and it was working fine for long time. But after switching server to VPS, I started to get 403 errors whenever the cron script runs. After some research, I found out that mod_security is the one that is causing the trouble and there are three things you can do to solve the problem.

  1. Disable mod_security via .htaccess or uninstall it totally from your server
  2. Convert your script to curl
  3. White list your domain in mod_security configuration file

I think the best solution in terms of security is probably converting your script into curl but I chose option #3.

Here is what you need to add to white list your domain:

You can add the line anywhere in mod_security’s configuration file. I added mine simply at the top.