form read database

2

Hello everyone, I'm trying to make a form read MySql database options, but the line of code below shows me only the word "Array".

What is my error on this line? Can someone help me.

<form id="cadastro" action= "cadastroprodutos.php" method="post">
<p>Codigo<input type="text" name="codigo"size='14' maxlength="14"   placeholder="Codigo"/></p>
Descrição<input type="text" name="descricao"size='50' maxlength="50" placeholder="Descrição"/>
<p>Cores<input type="text" name="cor"size="12" maxlength="12"  placeholder="Cor"/>
Grupo <input list="grupo" name="grupo" size="10"/>

<?php
  $sql= mysqli_query($conn,"select tipo from grupodeprodutos order by tipo");
    $resp=mysqli_fetch_array($sql);
      echo "<datalist id='grupo'><option value='$resp'></datalist>";

?>
Marca<input type="text" name="marca"size='20' maxlength="20" placeholder="Marca"/>
Preço<input type="text" name="preco"size='20' maxlength="20" placeholder="Preço"/></p>
<fieldset><legend>Campos Exclusivos para Moveis</legend> 
<p>Valor de montagem:<input type="text" name="valorm"size='6'maxlength="6" placeholder="Valor de Montagem" value='' OnKeyPress="formatar('R$###,##' this)"/></fieldset></p>
<input type="submit"id="botao" name="botaoo" value="Salvar"/>
</form>

I modified the code for this pattern. As explained below.

Grupo <input list="grupo" name="grupo" size="10"/>
<?php
$sql= mysqli_query($conn,"select tipo from grupodeprodutos order by tipo");
while ($resp = mysqli_fetch_row($sql)) {

    echo "<datalist id='grupo'><option value='" . $resp[0] . "'>    </datalist>";
}
?>

Now the data is collected, but the last item in the table is always displayed, and I need everyone to appear. Thanks for any help.

    
asked by anonymous 07.05.2016 / 17:58

2 answers

1

This is because the $ resp variable of your code is an array, which was returned by the mysqli_fetch_row function, and when you try to print a Array on the screen, PHP printed an 'Array' string, not what it contains.

To print the result on the screen, according to your code, you can do this using the while loop:

<?php
    $sql = mysqli_query($conn,"select tipo from grupodeprodutos order by tipo");

    echo "<datalist id='grupo'>";

    while ($resp = mysqli_fetch_row($sql)) {
        echo "<option value='" . $resp[0] . "'>";
    }

    echo "</datalist>";
?>
    
07.05.2016 / 18:10
0

Solve all problems by changing the field format to:

<select name="grupo"/>
<?php
$sql= mysqli_query($conn,"select tipo from grupodeprodutos order by tipo");
while ($resp = mysqli_fetch_array($sql)) {

     $group=$resp['tipo'];

    echo "<option value='$group'>$group</option>";
}

?>
</select>

In the old form the function executes, but the <datalist> only finds the first information given.

    
08.05.2016 / 03:33