douhun7609 2013-04-19 05:16
浏览 44
已采纳

SQL查询将表单值插入MySQL表

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条回答 默认 最新

  • dsla94915 2013-04-19 05:24
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?