2

So I want to fetch my data from Database and be able to display and update it. Im on the part where I have to display a value from the db on a Dropdown but only the first option of the dropdown is being displayed.

Below is complete code.

    <?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.";
  }
}
?>

I'm pretty sure that I am doing something wrong. Hope you guys figure it out for me. I'm new to this and I hope that I can learn from you guys. Thanks.

8
  • What you are trying to achieve?can you you explain bit more about your problem? Commented Feb 18, 2019 at 5:54
  • 1
    what will be in dropdown what you fetch? is there any ajax call? what is purpose of code? where is database code? is this core php? Commented Feb 18, 2019 at 6:01
  • please provide full code and also explain what you are trying to achieve. Commented Feb 18, 2019 at 6:05
  • 1
    if you provide parameter like... URL?provi = "desteen"; then... $_GET['provi'] will display desteen... there is nothing related with you database. for database you need to make database connection then you need to write a fetch query. to get data from database. Commented Feb 18, 2019 at 6:08
  • See about sql injection and the importance of prepared and bound queries Commented Feb 18, 2019 at 7:11

3 Answers 3

2

You can use something like this at basic level. this will retrieve all data from table and add a dropdown option to it.

    <select class="" name="" required>
      <option value="" selected disabled>Select a option</option>
      <?php
      $select_1 = $db->query("SELECT * FROM table");
      while ($row_1 = $select_1->fetch_assoc()) {
      ?>
         <option value="<?php echo $row_1['value']; ?>">
             <?php echo $row_1['name']; ?>
         </option>
      <?php } ?>
   </select>

where $db is your database connection and it must be mysqli connection.

Sign up to request clarification or add additional context in comments.

4 Comments

I don't quite understand your codes. I edited my post please refer to it. Thank you.
first of all you submitting the form as POST method and you trying to retrieve those from GET method. you should use $_POST or $_REQUEST for that. Then your database connection must assign to a variable like $db = new mysqli(host,user,password,database); Then in my code use select query to retrieve data that you want to retrieve changing table and adding where condition to it.
what does $select_1 and $row_1 stands for?
$select_1 is the variable that store the output object of the mysql query and the $row_1 is the variable that store the fetch array data of looping $select_1 data.
0

You should do like this, to display the list of provinces.

<?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();
?>

Comments

0

bro you need to loop data form your database to get continuous data out of your database you could use this.

 <?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();
?>

you code should look like this

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.