Jump to content

Recommended Posts

Hi, I am trying to use this code to take all the unique town names from one table and populate a new table with those towns.

 

this code doesnt add town names to the new table and deletes any that are there with blank space - any clues?

 

 


mysql_select_db($database_contractors, $contractors);
$query_Recordset3 = "SELECT DISTINCT est_town FROM hotels";
$res3 = mysql_query($query_Recordset3, $contractors) or die(mysql_error());





$num_rows = mysql_num_rows($res3);
echo $num_rows . "<br>";

while( $row = mysql_fetch_assoc($res3) ) {

  $town = $row['est_town'];
  echo $town . "<br />" ;

  mysql_query('UPDATE town_coords SET town_name = '.$town.'');

}



Link to comment
https://forums.phpfreaks.com/topic/255880-update-database-problem/
Share on other sites

NEVER run queries in loops! Anyway, you state you are trying to populate (i.e. INSERT) records into the second table. BUt, you are using an UPDATE query. An UPDATE query will 'update' existing records. Since, you didn't provide a WHERE clause on the update query it is attempting to update ALL the records in the table. And, apparently, the value you are using is an empty string. So, all the records are being updated with an empty string.

 

Based upon what you are trying to do, however, this should be a ONE-TIME operation. Once you have a table with the unique towns you should use the primary key in that table as a foreign key in the hotels table. Then you would never store the town names in the hotels table.

 

To accomplish the import of the unique town names into the new table you should first set the 'town_name' to be a unique field. Then you need just one query to import the all the new values

INSERT INTO town_coords (town_name)
SELECT DISTINCT est_town FROM hotels

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.