Problem with $ _GET

0

*** I have the following PHP file to connect to a database in MySQL: Connection.php

<?php
$Link = 'mysql:host=localhost;dbname=yt_colores';
$Usuario = 'root';
$Password = 'root';
try{
    $PDO = new PDO($Link, $Usuario, $Password);
} catch (PDOException $e) {
    print "¡Error!: " . $e->getMessage() . "<br/>";
    die();
}

*** I also have a PHP file to edit in my database: Edit.php

<?php
$Id = $_GET['Id'];
$Color = $_GET['Color'];
$Descripcion = $_GET['Descripcion'];
include_once 'Conexion.php';
$SQL_Editar = 'UPDATE Colores SET Color = ?, Descripcion = ? WHERE Id = ?';
$Sentencia_Editar = $PDO -> prepare($SQL_Editar);
$Sentencia_Editar -> execute(array($Color, $Descripcion, $Id));
header('location:Index.php');

*** From my Index.php values of the variable Id when clicking on a FontAwesome icon but var_dump ($ Unique_Product) throws me bool (false) instead of showing me the information of a specific record: Index.php

<?php
include_once 'Conexion.php';
$SQL_Leer = 'SELECT * FROM Colores';
$GSent = $PDO -> prepare($SQL_Leer);
$GSent -> execute();
$Resultado = $GSent -> fetchAll();
// *** AGREGAR
if($_POST){
    $Color = $_POST['Color'];
    $Descripcion = $_POST['Descripcion'];
    $SQL_Agregar = 'INSERT INTO Colores (Color, Descripcion) VALUES (?, ?)';
    $Sentencia_Agregar = $PDO -> prepare($SQL_Agregar);
    $Sentencia_Agregar -> execute(array($Color,$Descripcion));
    header('location:Index.php');
}
// *** EDITAR
if($_GET){
    $Id = $_GET['Id'];
    $SQL_Unico = 'SELECT * FROM Colores WHEN Id = ?';
    $GSent_Unico = $PDO -> prepare($SQL_Unico);
    $GSent_Unico -> execute(array($Id));
    $Resultado_Unico = $GSent_Unico -> fetch();
    var_dump($Resultado_Unico);
}
?>
<!doctype html>
<html lang="en">
    <head>
        <!-- Required meta tags -->
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
        <!-- Bootstrap CSS -->
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css" integrity="sha384-Smlep5jCw/wG7hdkwQ/Z5nLIefveQRIY9nfy6xoR1uRYBtpZgI6339F5dgvm/e9B" crossorigin="anonymous">
        <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.1.1/css/all.css" integrity="sha384-O8whS3fhG2OnA5Kas0Y9l3cfpmYjapjI0E4theH4iuMD+pLhbf6JI0jIMfYcK3yZ" crossorigin="anonymous">
        <title></title>
    </head>
    <body>
        <div class="container mt-5">
            <div class="row">
                <div class="col-md-6">
                    <?php foreach($Resultado as $Dato): ?>
                    <div 
                    class="alert alert-<?php echo $Dato['Color']?> text-uppercase"
                    role="alert">
                        <?php echo $Dato['Color']; ?>
                        -
                        <?php echo $Dato['Descripcion']; ?>
                        <a href="Index.php?Id=<?php echo $Dato['Id']?>" class="float-right">
                            <i class="fas fa-pencil-alt"></i>
                        </a>
                    </div>
                    <?php endforeach ?>
                </div>
                <div class="col-md-6">
                    <!-- Si NO hay un método GET (Editar) no muestra el formulario -->
                    <?php if(!$_GET): ?>
                    <h2>AGREGAR ELEMENTOS</h2>
                    <form method="POST">
                        <input type="text" class="form-control" name="Color">
                        <input type="text" class="form-control mt-3" name="Descripcion">
                        <button class="btn btn-primary mt-3">Agregar</button>
                    </form>
                    <?php endif ?>
                    <!-- Si hay un método GET (Editar) muestra el formulario -->
                    <?php if($_GET): ?>
                    <h2>EDITAR ELEMENTOS</h2>
                    <form method="GET" action="Editar.php">
                        <input type="text" class="form-control" name="Color"
                        value="<?php echo $Resultado_Unico['Color'] ?>">
                        <input type="text" class="form-control mt-3" name="Descripcion"
                        value="<?php echo $Resultado_Unico['Descripcion'] ?>">
                        <button class="btn btn-primary mt-3">Agregar</button>
                    </form>
                    <?php endif ?>
                </div>
            </div>
        </div>
        <!-- Optional JavaScript -->
        <!-- jQuery first, then Popper.js, then Bootstrap JS -->
        <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/js/bootstrap.min.js" integrity="sha384-o+RDsa0aLu++PJvFqy8fFScvbHFLtbvScb8AjopnFD+iEQ7wo/CG0xlczd+2O/em" crossorigin="anonymous"></script>
    </body>
</html>

Please help me because I have two days seeing what I have wrong or what is more but I do not know what it is. I'm just learning PHP; Any orientation is welcome. Thanks

Does not work: var_dump ($ Single_Proceed) Message: bool (false) Expected result: Show the indicated record on screen

    
asked by roberthung 26.07.2018 в 21:15
source

1 answer

1

You have a syntax error in this friend line

$SQL_Unico = 'SELECT * FROM Colores WHEN Id = ?';

It should be like this

$SQL_Unico = 'SELECT * FROM Colores WHERE Id = ?';
    
answered by 26.07.2018 / 21:27
source