Jump to content

Insert into table based on matching values in 2 of 3 tables.


OldWest

Recommended Posts

I have 3 tables w/ the following fields:

 

Cities

id | state_id | city_name

 

States

id | state_name

 

New_cities

id | state_id | city_name

 

I am trying to write a script (and still working on it) that will INSERT new cities in cities WHERE New_cities.state_id = Cities.state_id.

 

Obviously I am still in testing and results sort out, but honest to all hell, I might be going about this all wrong, and would appreciate any comments, criticism, assistance or feedback on what I am trying to do.

 

<?php
  $query = "SELECT * FROM cities, states, new_cities";
  if ($results = mysqli_query($cxn, $query)) {
      $row_cnt = mysqli_num_rows($results);
      echo $row_cnt . " Total Records in Query.<br /><br />";
      if (mysqli_num_rows($results)) {
          while ($row = mysqli_fetch_array($results)) {
              if ($row['cities']['state_id'] == $row['new_cities']['new_state_id']) {
                  $insert_city_query = "INSERT INTO cities id, new_state_id, city_name VALUES
('','$new_state_id','$city_name')" or mysqli_error();
              } //if ($row['cities']['state_id'] == $row['new_cities']['new_state_id'])
              echo "$row[new_city_name]<br />";
          } //while ($row = mysqli_fetch_array($results))
      } //if (mysqli_num_rows($results))
  } //if ($results = mysqli_query($cxn, $query))
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.