Link to home
Create AccountLog in
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

Avatar of StraySod
StraySod
Flag of Czechia image

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

Open in new window

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
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
Avatar of patrik20
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?
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

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
Avatar of StraySod
StraySod
Flag of Czechia image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
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
I have changed other query´s name from result to result2, now everything is fine, Thank you