0

I want to get a value from mysql populated in a drop down list. And then, the drop down list will be inserted in a table cell <td>. But I don't know how to arrange the codes. The below code didn't work. I hope you can help me correct it.

<?php
include("connection.php");
?>
<?php
$result=mysql_query("SELECT * FROM peralatansukan");

$count=mysql_num_rows($result);
echo"<select name=dropdown value=''>Dropdown</option>";
echo  "<table width='50%' border='1'>";
echo"<tr>";
echo"<td align='center'><b><font color='black'>No.</font></b></td>";
echo"<td align='center'><b><font color='black'>Peralatan Sukan</font></b>  
</td>";
echo"<td align='center'><b><font color='black'>Kuantiti</font></b></td>";
echo"</tr>";

if($count==0){
    echo "no record found";
}
else {
    while($row=mysql_fetch_array($result)){
        echo "<tr>";
        echo "<td align='center'><font color='black'>".$row["no"]."</font>  
        </td>";
        echo "<td align='center'><font  
        color='black'>".$row["peralatansukan"]."</font></td>";
        echo "<td align='center'>"."<option value=$row[kuantiti]></option>"." 
        </td>";
    }
    echo "</select>";
}

?>
Donald Duck
  • 8,409
  • 22
  • 75
  • 99
ila dila
  • 3
  • 1
  • 5
  • 3
    [**Please, don't use `mysql_*` functions in new code**](http://stackoverflow.com/q/12859942). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [**red box**](http://php.net/mysql-connect)? Learn about [*prepared statements*](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://php.net/manual/en/mysqlinfo.api.choosing.php) can help you decide which. – Qirel Jan 31 '17 at 15:42
  • 4
    From the looks of the HTML you're trying to produce, it sounds like you could use some tutorials on the basics of HTML. – David Jan 31 '17 at 15:43
  • hmm..I don't know how to use them.. – ila dila Jan 31 '17 at 15:48
  • 1
    `` in `
    – Funk Forty Niner Jan 31 '17 at 15:51
  • yes it could be.. i did it before...but, using html code only. no using php code at that time. – ila dila Jan 31 '17 at 15:56

2 Answers2

0

Try to take a look at the html select tag. http://www.w3schools.com/tags/tag_select.asp.

The only element allowed inside select is option tags.

<select>
    <option>option 1</option>
    <option>option 2</option>
    <option>option 3</option>
</select>

As Qirel mentioned: you should not use the deprecated mysql_* functions. Using mysqli you could do something like this instead.

<?php
// connect to the database
$mysqli = new mysqli($host, $username, $password, $database);

// select rows
$result = $mysqli->query("SELECT * FROM peralatansukan");

// display dropdown
echo '<select>';
foreach($result->fetch_assoc() as $row){
    echo '<option>' . $row['columnName'] . '</option>';
}

echo '</select>';
Thomas
  • 537
  • 1
  • 4
  • 14
0

I did it.. But, mysql is not so greater than the mysqli as u all stated before. Maybe I should learn the PDO n mysqli sometimes. And I hope there's another way to learn them in a short time n an easy way.

<?php
include("connection.php");

$result=mysql_query("SELECT * FROM peralatansukan");

$count=mysql_num_rows($result);

 echo  "<table width='50%' border='1'>";
   echo"<tr>";
   echo"<td align='center'><b><font color='black'>No.</font></b></td>";
   echo"<td align='center'><b><font color='black'>Peralatan Sukan</font></b>   
   </td>";
   echo"<td align='center'><b><font color='black'>Kuantiti</font></b></td>";
   echo"</tr>";

   if($count==0)
   {echo "no record found";}
   else {
       while($row=mysql_fetch_array($result))
       {
       echo "<tr>";
       echo "<td align='center'><font color='black'>".$row["no"]."</font>      
       </td>";
       echo "<td align='center'><font  
       color='black'>".$row["peralatansukan"]."</font></td>";
       echo "<td align='center'><select name=dropdown  
       value=''>Dropdown</option><option value=$row[kuantiti]>$row[kuantiti] 
       </option></td>";
       }
       echo "</select>";
       }

       ?>
ila dila
  • 3
  • 1
  • 5