如何从下拉列表中获取值并将其插入MySQL Workbench?

时间:2019-04-05 16:39:19

标签: php mysql mysqli mysql-workbench

选择下拉列表中的一项后,所选的选项不会在MySQL Workbench(版本8)中显示。

下面提供的代码(包括php代码)在我的名为crimenewscrimetypes的表中,还有许多其他列,但是我的问题是所选的选项在MySQL Workbench中没有显示。

Connect.php

<?php 
      $server = "xxx.xxx.xxx.xxx"; //xxx=number
      $username = "xxx";
      $password = "xxx";
      $dbname = "xxx";
      //Establish database connection
      $conn = new mysqli($server, $username, $password, $dbname);
      if($conn->connect_error)
      {
        die("Connection failed: ". $conn->connect_error);
      }
?>

Front.php (主页)

已经包含connect.php

<form method="post" action="add.php"><center style="color: #FFFFC9 ; font-family: Arial">     
        <b>Category: </b> 
        <select name="catagory">
        <?php
            $type = "SELECT crime_type FROM crimetypes";
            $qry = mysqli_query($conn,$type);
            while($fin = mysqli_fetch_assoc($qry))
            {
        ?>  <option value="<?php echo($fin["crime_type"]);?>"><?php echo($fin["crime_type"]);?></option>
        <?php            
            }
        ?>                      
        </select>
        <b>URL: </b> <input type="url" name="url" placeholder="URL (www.)"><br><br>
        <b>Date: </b> <input type="datetime-local" name="datetime"><br><br>               
        <b>Latitude: </b> <input type="text" name="lat" placeholder="Latitude"><br><br>
        <b>Longitude: </b> <input type="text" name="lng" placeholder="Longitude"><br><br>
        <input type="submit" class="btn btn-success" value="Add"> //Submit button 
</form>

Add.php

已经包含connect.php

    //For adding news
    $category = $_POST['category']; 
    $url = $_POST['url'];
    $datetime = $_POST['datetime'];
    $lat = $_POST['lat'];
    $lng = $_POST['lng'];
    $sql = "INSERT INTO crimenews (crimenews_type, crimenews_url, crimenews_date, crimenews_locationLat, crimenews_locationLong) VALUES ('$category', '$url', '$datetime', '$lat', '$lng')";

    if($conn->query($sql) === TRUE)
    {
        header("location: front.php");
    }
    else
    {
        echo "Error!";
    }

MySQL Workbench中的犯罪类型表

    crimetype_id|crime_type
    ------------|-----------
            1   | A
            2   | B
                 .
                 .
                 .

MySQL Workbench中的crimenews表 (这确实发生了)

crimenews_id|crimenews_type|crimenews_url|crimenews_date|crimenews_locationLat|crimenews_locationLong
------------|--------------|-------------|--------------|---------------------|----------------------
        1   |              |             |              |                     |
        2   |              |             |              |                     |
                           .
                           .
                           .

这是MySQL Workbench(crimenews表)中的预期结果

crimenews_id|crimenews_type|crimenews_url|crimenews_date|crimenews_locationLat|crimenews_locationLong
------------|--------------|-------------|--------------|---------------------|----------------------
        1   |       A      |             |              |                     |
        2   |       B      |             |              |                     |
                           .
                           .
                           .

如何从crimetypes表中获得Crime_type中的值并插入Crimenews_type中的crimenews表中?

1 个答案:

答案 0 :(得分:-1)

您选择的名称为catagory,在$_POST中,您将其命名为category。您必须更改它。