3

I developped a PHP website on an Apashe Server who worked just fine. When moved to my client's IIS Server we have a lot of errors like this one :

[18-May-2017 16:35:15 Asia/Kuwait] PHP Notice:  Undefined index: coupon in C:\inetpub\wwwroot\tourdesoceans\ville.php on line 61

I understand the error and checked the variables using isset(), !empty(), var_dump() and using $_GET instead of $_POST and they all seem to return expected results.

Here's a part of the form I'm using (I shortened it for this post) :

<form class="inscription" method="post">
    <a href="/">Choisissez une autre ville</a>
    <h1>ÉVÈNEMENT <?php echo $data['ville']; ?></h1>
    <p><strong>RÉSERVÉ AUX PROFESSIONNELS</strong></p>
    <p class="vip"><strong>Vous disposez d'une invitation VIP ?</strong><br />
    Merci de renseigner le code présent sur votre coupon :<br />
    <input type="text" name="coupon" value="<?php echo $_POST['coupon']; ?>" />
    <?php echo $erreur; ?></p>

    <label>Agence de voyage* :</label>
    <input type="text" name="agence" value="<?php echo $_POST['agence']; ?>" required />

    <label>Nom* :</label>
    <input type="text" name="nom" value="<?php echo $_POST['nom']; ?>" required />

    <label>Prénom* :</label>
    <input type="text" name="prenom" value="<?php echo $_POST['prenom']; ?>" required />

    <label>Email* :</label>
    <input type="email" name="email" value="<?php echo $_POST['email']; ?>" required />

    <label>Téléphone* :</label>
    <input type="tel" name="telephone" pattern="^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,5})|(\(?\d{2,6}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$" value="<?php echo $_POST['telephone']; ?>" required />

    <label>Adresse professionnelle* :</label>
    <textarea name="adresse" required><?php echo $_POST['adresse']; ?></textarea>

    <label>Code postal* :</label>
    <input type="text" name="cpostal" value="<?php echo $_POST['cpostal']; ?>" required />

    <label>Ville* :</label>
    <input type="text" name="ville" value="<?php echo $_POST['ville']; ?>" required />

    <input type="hidden" name="lieu" value="<?php echo strtolower($_POST['id']);?>" required />

    <input type="submit" value="Valider ma demande d’inscription" />
    <p>Si vous ne disposez pas d'une invitation VIP, <strong>rendez-vous le 1er juin 2017 pour vous inscrire !</strong></p>
    <p class="mention">*Champs obligatoires</p>
</form>

Here's how I get the variables and send them to the database :

if ($_SERVER['REQUEST_METHOD'] == 'POST') 
    {   
    ////////// PROTECTION DES DONNEES ENVOYEES //////////
    $coupon = htmlspecialchars(addslashes($_POST['coupon']));
    $agence = htmlspecialchars(addslashes($_POST['agence']));
    $nom = htmlspecialchars(addslashes($_POST['nom']));
    $prenom = htmlspecialchars(addslashes($_POST['prenom']));
    $email = htmlspecialchars(addslashes($_POST['email']));
    $telephone = htmlspecialchars(addslashes($_POST['telephone']));
    $adresse = htmlspecialchars(addslashes($_POST['adresse']));
    $cpostal = htmlspecialchars(addslashes($_POST['cpostal']));
    $ville = htmlspecialchars(addslashes($_POST['ville']));
    $lieu = htmlspecialchars(addslashes($_POST['lieu']));

    die(var_dump($_POST));

        // ENVOI A LA BDD
        $sql = "INSERT INTO tui_inscription (id, coupon, agence, nom, prenom, email, telephone, adresse, cpostal, ville, lieu) ";
        $sql .= "VALUES('', '".$coupon."', '".$agence."', '".$nom."', '".$prenom."', '".$email."', '".$telephone."', '".$adresse."', '".$cpostal."', '".$ville."', '".$lieu."')";
        $sql_add = mysqli_query($connexion, $sql) 
            or die ("Impossible de finaliser l'inscription");
    }

Here's a var_dump($_POST) after sending the form :

array(10) { ["coupon"]=> string(8) "VIPTDO17" ["agence"]=> string(4) "Lyon" ["nom"]=> string(6) "MYNAME" ["prenom"]=> string(9) "MYSURNAME" ["email"]=> string(15) "email@gmail.com" ["telephone"]=> string(10) "0123456789" ["adresse"]=> string(10) "MY ADDRESS" ["cpostal"]=> string(5) "69007" ["ville"]=> string(4) "TOWN" ["lieu"]=> string(0) "" }

They all marked as Undefined Index error in the logs !

Please let me know what further information you need to help me.

  • You should post all relevant code, otherwise your question stands to get closed with this one [PHP: “Notice: Undefined variable”, “Notice: Undefined index”, and “Notice: Undefined offset”](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – Funk Forty Niner May 18 '17 at 14:05
  • ok, I'll do that... but it's french :) – Grégory Santana May 18 '17 at 14:06
  • No problem, just as long as the syntax remains the same ;-) I can understand it fully. – Funk Forty Niner May 18 '17 at 14:07
  • You should re-edit to include the HTML for it, seeing you're using a form for the POST arrays. My guess is that the input for "coupon", doesn't have a name attribute for it, or is written as `Coupon` maybe. I can't say for sure, because I don't know what the HTML is. – Funk Forty Niner May 18 '17 at 14:11
  • I thought about it, but input are all well named. And it works perfectly on my server. Could it be a PHP config problem ? – Grégory Santana May 18 '17 at 14:13
  • Plus, the errors probably stem from the `value=""`'s since they're not defined. You need to use a ternary operator for those. – Funk Forty Niner May 18 '17 at 14:13
  • The above ^ is most likely your solution here. On your other server, error reporting must have been disabled, that's why you are now getting errors/notices. You need to test again every POST array to check if they are not empty or use a ternary operator in the values in the form elements. – Funk Forty Niner May 18 '17 at 14:17
  • Shame on me... You're totally right :( I was expecting the errors to come from the treatment... I don't get anymore Undefined Error, but the SQL request still die() – Grégory Santana May 18 '17 at 14:19
  • Most probably you are getting these notices when the form is first loaded. Because, nothing has been posted yet. Once the data is posted, there should be no such notice. – Atif May 18 '17 at 14:24
  • *"but the SQL request still die()"* - You need to use `mysqli_error($connexion)` on the query to tell what exactly went wrong. @GrégorySantana instead of `or die ("Impossible de finaliser l'inscription")` - probably an sql injection or something else, depending on the data going in. And `"TOWN" ["lieu"]=> string(0) ""` looks to be empty here, probably why, because of `name="lieu" value=""` being the wrong POST array. – Funk Forty Niner May 18 '17 at 14:24
  • **The answer is simple** You developed your site with error reporting **turned off**. So you didnt see all the errors in your site. When you moved to the IIS site it has error repoting **turned ON** and now you see all the errors! – RiggsFolly May 18 '17 at 14:28
  • Go back to your development server and **turn error reporting ON** and you will see them all on that server as well. **Thats the purpose of a development server** – RiggsFolly May 18 '17 at 14:29
  • Found it ! Thanks for your precious help ! – Grégory Santana May 18 '17 at 14:33
  • So I assume you are going to turn error reporting off in the IIS server as well? **Great - Wrong choise** – RiggsFolly May 18 '17 at 14:34
  • Of course not... I fixed the errors and reactivated error logs on my server instead. I can't remember why they where off. – Grégory Santana May 18 '17 at 16:40

0 Answers0