I'm a complete newbie with PHP/html stuff so please bear with me. I've been trying to populate a select box using data from a myslq database and I can't get it to work, all I have is just a blank textBox. This is what I have for now:
<select name="cargo">
<?php
require("conectadb.php");
$ok = conecta_db() or die ("Failure");
$sql = mysqli_query($ok, "SELECT descCargo FROM tbcargo");
while ($row = mysqli_fetch_array($sql)){
$c = $row['descCargo'];
echo("<option value=\"$c\">$c</option>");
}
?>
</select>
This is my database structure for now, with all the relevant rows:
Database name: tbcargo
PkCodCargo (primary key, AUTO_INCREMENT)
descCargo (what I want to fill the dropdown with)
I've tried everything I could think of to no avail, unfortunately. Can someone help to point out exactly what am I doing wrong here?
Thanks in advance!
Here is the solution.
# here database details
mysql_connect('hostname', 'username', 'password');
mysql_select_db('database-name');
$sql = "SELECT descCargo FROM tbcargo";
$result = mysql_query($sql);
echo "<select name='cargo'>";
while ($row = mysql_fetch_array($result)) {
echo '<option value="'. $row['descCargo'] .'">'.$row['descCargo'] .'</option>';
}
echo "</select>";
Update the part of your code to:
<select name= 'cargo'>
<?php
require("conectadb.php");
$ok = conecta_db() or die ("Failure");
$sql = mysqli_query($ok, "SELECT descCargo FROM tbcargo");
while ($rows = $sql->fetch_assoc())
{
echo '<option value="'.$rows['descCargo'].'">'.$rows['descCargo'].'</option>';
}
?>
</select>
I've finally found out the issue. My file was named .html instead of .php and that's why nothing ever worked.
Hope this is able to help another newbie like myself out there
Cheers!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.