Notice: Undefined variable

Asked

Viewed 890 times

-2

I have this problem, the code shows this error:

Notice: Undefined variable: Row in C: www crud-master formulario.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
 */

php form.

<?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>
  • 2

    Undefined variable $Row. You assign the values of fetch() to $date and not $Row.You have to change the name of your variable.

2 answers

1

You have an undefined variable called $row.

What happens is that you assign the values of fetch() the variable $data and then calls 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 that reason:

$row = $query->fetch_assoc();

Or change all the:

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

for that reason:

<?php echo $data['campo']?>
  • I switched to $Row = $query->fetch_assoc();, plus the error continues.

  • I wanted to ask, what is the logic of if($_GET), if($_POST)? Because you check if they are true, then you can’t even know which one you’re going to enter. Do you know which ifs you’re going to enter? @Michelspirlandeli

  • Depending, it may be that you enter $_GET and this if is not programmed $Row = $query->fetch_assoc();. Close to if ($sql !== false) { //prints foreach ($sql as $Row) {

  • So I couldn’t solve it I ended up changing my code,.

-1

Try to get the ['id'] of $row because in the

foreach($sql as $row)

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

  • 3

    Review your answer. It doesn’t fit the question at all.

Browser other questions tagged

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