SELECT pulling data from the Database

1

I'm not able to do a select that pulls the data from the Database, I wish someone could help me ...

<!DOCTYPE html>

<html>
    <head>
        <title>TODO supply a title</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
    <center>

        <h1>Adicionar ítem e categoria</h1>

        <form action="adicionar_item.php" method="post">
            <p><label for="item">ítem:</label>
            <input type="text" name="item" id="item" autofocus></p>


            <p><label for="categoria">Selecione a categoria:</label> <select name="categoria" id="categoria">
                <option></option>                
            <?php

            $sql="SELECT nome FROM categoria";
            $resultado=$conexao->query($sql);

            while($dados = $resultado->fetch_assoc()){
                echo "<option value=".$dados['nome'].">".$dados['nome']."</option>";
            }

            ?>
            </select></p>

            <input type="submit" value="Cadastrar">
        </form>

    </center>        
    </body>
</html>
    
asked by anonymous 20.06.2018 / 14:16

2 answers

0

Now the problem is the following, to add the item, I need only type what the item will be and select the category to which it will be added, I am now having difficulty inserting the item into the selected category, my code is so

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

$item=$_POST['item'];
$categoria=$_POST['categoria'];

$sql="INSERT INTO itens (item, id_cat) VALUES (\"$item\", \"$categoria\")";

$conexao->query($sql);

$linhasAfetadas=$conexao->affected_rows;

if($linhasAfetadas > 0){
    header("location:gerenciar.php");
} else {
    echo "<strong>Houve um erro ao registrar os itens e categoria!</strong>";
    echo "<a href=gerenciar.php>Voltar para a página anterior</a>";
}
    
20.06.2018 / 15:19
0

You did not connect to the Database, if you are using PDO, you should create a new connection:

$conexao = new PDO('mysql:host=servidor;dbname=bancodedados', 'usuario', 'senha')
    
20.06.2018 / 15:00