Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6881617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:07:50+00:00 2026-05-27T05:07:50+00:00

I built two tables with the following SQL: CREATE TABLE state( IDstate INTEGER not

  • 0

I built two tables with the following SQL:

CREATE TABLE state(
   IDstate INTEGER not null auto_increment,
   state_name VARCHAR(40) not null,
   UNIQUE(state_name),
   CONSTRAINT IDstate PRIMARY KEY(IDstate)
)auto_increment=100;

CREATE TABLE city(
   IDcity INTEGER not null auto_increment,
   city_name VARCHAR(60) not null,
   IDstate INTEGER not null,
   CONSTRAINT IDcity PRIMARY KEY(IDcity),
   CONSTRAINT IDstate FOREIGN KEY(IDstate) REFERENCES state(IDstate)
)auto_increment=100;

I want to create a function with PHP-PDO-MySQL, I am sure about the next:

  1. My function receives two parameters: $state, $city.
  2. I have to avoid the repetition of values.

    $dbcon=new PDO('mysql:host=localhost; dbname=hotel','root', '');
    $sql = "SELECT IDstate FROM state WHERE state_name=:state_name";
    $query = $dbcon->prepare($sql);
    $query->execute(array(':state_name'=>$state_name));
    $array = $query->fetchAll();
    if ($query->rowCount() == 0){
      $sql = "INSERT INTO state (state_name) VALUES (:state_name);";
      $query = $dbcon->prepare($sql);
      $query->execute(array(':state_name'=>$state_name));
      $sql = "INSERT INTO city (city_name, IDstate) VALUES (:city_name, :IDstate);";
      $query = $dbcon->prepare($sql);
      $query->execute(array(':city_name'=>$city_name, ':IDstate'=>$dbcon->lastInsertId()));
    } else {
      $IDstate = $array[0]['IDstate'];
      $sql = "INSERT INTO city (city_name, IDstate) VALUES (:city_name, :IDstate)";
      $query = $dbcon->prepare($sql);
      $query->execute(array(':city_name'=>$city_name, ':IDstate'=>$IDstate));
      print_r($query->rowCount());
    }
    

I need to cover the following options:

  1. Insert new both state and city.
  2. Insert new city for an existing state.
  3. Avoid insert for both existing state and city.

The code above works fine, but I’d like improve it. I was reading about the best option is to write a transaction instead of the previous code.

On the other hand, I need to add a third table, maybe a fourth table, I think I am repeating a lot of code: boilerplate coding.

I would like your opinions and advice.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T05:07:51+00:00Added an answer on May 27, 2026 at 5:07 am

    So, this may not fully answer your question, but here are my thoughts:

    • if it works, don’t fix it.
    • What engine are you using? If you are using MySQL’s MyISAM tables, it will not support transactions and will not return an error.
    • Depending on how much volume you are expecting, what you have shouldn’t be too slow.
    • Add a limit to your select, since you’re only getting 1 state name. Limits have a big performance increase.
    • The longest US state name is 13 characters, not 40
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following two tables defined... CREATE TABLE [LogLevel] ( [Id] int primary
I have the following table: I am trying to create an SQL query that
So I have two tables that have a layout like (but not named): Table
If you create a Crystal Report using the built in wizard, tables are linked,
Trying to create a table for quantiles of the sum of two dependent random
My application is built in two sections. A C# executable which is the front
I have two arrays built while parsing a text file. The first contains the
I'm currently working on two projects simultaneously: My main project (built with maven) A
I have two webapps, that are built together and respectively provide a client and
Is there a built in function in .NET 2.0 that will take two arrays

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.