Solved

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

Posted on 2014-10-23
3
343 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
join tables 4 58
PHP & MySQL - Rounding Results from a Select Query 3 42
Using cfstoredproc to return query data 2 53
MYSQL responding very slow 3 51
Foreword This is an old article.  Instead of using the MySQL extension that was used in the original code examples, please choose one of the currently supported database extensions instead.  More information is available here: MySQLi / PDO (http://…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

730 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