Kova Kova - 3 months ago 13
MySQL Question

Echo row from mysql table as results from html dropdown value

I have db table with three columns

id
,
club
and
percent
, and about a hundred rows.

First stage I want to populate html dropdown list with value from
club
column, and that is ok I did it, but in second stage I don't have any idea how to link value from the dropdown list with a specific row in table?

Any help would be appreciated.

This is code of first part which is ok:

<html>
<body>
<?php
include 'server.php';
$conn = new mysqli($servername, $username, $password, $dbname);

$sql = "SELECT id, club, percent FROM sometable";

?>
<form action="por.php" method="post">
<select name="home">
<?php
foreach ($conn->query($sql) as $row) {

echo "<option value=" .$row['id']. ">" .$row['club']. "</option>";

}
?>
</select>
<br><br>
<input type="submit">
</form>
</body>







EDIT
OK, I will try to be clearer:
1. this is my table:
table


  1. this is populated list with above code:
    enter image description here

  2. So, now I want to do this: if I choose e.g. Ciclon I want to diplay "Ciclon 55", or if I choose bolivar to display "Bolivar 37"...
    I know how to this if type $variable for each option, and in code for each option type if/elseif, but i want to populate list with value from table and later to display selected option, I dont want to type hundred and hundred lines.
    I hope its clear now.


Answer

I think this is what you needed.

<html>
<body>
<?php 
include 'server.php';
$conn = new mysqli($servername, $username, $password, $dbname);
if(isset($_POST['submit']))
{
 $home=$_POST['home'];
//To find the club and percent of the selected id
 $sql = "SELECT  club, percent FROM sometable where id=".$home; 
 $row=  $conn->query($sql);
 echo $row['club'].$row['percent']
}

$sql = "SELECT id, club, percent FROM sometable"; 

?>
 <form action="por.php" method="post">
    <select name="home">
        <?php                   
            foreach ($conn->query($sql) as $row) {

                echo "<option value=" .$row['id']. ">" .$row['club']. "</option>";  

            }
        ?>
    </select>
            <br><br>
    <input type="submit" name="submit">
</form>