-4

This is a php code i wrote to edit a line in an array "article" every line is compsed by:

(Colonne:     Type)            
(ref     : int(11),
nom   :varchar(25),
cat :  varchar(25),
prix     : int(11),
fabricant : varchar(35),
photo     :varchar(35))

so i make this php code:

      <?php
require_once("connection.php");
$ref=$_POST['ref'];
$req="Select * from article where ('ref =". $ref."')";
$rs= mysql_query($req) or die(mysql_error());
$AR=mysql_fetch_assoc($rs);

?>
    <html>
    <head>
        <meta charset="utf-8">
    </head>
    <body>
    <form method="POST" action="modifierArticle.php" enctype="multipart/form-data">
        <table>
            <tr>
                <td>Référence:</td>
                <td><input type="text" name="ref" value="<?php echo ($AR['ref']) ?>" readonly="true"></td>
            </tr>
            <tr>
                <td>Nom:</td>
                <td><input type="text" name="nom" value="<?php echo ($AR['nom']) ?>"></td>
            </tr>
            <tr>
                <td> Catégorie:  </td>
                <td><input type="text" name="cat" value="<?php echo ($AR['cat']) ?>"></td>
            </tr>
            <tr>
                <td> Prix Unitaire:  </td>
                <td><input type="text" name="prix" value="<?php echo ($AR['prix']) ?>"></td>
            </tr>
            <tr>
                <td> Fabricant:  </td>
                <td><input type="text" name="fab" value="<?php echo ($AR['fabricant']) ?>"></td>
            </tr>
            <tr>
                <td>Photo: </td>
                <td><input type="file" name="photo"><img src="./images/<?php echo ($AR['photo']) ?>"</td>
            </tr>
            <tr>
                <td></td>
                <td> <input type="submit" value="Enregistrer"></td>
            </tr>
        </table>

    </form>
    </body>
    </html>
<?php
mysql_free_result($rs);
mysql_close($conn);

?>


so i got an error:

Notice: Undefined index: ref in ...\editerArticle.php on line 3

Notice: Undefined index: ref in ...\editerArticle.php on line 3

M.chaudhry
  • 651
  • 1
  • 6
  • 13

5 Answers5

0
if(isset($_POST['ref']))
{
    $ref=$_POST['ref'];
    $req="Select * from article where ref = $ref";
    $rs= mysql_query($req) or die(mysql_error());
    $AR=mysql_fetch_assoc($rs);
}

You had a syntax error in $req add the if condition to make sure that $_POST['ref'] isset before running the query

HTML

<html>
    <had>
        <meta charset="utf-8">
    </had>
    <body>
    <form method="POST" action="modifierArticle.php" enctype="multipart/form-data">
        <table>
            <tr>
                <td>Référence:</td>
                <td><input type="text" name="ref" value="<?php echo $AR['ref']; ?>" readonly="true"></td>
            </tr>
            <tr>
                <td>Nom:</td>
                <td><input type="text" name="nom" value="<?php echo $AR['nom']; ?>"></td>
            </tr>
            <tr>
                <td> Catégorie:  </td>
                <td><input type="text" name="cat" value="<?php echo ;AR['cat']; ?>"></td>
            </tr>
            <tr>
                <td> Prix Unitaire:  </td>
                <td><input type="text" name="prix" value="<?php echo $AR['prix']; ?>"></td>
            </tr>
            <tr>
                <td> Fabricant:  </td>
                <td><input type="text" name="fab" value="<?php echo $AR['fabricant']; ?>"></td>
            </tr>
            <tr>
                <td>Photo: </td>
                <td><input type="file" name="photo"><img src="../images/<?php echo $AR['photo']; ?>"</td>
            </tr>
            <tr>
                <td></td>
                <td> <input type="submit" value="Enregistrer"></td>
            </tr>
        </table>

    </form>
    </body>
    </html>
Tabby
  • 388
  • 1
  • 11
  • Why use `!empty()`? This way `ref` can't be any _falsey_ value – kero Mar 26 '14 at 11:25
  • @kingkero something the user just enters data into the input field and then erases it, in this process `ref` may be set but it will be empty to avoid this i used `!empty()` – Tabby Mar 26 '14 at 11:27
  • If `ref` is `"0"`, `empty()` will return true and your logic fails - so users can't search for 0? – kero Mar 26 '14 at 11:39
  • @kingkero you are right my bad. Now is the logic right? – Tabby Mar 26 '14 at 11:50
  • Yes. Also `isSet()` and `empty()` at the same time makes no sense because `empty()` will check first if the variable is set ;) So it usually is an either or choice – kero Mar 26 '14 at 16:22
0

Use this code

<?php
require_once("connection.php");
$ref=$_POST['ref'];
$req="Select * from article where ref =$ref";
$rs= mysql_query($req) or die(mysql_error());

?>
    <html>
    <had>
        <meta charset="utf-8">
    </had>
    <body>
    <form method="POST" action="modifierArticle.php" enctype="multipart/form-data">
        <table>
            <?php while($AR=mysql_fetch_assoc($rs)) { ?>
            <tr>
                <td>Référence:</td>
                <td><input type="text" name="ref" value="<?php echo ($AR['ref']) ?>" readonly="true"></td>
            </tr>
            <tr>
                <td>Nom:</td>
                <td><input type="text" name="nom" value="<?php echo ($AR['nom']) ?>"></td>
            </tr>
            <tr>
                <td> Catégorie:  </td>
                <td><input type="text" name="cat" value="<?php echo ($AR['cat']) ?>"></td>
            </tr>
            <tr>
                <td> Prix Unitaire:  </td>
                <td><input type="text" name="prix" value="<?php echo ($AR['prix']) ?>"></td>
            </tr>
            <tr>
                <td> Fabricant:  </td>
                <td><input type="text" name="fab" value="<?php echo ($AR['fabricant']) ?>"></td>
            </tr>
            <tr>
                <td>Photo: </td>
                <td><input type="file" name="photo"><img src="./images/<?php echo ($AR['photo']) ?>"</td>
            </tr>
            <tr>
                <td></td>
                <td> <input type="submit" value="Enregistrer"></td>
            </tr>
            <?php } ?>
        </table>

    </form>
    </body>
    </html>
<?php
mysql_free_result($rs);
mysql_close($conn);

?>
Professor
  • 610
  • 4
  • 20
0

you need to use isset and iempty to ensure you have value in your $_POST

<?php
require_once("connection.php");
if (isset($_POST['ref']) && !empty ($_POST['ref']))
{

$ref=$_POST['ref'];
$req="Select * from article where ref ='.$ref.'";
$rs= mysql_query($req) or die(mysql_error());
while($rs = mysql_fetch_array($rs)){

$ref = $rs['ref'];
$nom = $rs['nom'];
$cat = $rs['cat'];
$prix = $rs['prix'];
$fabricant = $rs['fabricant'];
$photo = $rs['photo'];
}
?>

html

<form method="POST" action="modifierArticle.php" enctype="multipart/form-data">
        <table>
            <tr>
                <td>Référence:</td>
                <td><input type="text" name="ref" value="<?php echo $ref; ?>" readonly="true"></td>
            </tr>
            <tr>
                <td>Nom:</td>
                <td><input type="text" name="nom" value="<?php echo $nom; ?>"></td>
            </tr>
            <tr>
                <td> Catégorie:  </td>
                <td><input type="text" name="cat" value="<?php echo $cat; ?>"></td>
            </tr>
            <tr>
                <td> Prix Unitaire:  </td>
                <td><input type="text" name="prix" value="<?php echo $prix; ?>"></td>
            </tr>
            <tr>
                <td> Fabricant:  </td>
                <td><input type="text" name="fab" value="<?php echo $fabricant; ?>"></td>
            </tr>
            <tr>
                <td>Photo: </td>
                <td><input type="file" name="photo"><img src="./images/<?php echo $photo; ?>"</td>
            </tr>
            <tr>
                <td></td>
                <td> <input type="submit" value="Enregistrer"></td>
            </tr>
        </table>

    </form>
    </body>
    </html>

end mysql connnection

<?php
    mysql_free_result($rs);
    mysql_close($conn);

    ?>

EDIT EDITED FEW SYNTEX mistakes :)

M.chaudhry
  • 651
  • 1
  • 6
  • 13
-1

Use a array_key_exists check. Like this:

if(array_key_exists('ref', $_POST)) {
 //your code here
}
Engineer
  • 5,911
  • 4
  • 31
  • 58
-1

check whether the POST request is empty or not

if(!empty($_POST['ref']))
{
$ref=$_POST['ref'];
}
else
{
echo 'no value';
exit;
}
ɹɐqʞɐ zoɹǝɟ
  • 4,342
  • 3
  • 22
  • 35