无法从数据库中获取下拉值

时间:2019-02-18 05:44:53

标签: php mysql

因此,我想从数据库中获取数据并能够显示和更新它。我需要在下拉列表中显示数据库值的那一部分,但是仅显示下拉列表的第一个选项。

下面是完整的代码。

    <?php

  mysqli_connect('localhost', 'root', '');
  mysqli_select_db('storm');

  $_GET['id'];
  $ssh = $_GET['ssh'];
  $_GET['provi'];
  $_GET['impact'];
  $_GET['advice'];
  $_GET['date'];
  $_GET['typhoon'];
  $_GET['warning']; 

?>

<html>

<body>

  <div class="container">
    <form action="edit.php" method="post">

              <div class="form-group">
                <label for="prov">Provinces</label><br>
                <select id="prov" class="form-control" type="text" name="provi1">
                  <option value="Isabela"><?php echo $_GET['provi'];?></option>
                  <option value="La Union"><?php echo $_GET['provi'];?></option>
                  <option value="Pangasinan"><?php echo $_GET['provi'];?></option>
                  <option value="Ilocos Sur"><?php echo $_GET['provi'];?></option>
                  <option value="Ilocos Norte"><?php echo $_GET['provi'];?></option>
                </select>
              </div>

              <div class="form-group">
                <label>Date</label><br>
                <input class="w3-input w3-border form-control" type="date" name="date" value="">
              </div>

            <div class="col-md-6">
              <div class="form-group">
                <label>Typhoon Name</label><br>
                <input type="text" name="typhoon" value="<?php echo $_GET['typhoon']; ?>" class="form-control">
                <input type="hidden" name="id" value="">
              </div>
              <div class="form-group">
                <label>Warning #</label><br>
                <input type="text" name="warning" value="<?php echo $_GET['warning']; ?>" class="form-control">
              </div>
            </div>

          <div class="col">
            <div class="form-group">
              <input class="btnSubmit" type="submit" value="Update" name="submit" style="background-color: #408cff;">
              <input class="btnSubmit" type="reset" value="Cancel" style="background-color: #de5959;">
           </div>
          </div>

</form>

<?php
if(isset($_GET['submit'])){
  $id = $_GET['id'];
  $ssh = $_GET['ssh'];
  $muni = $_GET['muni'];
  $impact = $_GET['impact'];
  $advice = $_GET['advice'];
  $date = $_GET['date'];
  $typhoon = $_GET['typhoon'];
  $warning = $_GET['warning'];

  $query = "UPDATE twothree SET ssh='$ssh', muni='$muni', impact='$impact', advice='$advice', date='$date', typhoon='$typhoon', warning='$warning' WHERE id='$id'";
  $data = mysqli_query($conn, $query);
  if($data){
    echo "Record Updated Successfully!";
  }else{
    echo "Record Not Updated.";
  }
}
?>

我很确定我做错了什么。希望你们为我弄清楚。我对此并不陌生,希望我可以向你们学习。谢谢。

3 个答案:

答案 0 :(得分:2)

您可以在基本级别上使用类似的内容。这将从表中检索所有数据,并向其中添加一个下拉选项。

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    observeMessages(for: userID) { (messages) in
        self.messages = messages
        self.collectionView.reloadData()
    }
}

其中$ db是您的数据库连接,它必须是mysqli连接。

答案 1 :(得分:0)

您应该这样做,以显示省份列表。

<?php
$host = "localhost";
$user = "root";
$pwd = "";
$db = "storm";
$db_connection = new mysqli($host, $user, $pwd, $db);

if ($db_connection->connect_errno) {
    printf("Connect failed: %s\n", $db_connection->connect_error);
    exit();
}

//select provinces here
$provinces = $db_connection->query("Select * from provinces"); //change provinces according to your table name that you want to query.



?>
             <div class="form-group">
                <label for="prov">Provinces</label><br>
                <select id="prov" class="form-control" type="text" name="provi1">
                  <?php
                  while ($row = $provinces->fetch_object()){
                      echo '<option value="'.$row->province_name.'">'.$row->province_name.'</option>'; //change province_name according to your fieldname.
                  }
                  ?>
                </select>
              </div>

<?php
$db_connection->close();
?>

答案 2 :(得分:0)

您需要从数据库中循环数据以从数据库中获取连续数据,您可以使用它。

 <?php
$host = "localhost";
$user = "root";
$pwd = "";
$db = "storm";
$db_connection = new mysqli($host, $user, $pwd, $db);

if ($db_connection->connect_errno) {
    printf("Connect failed: %s\n", $db_connection->connect_error);
    exit();
}

//select provinces here
$query = mysqli_query($db_connection,"Select * from provinces");



?>
             <div class="form-group">
                <label for="prov">Provinces</label><br>
                <select id="prov" class="form-control" type="text" name="provi1">
                  <?php
                  while ($row = mysqli_fetch_assoc($query)){
                      echo '<option value="'.$row["province_name"].'">'.$row["province_name"].'</option>'; //change province_name according to your fieldname.
                  }
                  ?>
                </select>
              </div>

<?php
$db_connection->close();
?>

您的代码应如下所示