Partage
  • Partager sur Facebook
  • Partager sur Twitter

Upload d'image undefined index

Comment régler cette erreur ?

    13 juin 2021 à 16:39:10

    Bonjour j'essaye de faire un système d'upload d'image grâce à l'id du produit PHPmyadmin mais les erreurs suivantes me bloquent:

    Notice: Undefined index: img in C:\wamp64\www\Ben&co\admin\admin.php on line 28

    Notice: Undefined index: img in C:\wamp64\www\Ben&co\admin\admin.php on line 29

     Le point en question: 

    $title = $_POST['title'];
    $description = $_POST['description'];
    $price = $_POST['price'];       
    $img = $_POST['img'];
    $img = $_FILES['img']['name'];       //erreur 1
    $img_tmp = $_FILES['img']['tmp_name']; //erreur 2
    

    Le script complet: 

    <?php
    session_start();
    ?>
    
    
    <link rel="stylesheet" href="../style/bootstrap.css"/>
    <h1>Bienvenue, <?php echo $_SESSION['username']; ?></h1>
    <br/>
    <a href="?action=add">Ajouter un produit</a>
    <a href="?action=modifyanddelete">Modifier / Supprimer un produit</a><br/><br/>
    
    
    
    <?php
    
    if(isset($_SESSION['username'])){
    if(isset($_GET['action'])){
    if($_GET['action']=='add'){
    
    
    if(isset($_POST['submit'])){
    
    
    $title = $_POST['title'];
    $description = $_POST['description'];
    $price = $_POST['price'];
    $img = $_POST['img'];
    $img = $_FILES['img']['name'];
    $img_tmp = $_FILES['img']['tmp_name'];
    
    if(!empty($img_tmp)){
    
    $image = explode('.',$img);
    
    $image_ext = end($image);
    
    if(in_array(strtolower($image_ext), array('png','jpg','jpeg'))===false){
    
        echo'Veuillez rentrer une image ayant pour extension : png, jpg, jpeg';  
    }else{
        
        $picture = $title . ".jpg";
        move_uploaded_file( $_FILES['img']['tmp_name'], "img/" . $picture);
    
        if($image_size['mime']=='image/jpeg'){
            
            $image_src = imagecreatefromjpeg($img_tmp);
        
        }else if($image_size['mime']=='image/png'){
            
            $image_src = imagecreatefrompng($img_tmp);
        }else{
    
            $image_src = false;
            echo'Veuillez rentrer une image valide';
        }
    
        if($image_src!==false){
    
            $image_width=200;
    
            if($image_size[0]==$image_width){
    
            $image_finale = $image_src;
            
            }else{
    
                $new_width[0]=$image_width;
    
                $new_height = 200;
    
                $image_finale = imagecreatetruecolor($new_width[0],$new_height[1]);
    
                imagecopyresampled($image_finale,$image_src,0,0,0,0,$new_width[0],$new_height[1],$image_size[0],$image_size[1]);
    
            }
    
            imagejpeg($image_finale,'imgs/'.$title.'.jpg');
        }
    
    
    }
    
    }else{
    
        echo'Veuillez rentrer une image';
    }
    
    if ($title&&$description&&$price){
    
    try 
    {
    $db = new PDO('mysql:host=localhost;dbname=ben&co', 'root','');
    $db->setAttribute(PDO::ATTR_CASE, PDO::CASE_LOWER);
    $db->setAttribute(PDO::ATTR_ERRMODE , PDO::ERRMODE_EXCEPTION);
    }
    catch(Exception $e){
    
    echo'une erreur est survenue'; 
    die();
    
    }
    $insert = $db->prepare("INSERT INTO products (title, description, price) VALUES('$title', '$description', '$price')");
    $insert->execute();
    
    }else{
    echo'Veuillez remplir tous les champs';
    
    }
    
    }
    
    ?>
    
    <form action="" method="post" enctype="multipart/form-data  ">
    <h3>Titre du produit :</h3><input type="text" name="title"/>
    <h3>Description du produit :</h3><textarea type="text" name="description"></textarea>
    <h3>Prix :</h3><input type="text" name="price"/><br/><br/>
    <input type="file" name='img'/><br/><br/>
    <input type="submit" name='submit'>
    </form>
    
    
    <?php
    
    }else if($_GET['action']=='modifyanddelete'){
    
    $db = new PDO('mysql:host=localhost;dbname=ben&co', 'root',''); $select = $db->prepare("SELECT * FROM products");
    $select->execute();
    
    while($s=$select->fetch(PDO::FETCH_OBJ)){
        echo $s->title;
        ?>
        <a href="?action=modify&amp;id=<?php echo $s->id; ?>">Modifier</a>
        <a href="?action=delete&amp;id=<?php echo $s->id; ?>">Supprimer</a><br/><br/>
        <?php
    }
    
    }else if($_GET['action']=='modify'){
    
        $id=$_GET['id'];
        $db = new PDO('mysql:host=localhost;dbname=ben&co', 'root','');    $select = $db->prepare("SELECT * FROM products WHERE id=$id");
        $select->execute();
    
        $data = $select->fetch(PDO::FETCH_OBJ);
    
        ?>
    
    
        <form action="" method="post">
        <h3>Titre du produit :</h3><input value="<?php echo $data->title; ?>" type="text" name="title"/>
        <h3>Description du produit :</h3><textarea type="text" name="description"><?php echo $data->description; ?></textarea>
        <h3>Prix :</h3><input value="<?php echo $data->price; ?>" type="text" name="price"/><br/><br/>
        <input type="submit" name="submit" value="Modifier"/>
        </form>
    
    
        <?php
    
    
        if(isset($_POST['submit'])){
    
            $title=$_POST['title'];
            $description=$_POST['description'];
            $price=$_POST['price'];
    
            $db = new PDO('mysql:host=localhost;dbname=ben&co', 'root','');   $update = $db->prepare("UPDATE products SET title='$title' ,description='$description' ,price='$price' WHERE id=$id");
            $update->execute();
    
            header('location: admin.php?action?==modifyanddelete');
        }
    
    
    
    
    }else if($_GET['action']=='delete'){
    
        $id=$_GET['id'];
        $db = new PDO('mysql:host=localhost;dbname=ben&co', 'root','');    $select = $db->prepare("DELETE FROM products WHERE id=$id");
        $select->execute();
    
    
    }else{
    
    die('une erreur s\'est produite.');
    
    }
    }else{
    
    
    }
    }else{
    
    header('Location: ../index.php');
    
    
    }
    
    
    
    ?>
    

    J'espère que vous trouverez une solution merci d'avance


    • Partager sur Facebook
    • Partager sur Twitter
      13 juin 2021 à 20:05:06

      Bonjour.

      Pourquoi tu as 15 000 milles connexion a la bdd ? Dont une seul a les erreurs d'activer. 

      Que fait la ligne 6 ici ? Elle devrait être entre les balises head

      Supprime la ligne 27.

      Il y a des espaces en trop dans le enctype du formulaire

      Et apprend à indenter ton code pour le rendre plus lisible.

      Aucune requête n'est correctement préparer donc bonjour les injections sql 

      • Partager sur Facebook
      • Partager sur Twitter

      Upload d'image undefined index

      × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
      × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
      • Editeur
      • Markdown