Avatar of patrik20
patrik20
 asked on

Copy values from a column and then adding .jpg  to this value and paste it to the new column

Hi
I have added a new column to a table and I will copy the values from that a different column ( from the same table ) and then adding .jpg  to this values and paste it to the new column.
Can please somebody correct my database query and my phpscript.


SQL query on database not works:
 
UPDATE `profile_pic` SET `pic1` = `username` + `.jpg`
 
Even this script not works:
<?php 
require_once 'admin/serverdata.php';
$conn = mysql_connect("$dbHost","$dbUser","$dbPass");
$db = mysql_select_db("$dbName");
$result=mysql_query("select * from profile_pic");
$rowimage=mysql_fetch_assoc($result);
$pic1=$rowimage["username"];
$pic1jpg= $pic1.'.jpg';
$result= mysql_query("UPDATE profile_pic SET pic1 = '$pic1jpg'");
?>

Open in new window

PHPMySQL Server

Avatar of undefined
Last Comment
patrik20

8/22/2022 - Mon
StraySod

UPDATE `profile_pic` SET `pic1` = concat( `username`,`.jpg`)
StraySod

sorry, need apostrophes for .jpg
UPDATE `profile_pic` SET `pic1` = concat( `username`,'.jpg')

Open in new window

StraySod

in php script:

you need where clause in select statement to uniquely identify the row.
the way you do it, you always use the first row of result set
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
StraySod

and you need the where clause in update statement too, otherwise you'll update all rows in database table profile_pic to have the same value
patrik20

ASKER
Hi StraySod
Thanks for your reply, Your SQL ouery works fine, thanks.
About thr php script, I have two rows in this table username (uniq) and pic1. How shoud I have where clause in select and uppdate statment to make it works?
StraySod

you must know the username value in you script. should be sent by a form or something, don't know what exactly your source structure is etc. but if you could determine the username value, then you won't need any select statement.

Or do you want to update all columns? If so, do it like this:
<?php 
require_once 'admin/serverdata.php';
$conn = mysql_connect("$dbHost","$dbUser","$dbPass");
$db = mysql_select_db("$dbName");
$result=mysql_query("select * from profile_pic");
while ($rowimage=mysql_fetch_assoc($result)){
 $pic1=$rowimage["username"];
 $pic1jpg= $pic1.'.jpg';
 $result= mysql_query("UPDATE profile_pic SET pic1 = '$pic1jpg' WHERE username = '$pic1'");
}
?>

Open in new window

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
patrik20

ASKER
I have got error
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\dating\dating\run.php on line 6
line :
while ($rowimage=mysql_fetch_assoc($result)){
ASKER CERTIFIED SOLUTION
StraySod

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
patrik20

ASKER
same error:
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\dating\dating\run.php on line 6
patrik20

ASKER
I have changed other query´s name from result to result2, now everything is fine, Thank you
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy