I need to store both text and images to be stored in the same field? How can this be implemented in php using a mysql database? I have used BLOB to store images in the past . Which datatype should I use for this problem?

And how will this work...?
Actually I am implementing a small questionnaire for my site. So the questions have options which will be sometimes a combination of text and images.Note:there can be multiple images for 1 option and at any position :ie in the beginning of the text ,in the middle of the text and at the end of the text.....How to implement this so that I am able to correctly display the option content with the text and images in the correct order...?

Dani AI

Generated

Both ideas already suggested by and are workable. For a maintainable, secure solution that supports multiple images per option and arbitrary placement, store option content as a sequence of “segments” (text or image) and keep image binaries/paths and metadata in a separate images table or on disk/CDN. That makes ordering, rendering and sanitization deterministic instead of trying to parse mixed free-form HTML at display time.

Example of a segment format (store in a JSON/TEXT column):

[
  {"type":"text", "value":"Start of option"},
  {"type":"image", "image_id":42, "caption":"diagram A"},
  {"type":"text", "value":"continued text"}
]

Suggested table layout (MySQL JSON column if available, otherwise TEXT):

options (id INT PK, content JSON_or_TEXT, ...)
option_images (id INT PK, option_id INT, file_path VARCHAR(255), alt_text VARCHAR(255), sort_order INT)

Rendering workflow (server-side): decode the segments, fetch any referenced image rows by id, then output segments in order. Sanitize text with htmlspecialchars (or a library like HTMLPurifier if limited HTML is allowed). For images emit <img> with the canonical URL from file_path, alt_text, and loading="lazy" for performance.

Notes and troubleshooting: prefer filesystem/ CDN storage for images (store paths in DB) to avoid large BLOBs and backup/performance pain. Keep sort_order or explicit segment indices to preserve placement. Always use prepared statements for DB writes, validate uploaded files (type, size), and check permissions/URL paths if images fail to display.

Recommended Answers

All 2 Replies

You can store the HTML for a particular option (text column), if your images are accessible by a path (instead of images stored in a blob).

Store images and text in different columns, use CSS classes for image position and store the CSS class name with the option.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.