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))
?>

Link to comment
Share on other sites

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.