Solved

Need to add a forward slash "/" to all image fields in MySQL table?

Posted on 2014-10-23
3
329 Views
Last Modified: 2014-10-23
Hi,
I need to add a forward slash "/" to all image fields in MySQL table only where they don't already exits.

Is there an easy way to do this?

Thanks in advance for your help.

CREATE TABLE IF NOT EXISTS `items_test` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `image1` varchar(60) DEFAULT '/uploads/nopic.jpg',
  `image2` varchar(60) DEFAULT '/uploads/nopic.jpg',
  `image3` varchar(60) DEFAULT '/uploads/nopic.jpg',
  `image4` varchar(60) DEFAULT '/uploads/nopic.jpg',
  `image5` varchar(250) DEFAULT '/uploads/nopic.jpg',
  PRIMARY KEY (`id`),
  UNIQUE KEY `id` (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1151 ;



INSERT INTO `items_test` (`id`, `image1`, `image2`, `image3`, `image4`, `image5`) VALUES
(802, 'uploads/item802_1.jpg', 'uploads/nopic.jpg', 'uploads/nopic.jpg', 'uploads/nopic.jpg', 'uploads/nopic.jpg'),
(802, '/uploads/products_good.jpg', '/uploads/products_good2.jpg', 'uploads/nopic.jpg', 'uploads/nopic.jpg', 'uploads/nopic.jpg'),
(891, 'uploads/new_image_1.jpg', 'uploads/nopic.jpg', 'uploads/nopic.jpg', 'uploads/nopic.jpg', 'uploads/nopic.jpg');

Open in new window

0
Comment
Question by:sabecs
  • 2
3 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 40400856
UPDATE items_test
SET
image1=CONCAT('/',image1)
WHERE
!(SUBSTR(image1, 1, 1)='/')

Open in new window

0
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 40400859
Forget that, didn't see multiple fields

UPDATE
  items_test
SET
  image1 = IF (!(SUBSTR(image1, 1, 1)='/'), CONCAT('/',image1),image1),
  image2 = IF (!(SUBSTR(image2, 1, 1)='/'), CONCAT('/',image2),image2),
  image3 = IF (!(SUBSTR(image3, 1, 1)='/'), CONCAT('/',image3),image3),
  image4 = IF (!(SUBSTR(image4, 1, 1)='/'), CONCAT('/',image4),image4),
  image5 = IF (!(SUBSTR(image5, 1, 1)='/'), CONCAT('/',image5),image5)

Open in new window

0
 

Author Closing Comment

by:sabecs
ID: 40400896
Thanks Gary, very much appreciated.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

A lot of articles have been written on splitting mysqldump and grabbing the required tables. A long while back, when Shlomi (http://code.openark.org/blog/mysql/on-restoring-a-single-table-from-mysqldump) had suggested a “sed” way, I actually shell …
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now