0

I am trying to create a php form that inserts a zip code into my database and associates it with the user-selected city and state but I've been at it for almost two days and I am at wits end. Any help is greatly appreciated.

I have three tables:

|state            |
|state_id INT     | PK
|state VARCHAR(25)|

|city             |
|city_id INT      | PK
|city VARCHAR(45) |
|state_id INT     | PK FK

|zip_code         |
|zip_code_id INT  |
|zip_code VARCHAR(10) |
|city_id INT      | PK FK
|city_state_id INT| PK

I have a php form that has an input for the zip code and select boxes for the city and state (the city options are cascaded based on the state selection):

<form action="address.php" method="post">
<h3>Add ZIP Code</h3>
Zip Code:<br />
<input type="text" name="zipCode" id="zipCode" value="" /><br /><br />
City:<br />
<select name="city" id="cityName">
</select>
<select name="state">
<?php foreach($rows as $row): ?>
<option value="<?php echo htmlentities($row['state'],ENT_QUOTES,'UTF-8');?>"><?php echo htmlentities($row['state'],ENT_QUOTES,'UTF-8');?>
</option>
<?php endforeach; ?>
</select>
<input type="submit" name="zipCode_submit" id="" value="Add Zip Code" />
</form>

And here's the php to handle the form:

if (!empty($_POST['zipCode_submit'])){
if(empty($_POST['zipCode'])){
    die("Please enter a ZIP Code.");
}

if (empty($_POST['city']) || empty($_POST['state'])){
    die("Please select a city and a state.");
}

//Check if zip code is already in database
    $query = "SELECT 1 FROM zip_code WHERE zip_code = :zip_code";
    $query_params = array(':zip_code' => $_POST['zipCode']);

    $stmt = DB\query($query, $query_params, $db);

    if($stmt){
        die("This ZIP code is already entered.");
    }

//Insert zip code into database
    $query = "SOME Magical SQL query that I cannot seem to figure out...";
    $query_params = array(':zipCode' => $_POST['zipCode'], ':city' => $_POST['city'], ':state' => $_POST['state']);

    $stmt = DB\query($query, $query_params, $db);
}

This is the bit of code I have to handle the query:

function query($query, $query_params, $db){
try{
$stmt = $db->prepare($query);
$stmt->execute($query_params);
return ($stmt->rowCount() > 0) ? $stmt : false;
}catch(PDOException $ex){
    //On production remove .getMessage().
    die("Failed to run query: " . $ex->getMessage());
}
}

As far as the magical SQL query that I cannot seem to figure out, I tried several different queries (I'm relatively new to MySQL, so I haven't tried anything too advanced). But this is the closest that I think that I've gotten:

INSERT INTO zip_code (zip_code, city_city_id, city_state_state_id) SELECT zip_code, city_city_id, city_state_state_id
FROM zip_code 
JOIN (city, state)
ON zip_code.city_city_id = city.city_id
AND zip_code.city_state_state_id = state.state_id
WHERE zip_code.zip_code = :zipCode
AND city.city = :city
AND state.state = :state

Again, I'm trying to create the INSERT query that uses the data from the zip code input field along with the names of the city and state that the user selects. I can't seem to wrap my head around how to pair up the names of the city and state with the city and state id inside a query that is selecting them from other tables...

Again, any help is much appreciated. Thank you.

2 Answers 2

1

Instead of giving state name for <option value="">, give state_id in that. Then it is done.

<select name="state">
   <?php foreach($rows as $row): ?>
      <option value="<?php echo $row['state_id']; ?>"><?php echo htmlentities($row['state'],ENT_QUOTES,'UTF-8');?>
      </option>
   <?php endforeach; ?>
</select>

While you are submitting, the form will post the selected state's id to that page. So you don't need to compate the state name and get the state id again. You can use it directly. It is similar in the case of city also

Sign up to request clarification or add additional context in comments.

Comments

1

Try this

$city=$_POST["CITY_ID"];
$state_id=$_POST["STATE_ID"]

Put select zip code values directly in select command like if zipcode is 8 then

INSERT INTO zip_code (zip_code, city_city_id, city_state_state_id)
(SELECT 8,city.city_id,state.state_id FROM city,state where city.state_id=state.state_id and city.city_id='$city'and state_id='$state_id')

3 Comments

When I try the query you suggested, it is successful (no errors) but no rows are inserted. What am I doing wrong?
SELECT 8,city.city_id,state.state_id FROM city,state where city.state_id=state.state_id and city.city_id='$city'and state_id='$state_id' run this query and check wether any rows return or not
The code below returns rows. Now I'm just trying to create the INSERT statement based on this: SELECT zip_code.zip_code_id, zip_code.zip_code, zip_code.city_id, zip_code.state_id FROM zip_code JOIN (city, state) ON zip_code.city_id = city.city_id AND zip_code._state_id = state.state_id WHERE zip_code.zip_code = :zip_code AND city.city_id = :city AND state.state_id = :state

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.