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.