Khaleesi Khaleesi - 2 months ago 13
PHP Question

Error with php variables, i don't understand

I have a problem with a var in my php code ..
I don't understand how

there is my code :

<fieldset>
<?
if(isset($_POST['requete']) && $_POST['requete'] != NULL){
include "connexion.inc";
$reponse = $bdd->query('SELECT id_piece, piece
FROM tbl_piece
WHERE piece = '.$requete.' ');
while($donnees = $reponse->fetch()){
echo $donnees['id_piece'];
}
}
else{
echo $requete;
}
?>
<form method="post" action="standard.php">
<input type="text" name="requete">
<input type="submit" value="Rechercher">
</form>
</fieldset>


So, on my web page, there is just a text box with a button to validate and " test " onto the textbox, yeh there i understand, my var is ok and all is alright my script is waiting my Keyword to find in my table ok
I write " AD202 " which is present in my table and click on my button
and then i get this error

Notice: Undefined variable: requete in /home/libtronic/www/html/standard.php on line 52


and .. i don't understand how to keep my var declared and assigned with my "AD202" keyword

Could you help me :/ ?

Answer

You try to use a variable without defining it first. Also, it would be safer and better practise to use prepared statements within your php code to avoid nasty surprises ( sql injection )

<fieldset>
    <?php
        if(isset($_POST['requete']) && $_POST['requete'] != NULL){
            include "connexion.inc";

            /* define the variable!! */
            $requete=$_POST['requete'];

            $reponse = $bdd->query('SELECT id_piece, piece 
                FROM tbl_piece 
                WHERE piece = '.$requete.' ');

            while($donnees = $reponse->fetch()){
                echo $donnees['id_piece'];
            }
        } else{
            echo $requete;
        }
    ?>
    <form method="post" action="standard.php">
        <input type="text" name="requete">
        <input type="submit" value="Rechercher">
    </form>
</fieldset>