Undefined array key “profile” – PHP – SitePoint Forums
Good morning,
I get this error message and I don’t see where my error is in my code.
Do you have an idea ?
my source code :
<?php
require_once 'db.php';
// ajouter un produit
if(isset($_POST['ajouter']))
{
$code_article = $_POST['code_article'];
$nom_article = $_POST['nom_article'];
$quantite = $_POST['quantite'];
$images=$_FILES['profile']['name'];
$tmp_dir=$_FILES['profile']['tmp_name'];
$imageSize=$_FILES['profile']['size'];
$upload_dir="uploads/";
$imgExt=strtolower(pathinfo($images,PATHINFO_EXTENSION));
$valid_extensions=array('jpeg', 'jpg', 'png', 'gif', 'pdf');
$picProfile=rand(1000, 10000000).".".$imgExt;
move_uploaded_file($tmp_dir, $upload_dir.$picProfile);
$sql ="INSERT INTO `produit`( `code_article`, `nom_article`, `images`, `quantite`)
VALUES (:code_article, :nom_article, :pic, :quantite)";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':code_article', $code_article);
$stmt->bindParam(':nom_article', $nom_article);
$stmt->bindParam(':pic', $picProfile);
$stmt->bindParam(':quantite', $quantite);
$stmt->execute();
}
?>
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://www.sitepoint.com/community/t/undefined-array-key-profile/style.css">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<title>Gestion de stock</title>
</head>
<body>
<header>
<nav class="navbar">
<p class="logo">Gestion De Stock</p>
<ul class="navlinks">
<li><a href="#">Les Produits</a></li>
<li><a href="#">L'etat de stock</a></li>
<li><a href="#">Les Factures</a></li>
<li><a href="#">Les Fournisseurs</a></li>
</ul>
</nav>
</header>
<div class="container">
<div class="row">
<div class="col-2 mt-3">
<form action="" class="form-group mt-3" method="post">
<label for="">Image du Produit :</label>
<input type="file" class="form-control mt-3" name="images">
<label for="">Code Article :</label>
<input type="text" class="form-control mt-3" name="code_article">
<label for="">Nom du Produit :</label>
<input type="text" class="form-control mt-3" name="nom_article">
<label for="">Quantite :</label>
<input type="text" class="form-control mt-3" name="quantite">
<button type="submit" class="btn btn-primary mt-3" name="ajouter">Enregistrer</button>
</form>
</div>
<div class="col-10 mt-3">
<table class="table table-striped">
<thead>
<th>Image</th>
<th>Code Article</th>
<th>Designation</th>
<th>Quantite</th>
</thead>
<tbody>
<tr>
<td><img src="./images/pentalon.png" alt=" " class="image_product"></td>
<td>1000</td>
<td>Pc Portable HP</td>
<td>250 PC</td>
</tr>
<tr>
<td><img src="./images/pentalon.png" alt=" " class="image_product"></td>
<td>1000</td>
<td>Pc Portable HP</td>
<td>250 PC</td>
</tr>
<tr>
<td><img src="./images/pentalon.png" alt=" " class="image_product"></td>
<td>1000</td>
<td>Pc Portable HP</td>
<td>250 PC</td>
</tr>
<tr>
<td><img src="./images/pentalon.png" alt=" " class="image_product"></td>
<td>1000</td>
<td>Pc Portable HP</td>
<td>250 PC</td>
</tr>
<tr>
<td><img src="./images/pentalon.png" alt=" " class="image_product"></td>
<td>1000</td>
<td>Pc Portable HP</td>
<td>250 PC</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>
Your form doesn’t have an input named “profile”, so it won’t be defined.
Because you are looking for it in $_FILES
you would specifically need a file type input of that name to define it.
1 Like
Is this where I have to define profile?
<form action="" class="form-group mt-3" method="post">
<label for="">Image du Produit :</label>
<input type="file" class="form-control mt-3" name="images">
<label for="">Code Article :</label>
<input type="text" class="form-control mt-3" name="code_article">
<label for="">Nom du Produit :</label>
<input type="text" class="form-control mt-3" name="nom_article">
<label for="">Quantite :</label>
<input type="text" class="form-control mt-3" name="quantite">
<button type="submit" class="btn btn-primary mt-3" name="ajouter">Enregistrer</button>
</form>
Yes.
You’ll need to specify the correct enctype
for your form, too.
1 Like
Thank you for your help, in fact several of us are following an online tutorial and we are just starting out and we have this error.
I have understood the error, but from a practical code point of view, what should I do?
comme ceci je pense ?
<form action="index.php" class="form-group mt-3" method="POST" enctype="multipart/form-data" >
<label for="">Image du Produit :</label>
<input type="file" class="form-control mt-3" name="profile" accept="*/image">
<label for="">Code Article :</label>
<input type="text" class="form-control mt-3" name="code_article" required>
<label for="">Designation du Produit :</label>
<input type="text" class="form-control mt-3" name="nom_article" required>
<label for="">Quantite :</label>
<input type="text" class="form-control mt-3" name="quantite" required>
<button type="submit" class="btn btn-primary mt-3" name="ajouter">Enregistrer</button>
</form>
It seems to me that you’ve fixed the original problem – you now have a form field called “profile”, so the error messages should have gone away as long as you upload an image. Obviously you’ll need to check that before trying to work with it, and presumably your tutorial will cover that at some point.
You’ve also added the enctype
parameter, so all should be good. Or do you still have a problem?
1 Like
I have the same problem again: product_image is not defined or should I declare product_image?
<?php
require_once 'db.php';
// ajouter un produit
if(isset($_POST['ajouter']))
{
$code_article = $_POST['code_article'];
$nom_article = $_POST['nom_article'];
$quantite = $_POST['quantite'];
$images=$_FILES['profile']['name'];
$tmp_dir=$_FILES['profile']['tmp_name'];
$imageSize=$_FILES['profile']['size'];
$upload_dir="uploads/";
$imgExt=strtolower(pathinfo($images,PATHINFO_EXTENSION));
$valid_extensions=array('jpeg', 'jpg', 'png', 'gif', 'pdf');
$picProfile=rand(1000, 10000000).".".$imgExt;
move_uploaded_file($tmp_dir, $upload_dir.$picProfile);
$sql ="INSERT INTO `produit`( `code_article`, `nom_article`, `images`, `quantite`)
VALUES (:code_article, :nom_article, :pic, :quantite)";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':code_article', $code_article);
$stmt->bindParam(':nom_article', $nom_article);
$stmt->bindParam(':pic', $picProfile);
$stmt->bindParam(':quantite', $quantite);
$stmt->execute();
}
$stmt = $pdo->query('SELECT * FROM produit');
?>
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://www.sitepoint.com/community/t/undefined-array-key-profile/style.css">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<title>Gestion de stock</title>
</head>
<body>
<header>
<nav class="navbar">
<p class="logo">Gestion De Stock</p>
<ul class="navlinks">
<li><a href="#">Les Produits</a></li>
<li><a href="#">L'etat de stock</a></li>
<li><a href="#">Les Factures</a></li>
<li><a href="#">Les Fournisseurs</a></li>
</ul>
</nav>
</header>
<div class="container">
<div class="row">
<div class="col-2 mt-3">
<form action="index.php" class="form-group mt-3" method="POST" enctype="multipart/form-data" >
<label for="">Image du Produit :</label>
<input type="file" class="form-control mt-3" name="profile" accept="*/image">
<label for="">Code Article :</label>
<input type="text" class="form-control mt-3" name="code_article" required>
<label for="">Designation du Produit :</label>
<input type="text" class="form-control mt-3" name="nom_article" required>
<label for="">Quantite :</label>
<input type="text" class="form-control mt-3" name="quantite" required>
<button type="submit" class="btn btn-primary mt-3" name="ajouter">Enregistrer</button>
</form>
</div>
<div class="col-10 mt-3">
<table class="table table-striped">
<thead>
<th>Image</th>
<th>Code Article</th>
<th>Designation</th>
<th>Quantite</th>
</thead>
<tbody>
<?php
while ( $row = $stmt->fetch())
{
?>
<tr>
<td><img src="./uploads/<?php echo $row-> image_produit; ?>" alt=" " class="image_product "></td>
<td><?php echo $row-> code_article; ?> </td>
<td><?php echo $row-> nom_article; ?> </td>
<td><?php echo $row-> quantite; ?> </td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>