Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL query to insert form value into MySQL table
    primarykey
    data
    text
    <p>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.</p> <p>I have three tables:</p> <pre><code>|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 </code></pre> <p>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):</p> <pre><code>&lt;form action="address.php" method="post"&gt; &lt;h3&gt;Add ZIP Code&lt;/h3&gt; Zip Code:&lt;br /&gt; &lt;input type="text" name="zipCode" id="zipCode" value="" /&gt;&lt;br /&gt;&lt;br /&gt; City:&lt;br /&gt; &lt;select name="city" id="cityName"&gt; &lt;/select&gt; &lt;select name="state"&gt; &lt;?php foreach($rows as $row): ?&gt; &lt;option value="&lt;?php echo htmlentities($row['state'],ENT_QUOTES,'UTF-8');?&gt;"&gt;&lt;?php echo htmlentities($row['state'],ENT_QUOTES,'UTF-8');?&gt; &lt;/option&gt; &lt;?php endforeach; ?&gt; &lt;/select&gt; &lt;input type="submit" name="zipCode_submit" id="" value="Add Zip Code" /&gt; &lt;/form&gt; </code></pre> <p>And here's the php to handle the form:</p> <pre><code>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' =&gt; $_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' =&gt; $_POST['zipCode'], ':city' =&gt; $_POST['city'], ':state' =&gt; $_POST['state']); $stmt = DB\query($query, $query_params, $db); } </code></pre> <p>This is the bit of code I have to handle the query:</p> <pre><code>function query($query, $query_params, $db){ try{ $stmt = $db-&gt;prepare($query); $stmt-&gt;execute($query_params); return ($stmt-&gt;rowCount() &gt; 0) ? $stmt : false; }catch(PDOException $ex){ //On production remove .getMessage(). die("Failed to run query: " . $ex-&gt;getMessage()); } } </code></pre> <p>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:</p> <pre><code>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 </code></pre> <p>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...</p> <p>Again, any help is much appreciated. Thank you.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload