Ygor Montenegro Ygor Montenegro - 1 month ago 6
MySQL Question

Fetch array for dropdown list with MySQL data in a form

How can i start it? I know that i need to make an mysql query, but how to transform data, in options in the dropdown list. And remember, it's inside a form, to send the result from a MySQL table.

(EDIT)

I am working inside a printf That is what i want:

<?php ob_start();
include('/../../config.php');

if(isset($_POST['edit_id']) && !empty($_POST['edit_id'])) {

$edit_id = mysql_real_escape_string($_POST['edit_id']);
$result = mysql_query("SELECT username, password, nome, cidade, pais, base, isactive, admin, dov, checador, dinheiro, email, datanascimento, profissao, idivao, idvatsim, horas, rank FROM acars_users WHERE `id`='".$edit_id."'");
$resultdl = mysql_query("SELECT * FROM acars_hubs");

$data = mysql_fetch_array($result);
$dl = mysql_fetch_array($resultdl);


printf("<div align=\"center\">
<br><form method=\"post\" action=\"editar2.php\">
<p><font size=\"2\" face=\"Segoe UI, Arial, Helvetica, sans-serif\" align=\"center\">Modifique os campos que deseja para <strong>editar este membro.</font><br>
<br>

<table width=\"700\" border=\"0\" align=\"center\" >
<tr>
<td>Base Operacional:</td>
<td><label for=\"hub\"></label>
<select name=\"hub\">
<option>".$dl['name']."</option>
</select>
</td>
</td>
</tr>
</table></br></br>
<input name=\"edit_id\" value=\"$edit_id\" type=\"hidden\">
<input type=\"image\" src=\"img/Editar.PNG\" width='85' height='30'></form>
</form>

</table>
</div>

");
while ($data = mysql_fetch_array($result));
while ($dl = mysql_fetch_array($resultdl));
ob_end_flush();
?>

Answer

I've discovered the problem. My first $dl = mysql_fetch_array($resultdl, MYSQL_ASSOC); pulls the first row from the table. When I start while loop the mysql_fetch_array picks up with the next row. So, the solution is remove the first call. This is the final code:

<?php 
   include('../../../../../config.php'); 

    if(isset($_POST['edit_id']) && !empty($_POST['edit_id'])) {

    $edit_id = mysql_real_escape_string($_POST['edit_id']); 

    $result = mysql_query("SELECT * FROM acars_users WHERE `id`='".$edit_id."'");
    $data = mysql_fetch_array($result);
    $resultdl = mysql_query("SELECT * FROM acars_hubs");

    printf("<div  align=\"center\">
    <form method=\"post\" action=\"actions/actions_editar.php\">
    ");

           while ($dl = mysql_fetch_array($resultdl, MYSQL_ASSOC)){

        printf(" 
        <option value=".$dl["id"].">".$dl["name"]."</option>;
        ");

           }

    printf("
        <input name=\"edit_id\" value=\"$edit_id\" type=\"hidden\">
        <input type=\"image\" src=\"../../images/botao_editar.PNG\" width='85' height='30'></form>
        ");

?>
Comments