Notice: Undefined variable [closed]

-2

I'm having this problem, the code shows this error:

  

Notice: Undefined variable: row in C: \ www \ crud-master \ form.php on line 33

MODELS:

<?php

try {

    $pdo = new PDO('mysql:host=localhost;dbname=CRUD_DATA', 'root', 'root');
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (Exception $e) {
    print "LOG de Erro {$e}";
}      

/*
    === Aqui temos uma conexao tipo SINGLETON, que garente que teremos apenas uma conexao durante todo o processo.
 *      Isso evita que muitas conexoes sejam abertas ocassionando lentidao no sistema
 */

form.php

<?php
require('./conn/conexao_PDO.php');


if ($_GET) {

    $sql = $pdo->query("SELECT * FROM pessoas WHERE id = {$_GET['id']}");

    if ($sql !== false) {
        //imprima
        foreach ($sql as $row) {

        }
    }
} elseif ($_POST) {
    $sql = "UPDATE pessoas set nome = '{$_POST['nome']}', email = '{$_POST['email']}', telefone = '{$_POST['telefone']}', endereco = '{$_POST['endereco']}'";
    $query = $pdo->query($sql);

    $sql = "SELECT * FROM pessoas where id = {$_POST['id']}";

    $query = $pdo->prepare($sql);
    $query->execute();
    $data = $query->fetch_assoc();
}
?>
<!DOCTYPE html>
<html lang="pt-br">
    <head>
        <?php require ('./header.php'); ?>
    </head>
    <body>
        <div class="container">
            <h2>ID:<?php echo $row['id']?></h2>
            <form class="form-horizontal" method="post" action="formulario.php">
                <div class="form-group">
                    <label class="control-label col-sm-2" for="nome">Nome:</label>
                    <div class="col-sm-10">
                        <input type="hidden" id="id" value="<?php echo $row['id']?>">

                        <input type="text" class="form-control" id="nome" placeholder="Como te lhamas ??" value="<?php echo $row['nome']?>">
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label col-sm-2" for="email">Email:</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" id="email" placeholder="Qual é o email pra enviar nudes ?" value="<?php echo $row['email']?>">
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label col-sm-2" for="telefone">Telefone:</label>
                    <div class="col-sm-10">
                        <input type="phone" class="form-control" id="telefone" placeholder="Numero do zap zap ?" value="<?php echo $row['telefone'];?>">
                    </div>
                </div>
                <div class="form-group">
                    <label class="control-label col-sm-2" for="endereco">Endereço:</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" id="endereco" placeholder="Endereço para os Presentes" value="<?php echo $row['endereco']?>">
                    </div>
                </div>

                <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                        <button type="submit" class="btn btn-warning">SALVAR ALTERAÇÃO</button>
                        <a href="index.php"><button type="button" class="btn btn-info">Voltar</button></a>
                    </div>
                </div>
            </form>
        </div>
    </body>
</html>
    
asked by anonymous 29.08.2017 / 16:42

2 answers

1

You have an undefined variable called $row .

What happens is that you assign the values of fetch() to variable $data and then call for a variable that was not created, $row . You have to change the name of your variable, or at the time of fetch() :

$data = $query->fetch_assoc();

for this:

$row = $query->fetch_assoc();

Or change all:

<?php echo $row['campo']?>

for this:

<?php echo $data['campo']?>
    
29.08.2017 / 16:58
-1

Try to get ['id'] from $row because no

foreach($sql as $row)

$row will receive the last property of the array. Then $row will be a string

    
29.08.2017 / 16:50