0

I'm having a problem with my hosting add form.

Indeed I have a form of addition of hergement.

I encountered some problem adding an image in the database and in a file but now it works. To make it work I "isolated" adding image to the database by creating another file telling me that once it will work I will just add it to the form and everything will work as expected. Except that when I add it to my final form (complete, with all the fields) it puts me that the new lenames that I insert are undefined with that in the other file or I just added the image in the database works .... can you take a look at this problem please?

Thank you

My code "isolated"

<?php

$bdd = new PDO('mysql:host=127.0.0.1;dbname=resa', 'root', '');

if(isset($_POST['formAjoutHebergement']))
{

  $target = "imgheb/images/".basename($_FILES['imageheb']['name']);
  $temp = explode (".", $_FILES["imageheb"]["name"]);
  $nomimg = $_FILES['imageheb']['name'];


  if(move_uploaded_file($_FILES["imageheb"]["tmp_name"],$target)){
    $valide = 'Ok';
    /*$insertimg = "INSERT INTO hebergement(IMAGEHEB) VALUES('$nomimg')";*/
    $sql = $bdd->prepare("INSERT INTO `hebergement` (`IMAGEHEB`) VALUES (?)");
    $sql->execute(array($nomimg));

  }

  else
  {
    $erreur = "Probleme upload image";
  }
}

?>

My complete code telling me that lines 80, 82, 84 and 87 are Undefined index ... I do not understand:

<?php

$bdd = new PDO('mysql:host=127.0.0.1;dbname=resa', 'root', '');

if(isset($_POST['formAjoutHebergement']))
{

  $codetypeheb = htmlspecialchars($_POST['codetypeheb']);
  $nomheb = htmlspecialchars($_POST['nomheb']);
  $nbplaceheb = htmlspecialchars($_POST['nbplaceheb']);
  $surfaceheb = htmlspecialchars($_POST['surfaceheb']);
  $internet = htmlspecialchars($_POST['internet']);
  $anneeheb = htmlspecialchars($_POST['anneeheb']);
  $secteurheb = htmlspecialchars($_POST['secteurheb']);
  $orientationheb = htmlspecialchars($_POST['orientationheb']);
  $etatheb = htmlspecialchars($_POST['etatheb']);
  $descriheb = htmlspecialchars($_POST['descriheb']);
  $tarifsemheb = htmlspecialchars($_POST['tarifsemheb']);




  if(!empty($_POST['codetypeheb']) AND !empty($_POST['nomheb']) AND !empty($_POST['nbplaceheb']) AND !empty($_POST['surfaceheb']) AND !empty($_POST['internet']) AND !empty($_POST['anneeheb']) AND !empty($_POST['secteurheb']) AND !empty($_POST['orientationheb']) AND !empty($_POST['etatheb']) AND !empty($_POST['descriheb']) AND !empty($_POST['tarifsemheb']))
  {

    $codetypeheblenght = strlen($codetypeheb);
    $nomheblenght = strlen($nomheb);
    $nbplaceheblenght = strlen($nbplaceheb);
    $surfaceheblenght = strlen($surfaceheb);
    $anneeheblenght = strlen($anneeheb);
    $secteurheblenght = strlen($secteurheb);
    $orientationheblenght = strlen($orientationheb);
    $etatheblenght = strlen($etatheb);
    $descriheblenght = strlen($descriheb);
    $tarifsemheblenght = strlen($tarifsemheb);


    $reqcodetypeheb = $bdd->prepare("SELECT * FROM hebergement WHERE codetypeheb = ?");
    $reqcodetypeheb->execute(array($codetypeheb));
    $codetypehebexist = $reqcodetypeheb->rowCount();

    $reqnomheb = $bdd->prepare("SELECT * FROM hebergement WHERE nomheb = ?");
    $reqnomheb->execute(array($nomheb));
    $nomhebexist = $reqnomheb->rowCount();


    if($codetypehebexist == 0)
    {
      if($nomhebexist == 0)
      {


        $target = "imgheb/images/".basename($_FILES['imageheb']['name']);
        var_dump($target);
        $temp = explode (".", $_FILES["imageheb"]["name"]);
        var_dump($temp);
        $nomimg = $_FILES['imageheb']['name'];
        var_dump($nomimg);

        if(move_uploaded_file($_FILES["imageheb"]["tmp_name"],$target)){
          $valide = 'Ok';
          $sql = $bdd->prepare("INSERT INTO `hebergement` (`IMAGEHEB`) VALUES (?)");
          $sql->execute(array($nomimg));
          var_dump($sql);
        }

        else
        {
          $erreur = "Probleme upload image";
        }


        $insertheb = $bdd->prepare("INSERT INTO hebergement(codetypeheb, nomheb, nbplaceheb, surfaceheb, internet, anneeheb, secteurheb, orientationheb, etatheb, descriheb, tarifsemheb) VALUES(?,?,?,?,?,?,?,?,?,?,?)");
        $insertheb->execute(array($codetypeheb, $nomheb, $nbplaceheb, $surfaceheb, $internet, $anneeheb, $secteurheb, $orientationheb, $etatheb, $descriheb, $tarifsemheb ));
        $valide = "Votre hebergement a bien été créé !";
        /*header('Location: index1.php');*/
      }
      else
      {
        $erreur = "Nom hebergement déjà utilisé !";
      }
    }
    else
    {
      $erreur = "Code type hebergement déjà utilisé !";
    }
  }
}
?>
  • Those nested `if`s tho :( It's no wonder you can't debug your code. I mean no offence at all, and this doesn't answer the question, but a better code writing style will reduce your bugs and improve your ability to find them when they popup. You have 13 levels of IF :( then you have elses for some of them later on. It's just impossible to see where anything is. – James Apr 01 '18 at 17:32
  • woah! That's a lot of code. Your style of coding is questionable though – Rotimi Apr 01 '18 at 17:38
  • @James Okay I understand thank you so much for the remarks I do not feel offended at all. So there is an alternative to if else? I did this to target the error messages ... if i put everything in the same if i will only have one error message right? –  Apr 01 '18 at 17:41
  • Not so much alternative to if else, just the use of them. Although you could use a switch statement instead. eg instead of having all the IFs nested in each other then the thing that's ok, and then the elses. Just have all the IFs together and in each one check for bad values, if bad values set the var `$error`, then only do the DB thing if `$error` is not set or is false etc. – James Apr 01 '18 at 17:44
  • @James That's it, I just removed the yew and its else which are not "indispensable" for the moment. In spite of that the problem always arises ... –  Apr 01 '18 at 17:53

0 Answers0