form read database

Asked

Viewed 334 times

2

Hello Folks I am trying to make a form read Mysql database options, however the code line below shows me only the word "Array".

What is my mistake in this line? Can anyone 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 of the table is always displayed, and I need everyone to appear. I appreciate any help.

2 answers

1


This is because the $Resp variable in your code is an array, which was returned by the function mysqli_fetch_row and when trying to print on the screen a Array, PHP prints a string 'Array', not what it contains.

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

<?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>";
?>
  • 1

    Thanks your tips were very useful, but it didn’t work at first. But I switched ($Resp = mysqli_fetch_array($sql)) to ($Resp = mysqli_fetch_row($sql)) and ran perfectly.

  • Hi Amigo had the problem after, the form only displayed the last table item

0

Solve all problems by changing the field 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 locates the first informed data.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.