Hi! 🙂 Newbie to forum AND php/sql.
This is great idea to set aside a dedicated forum for newbies (and their endless repeated questions I'm sure) so as to not disrupt the main forum!
my problem:
updating data from one table to another.
database has 2 tables: friends, cars
both tables have a common column: name
friends table contains: fav_color
cars table contains: car_color
I want to replace all car_color with fav_color
And this is where I appear to be stuck.
I've scrounged around internet sites digging thru elementary examples and snippets here and there to formulate the following:
// test to prime/change color
mysql_query("UPDATE cars SET car_color = 'ugly color'");
$result = mysql_query("SELECT friends.name, friends.fav_color, cars.car_color ".
"FROM friends, cars ".
"WHERE friends.name = cars.name")
or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$rowid = $row['car_color'];
mysql_query("UPDATE cars SET car_color = fav_color WHERE car_color = $rowid");
// print out to confirm updates
echo $row['name']. ": ". '</br>' .
'fav color = ' . $row['fav_color'] . '</br>' .
'car color = ' . $row['car_color'] . '<P><P><P>';
}
Resulting output
Cathy:
fav color = Pink
car color = ugly color
Joe:
fav color = Blue
car color = ugly color
Mary:
fav color = Black
car color = ugly color
I've tried all sorts of things to get car_color to change in that WHERE loop, but just stumble. Help! 😕 Can't seem to get past this stage of learning process and am spinning wheels looking back @ the basics.