1406 🐬 MySQL

MySQL ERROR 1406: Data too long for column

The value is longer than the column allows (e.g. 300 characters into VARCHAR(255)), and strict mode rejects it instead of truncating.

Seen on: MySQL PHP

Meaning

With STRICT_TRANS_TABLES (default since MySQL 5.7), oversize values raise 1406. Without strict mode MySQL silently truncated them — so moving to a new server can make old code fail.

Common causes

  • Input longer than the column definition
  • Multi-byte characters exceeding byte limits in older setups
  • Column too small for real-world data (URLs, user agents, names)
  • ENUM/SET value not in the list (related 1265 warning)

⚡ Quick fix

  1. Validate/limit length in the application
  2. Widen the column (VARCHAR(2048) or TEXT)
  3. Don’t disable strict mode — it protects data integrity

Detailed fix by platform

MySQL

  1. Widen the column:
    sql
    ALTER TABLE visits MODIFY user_agent VARCHAR(512) NOT NULL DEFAULT '';

PHP

  1. Trim before insert: mb_substr($ua, 0, 512) and validate max lengths in forms.

How to diagnose

  1. Column — Defined length vs actual value length
  2. Mode — SELECT @@sql_mode — strict?

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.