PHP: If-Statement & Form









up vote
0
down vote

favorite












my code is ...



<?php
$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);
$passwordr = mysql_real_escape_string($_POST['passwordr']);
$email = mysql_real_escape_string($_POST['email']);

if(empty($username) || empty($password) || empty($passwordr) || empty($email))
echo '<div class="alert alert-danger">Bitte fülle alle Felder aus.</div>';


elseif(!preg_match('/^[a-zA-Z0-9-_^#]4,15$/', $username))
echo '<div class="alert alert-danger">Bitte gebe einen gültigen Usernamen ein.<br />Der Username muss mindestens 4 und maximal 15 Zeichen lang sein und darf nur folgende Sonderzeichen enthalten: -_^#</div>';


elseif(!preg_match('/^[a-zA-Z0-9-_*^#?!.,;@€]8,20$/', $password))
echo '<div class="alert alert-danger">Bitte gebe ein gültiges Passwort ein.<br />Das Passwort muss mindestens 8 und maximal 20 Zeichen lang sein und darf nur folgende Sonderzeichen enthalten: -_*^#?!.,;@€</div>';


elseif($passwordr != $password)
echo '<div class="alert alert-danger">Die Passwörter stimmen nicht überein.</div>';


elseif(!preg_match('/^[a-zA-Z0-9-_.]+@[a-zA-Z0-9-_.]+.[a-zA-Z]2,4$/', $email))
echo '<div class="alert alert-danger">Bitte gebe eine gültige E-Mail Adresse ein.</div>';


else
die("have a n1 day");

?>


somehow, I always get <div class="alert alert-danger">Bitte fülle alle Felder aus.</div>, even when the forms aren't empty.



why?



here's my form ...



<div class="row">
<form method="POST">
<fieldset>
<div class="col-lg-5">
<div id="username-group" class="form-group">
<label for="username">Username</label>
<input type="text" name="username" id="username" class="form-control" placeholder="Username">
</div>
<div id="password-group" class="form-group">
<label for="password">Passwort</label>
<input type="password" name="password" id="password" class="form-control" placeholder="Passwort">
</div>
<div id="passwordr-group" class="form-group">
<label for="passwordr">Passwort wiederholen</label>
<input type="password" name="passwordr" id="passwordr" class="form-control" placeholder="Passwort wiederholen">
</div>
<div id="email-group" class="form-group">
<label for="email">E-Mail</label>
<input type="email" name="email" id="email" class="form-control" placeholder="E-Mail">
</div>
<input type="submit" name="submit" id="submit" class="btn btn-default" value="Registrieren">
</div>
</fieldset>
</form>
</div>


I'm using bootstrap, but I don't think that's the problem.
and the code looks fine too.










share|improve this question



















  • 3




    Where are your variables being set? Can you provide your form markup too?
    – Jon Stirling
    May 14 '15 at 21:34










  • First check that you are actually getting values back from the form, you can use: print_r($_REQUEST);die; . Than show some more code where you are actually assigning this from $_POST (or $_GET) into those variables. you're not using extract, right?
    – Mahakala
    May 14 '15 at 21:39






  • 1




    You realize that the name doesn't magically turn into $username, but $_POST['username']
    – adeneo
    May 14 '15 at 21:41










  • edited the topic.
    – Underbytex
    May 14 '15 at 21:43






  • 1




    Please, stop using mysql_* functions. They are no longer maintained and are officially deprecated. Learn about prepared statements instead, and consider using PDO, it's not as hard as you think.
    – Jay Blanchard
    May 14 '15 at 21:46














up vote
0
down vote

favorite












my code is ...



<?php
$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);
$passwordr = mysql_real_escape_string($_POST['passwordr']);
$email = mysql_real_escape_string($_POST['email']);

if(empty($username) || empty($password) || empty($passwordr) || empty($email))
echo '<div class="alert alert-danger">Bitte fülle alle Felder aus.</div>';


elseif(!preg_match('/^[a-zA-Z0-9-_^#]4,15$/', $username))
echo '<div class="alert alert-danger">Bitte gebe einen gültigen Usernamen ein.<br />Der Username muss mindestens 4 und maximal 15 Zeichen lang sein und darf nur folgende Sonderzeichen enthalten: -_^#</div>';


elseif(!preg_match('/^[a-zA-Z0-9-_*^#?!.,;@€]8,20$/', $password))
echo '<div class="alert alert-danger">Bitte gebe ein gültiges Passwort ein.<br />Das Passwort muss mindestens 8 und maximal 20 Zeichen lang sein und darf nur folgende Sonderzeichen enthalten: -_*^#?!.,;@€</div>';


elseif($passwordr != $password)
echo '<div class="alert alert-danger">Die Passwörter stimmen nicht überein.</div>';


elseif(!preg_match('/^[a-zA-Z0-9-_.]+@[a-zA-Z0-9-_.]+.[a-zA-Z]2,4$/', $email))
echo '<div class="alert alert-danger">Bitte gebe eine gültige E-Mail Adresse ein.</div>';


else
die("have a n1 day");

?>


somehow, I always get <div class="alert alert-danger">Bitte fülle alle Felder aus.</div>, even when the forms aren't empty.



why?



here's my form ...



<div class="row">
<form method="POST">
<fieldset>
<div class="col-lg-5">
<div id="username-group" class="form-group">
<label for="username">Username</label>
<input type="text" name="username" id="username" class="form-control" placeholder="Username">
</div>
<div id="password-group" class="form-group">
<label for="password">Passwort</label>
<input type="password" name="password" id="password" class="form-control" placeholder="Passwort">
</div>
<div id="passwordr-group" class="form-group">
<label for="passwordr">Passwort wiederholen</label>
<input type="password" name="passwordr" id="passwordr" class="form-control" placeholder="Passwort wiederholen">
</div>
<div id="email-group" class="form-group">
<label for="email">E-Mail</label>
<input type="email" name="email" id="email" class="form-control" placeholder="E-Mail">
</div>
<input type="submit" name="submit" id="submit" class="btn btn-default" value="Registrieren">
</div>
</fieldset>
</form>
</div>


I'm using bootstrap, but I don't think that's the problem.
and the code looks fine too.










share|improve this question



















  • 3




    Where are your variables being set? Can you provide your form markup too?
    – Jon Stirling
    May 14 '15 at 21:34










  • First check that you are actually getting values back from the form, you can use: print_r($_REQUEST);die; . Than show some more code where you are actually assigning this from $_POST (or $_GET) into those variables. you're not using extract, right?
    – Mahakala
    May 14 '15 at 21:39






  • 1




    You realize that the name doesn't magically turn into $username, but $_POST['username']
    – adeneo
    May 14 '15 at 21:41










  • edited the topic.
    – Underbytex
    May 14 '15 at 21:43






  • 1




    Please, stop using mysql_* functions. They are no longer maintained and are officially deprecated. Learn about prepared statements instead, and consider using PDO, it's not as hard as you think.
    – Jay Blanchard
    May 14 '15 at 21:46












up vote
0
down vote

favorite









up vote
0
down vote

favorite











my code is ...



<?php
$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);
$passwordr = mysql_real_escape_string($_POST['passwordr']);
$email = mysql_real_escape_string($_POST['email']);

if(empty($username) || empty($password) || empty($passwordr) || empty($email))
echo '<div class="alert alert-danger">Bitte fülle alle Felder aus.</div>';


elseif(!preg_match('/^[a-zA-Z0-9-_^#]4,15$/', $username))
echo '<div class="alert alert-danger">Bitte gebe einen gültigen Usernamen ein.<br />Der Username muss mindestens 4 und maximal 15 Zeichen lang sein und darf nur folgende Sonderzeichen enthalten: -_^#</div>';


elseif(!preg_match('/^[a-zA-Z0-9-_*^#?!.,;@€]8,20$/', $password))
echo '<div class="alert alert-danger">Bitte gebe ein gültiges Passwort ein.<br />Das Passwort muss mindestens 8 und maximal 20 Zeichen lang sein und darf nur folgende Sonderzeichen enthalten: -_*^#?!.,;@€</div>';


elseif($passwordr != $password)
echo '<div class="alert alert-danger">Die Passwörter stimmen nicht überein.</div>';


elseif(!preg_match('/^[a-zA-Z0-9-_.]+@[a-zA-Z0-9-_.]+.[a-zA-Z]2,4$/', $email))
echo '<div class="alert alert-danger">Bitte gebe eine gültige E-Mail Adresse ein.</div>';


else
die("have a n1 day");

?>


somehow, I always get <div class="alert alert-danger">Bitte fülle alle Felder aus.</div>, even when the forms aren't empty.



why?



here's my form ...



<div class="row">
<form method="POST">
<fieldset>
<div class="col-lg-5">
<div id="username-group" class="form-group">
<label for="username">Username</label>
<input type="text" name="username" id="username" class="form-control" placeholder="Username">
</div>
<div id="password-group" class="form-group">
<label for="password">Passwort</label>
<input type="password" name="password" id="password" class="form-control" placeholder="Passwort">
</div>
<div id="passwordr-group" class="form-group">
<label for="passwordr">Passwort wiederholen</label>
<input type="password" name="passwordr" id="passwordr" class="form-control" placeholder="Passwort wiederholen">
</div>
<div id="email-group" class="form-group">
<label for="email">E-Mail</label>
<input type="email" name="email" id="email" class="form-control" placeholder="E-Mail">
</div>
<input type="submit" name="submit" id="submit" class="btn btn-default" value="Registrieren">
</div>
</fieldset>
</form>
</div>


I'm using bootstrap, but I don't think that's the problem.
and the code looks fine too.










share|improve this question















my code is ...



<?php
$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);
$passwordr = mysql_real_escape_string($_POST['passwordr']);
$email = mysql_real_escape_string($_POST['email']);

if(empty($username) || empty($password) || empty($passwordr) || empty($email))
echo '<div class="alert alert-danger">Bitte fülle alle Felder aus.</div>';


elseif(!preg_match('/^[a-zA-Z0-9-_^#]4,15$/', $username))
echo '<div class="alert alert-danger">Bitte gebe einen gültigen Usernamen ein.<br />Der Username muss mindestens 4 und maximal 15 Zeichen lang sein und darf nur folgende Sonderzeichen enthalten: -_^#</div>';


elseif(!preg_match('/^[a-zA-Z0-9-_*^#?!.,;@€]8,20$/', $password))
echo '<div class="alert alert-danger">Bitte gebe ein gültiges Passwort ein.<br />Das Passwort muss mindestens 8 und maximal 20 Zeichen lang sein und darf nur folgende Sonderzeichen enthalten: -_*^#?!.,;@€</div>';


elseif($passwordr != $password)
echo '<div class="alert alert-danger">Die Passwörter stimmen nicht überein.</div>';


elseif(!preg_match('/^[a-zA-Z0-9-_.]+@[a-zA-Z0-9-_.]+.[a-zA-Z]2,4$/', $email))
echo '<div class="alert alert-danger">Bitte gebe eine gültige E-Mail Adresse ein.</div>';


else
die("have a n1 day");

?>


somehow, I always get <div class="alert alert-danger">Bitte fülle alle Felder aus.</div>, even when the forms aren't empty.



why?



here's my form ...



<div class="row">
<form method="POST">
<fieldset>
<div class="col-lg-5">
<div id="username-group" class="form-group">
<label for="username">Username</label>
<input type="text" name="username" id="username" class="form-control" placeholder="Username">
</div>
<div id="password-group" class="form-group">
<label for="password">Passwort</label>
<input type="password" name="password" id="password" class="form-control" placeholder="Passwort">
</div>
<div id="passwordr-group" class="form-group">
<label for="passwordr">Passwort wiederholen</label>
<input type="password" name="passwordr" id="passwordr" class="form-control" placeholder="Passwort wiederholen">
</div>
<div id="email-group" class="form-group">
<label for="email">E-Mail</label>
<input type="email" name="email" id="email" class="form-control" placeholder="E-Mail">
</div>
<input type="submit" name="submit" id="submit" class="btn btn-default" value="Registrieren">
</div>
</fieldset>
</form>
</div>


I'm using bootstrap, but I don't think that's the problem.
and the code looks fine too.







php forms






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 16:16









Cœur

17.2k9102141




17.2k9102141










asked May 14 '15 at 21:32









Underbytex

99115




99115







  • 3




    Where are your variables being set? Can you provide your form markup too?
    – Jon Stirling
    May 14 '15 at 21:34










  • First check that you are actually getting values back from the form, you can use: print_r($_REQUEST);die; . Than show some more code where you are actually assigning this from $_POST (or $_GET) into those variables. you're not using extract, right?
    – Mahakala
    May 14 '15 at 21:39






  • 1




    You realize that the name doesn't magically turn into $username, but $_POST['username']
    – adeneo
    May 14 '15 at 21:41










  • edited the topic.
    – Underbytex
    May 14 '15 at 21:43






  • 1




    Please, stop using mysql_* functions. They are no longer maintained and are officially deprecated. Learn about prepared statements instead, and consider using PDO, it's not as hard as you think.
    – Jay Blanchard
    May 14 '15 at 21:46












  • 3




    Where are your variables being set? Can you provide your form markup too?
    – Jon Stirling
    May 14 '15 at 21:34










  • First check that you are actually getting values back from the form, you can use: print_r($_REQUEST);die; . Than show some more code where you are actually assigning this from $_POST (or $_GET) into those variables. you're not using extract, right?
    – Mahakala
    May 14 '15 at 21:39






  • 1




    You realize that the name doesn't magically turn into $username, but $_POST['username']
    – adeneo
    May 14 '15 at 21:41










  • edited the topic.
    – Underbytex
    May 14 '15 at 21:43






  • 1




    Please, stop using mysql_* functions. They are no longer maintained and are officially deprecated. Learn about prepared statements instead, and consider using PDO, it's not as hard as you think.
    – Jay Blanchard
    May 14 '15 at 21:46







3




3




Where are your variables being set? Can you provide your form markup too?
– Jon Stirling
May 14 '15 at 21:34




Where are your variables being set? Can you provide your form markup too?
– Jon Stirling
May 14 '15 at 21:34












First check that you are actually getting values back from the form, you can use: print_r($_REQUEST);die; . Than show some more code where you are actually assigning this from $_POST (or $_GET) into those variables. you're not using extract, right?
– Mahakala
May 14 '15 at 21:39




First check that you are actually getting values back from the form, you can use: print_r($_REQUEST);die; . Than show some more code where you are actually assigning this from $_POST (or $_GET) into those variables. you're not using extract, right?
– Mahakala
May 14 '15 at 21:39




1




1




You realize that the name doesn't magically turn into $username, but $_POST['username']
– adeneo
May 14 '15 at 21:41




You realize that the name doesn't magically turn into $username, but $_POST['username']
– adeneo
May 14 '15 at 21:41












edited the topic.
– Underbytex
May 14 '15 at 21:43




edited the topic.
– Underbytex
May 14 '15 at 21:43




1




1




Please, stop using mysql_* functions. They are no longer maintained and are officially deprecated. Learn about prepared statements instead, and consider using PDO, it's not as hard as you think.
– Jay Blanchard
May 14 '15 at 21:46




Please, stop using mysql_* functions. They are no longer maintained and are officially deprecated. Learn about prepared statements instead, and consider using PDO, it's not as hard as you think.
– Jay Blanchard
May 14 '15 at 21:46












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










If you add the error logging



error_reporting(E_ALL);
ini_set('display_errors','On');


you will see this:




Deprecated: mysql_real_escape_string(): The mysql extension is
deprecated and will be removed in the future: use mysqli or PDO
instead in .../public_html/index.php on line 38



Warning: mysql_real_escape_string(): Access denied for user 'www-data'@'localhost' (using password: NO) in
.../public_html/index.php on line 38




mysql_real_escape_string requires you to connect to the database first. Also indeed this function is deprecated.



You can start with deleting mysql_real_escape_string from the code to see that it works.






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f30247727%2fphp-if-statement-form%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    If you add the error logging



    error_reporting(E_ALL);
    ini_set('display_errors','On');


    you will see this:




    Deprecated: mysql_real_escape_string(): The mysql extension is
    deprecated and will be removed in the future: use mysqli or PDO
    instead in .../public_html/index.php on line 38



    Warning: mysql_real_escape_string(): Access denied for user 'www-data'@'localhost' (using password: NO) in
    .../public_html/index.php on line 38




    mysql_real_escape_string requires you to connect to the database first. Also indeed this function is deprecated.



    You can start with deleting mysql_real_escape_string from the code to see that it works.






    share|improve this answer


























      up vote
      0
      down vote



      accepted










      If you add the error logging



      error_reporting(E_ALL);
      ini_set('display_errors','On');


      you will see this:




      Deprecated: mysql_real_escape_string(): The mysql extension is
      deprecated and will be removed in the future: use mysqli or PDO
      instead in .../public_html/index.php on line 38



      Warning: mysql_real_escape_string(): Access denied for user 'www-data'@'localhost' (using password: NO) in
      .../public_html/index.php on line 38




      mysql_real_escape_string requires you to connect to the database first. Also indeed this function is deprecated.



      You can start with deleting mysql_real_escape_string from the code to see that it works.






      share|improve this answer
























        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






        If you add the error logging



        error_reporting(E_ALL);
        ini_set('display_errors','On');


        you will see this:




        Deprecated: mysql_real_escape_string(): The mysql extension is
        deprecated and will be removed in the future: use mysqli or PDO
        instead in .../public_html/index.php on line 38



        Warning: mysql_real_escape_string(): Access denied for user 'www-data'@'localhost' (using password: NO) in
        .../public_html/index.php on line 38




        mysql_real_escape_string requires you to connect to the database first. Also indeed this function is deprecated.



        You can start with deleting mysql_real_escape_string from the code to see that it works.






        share|improve this answer














        If you add the error logging



        error_reporting(E_ALL);
        ini_set('display_errors','On');


        you will see this:




        Deprecated: mysql_real_escape_string(): The mysql extension is
        deprecated and will be removed in the future: use mysqli or PDO
        instead in .../public_html/index.php on line 38



        Warning: mysql_real_escape_string(): Access denied for user 'www-data'@'localhost' (using password: NO) in
        .../public_html/index.php on line 38




        mysql_real_escape_string requires you to connect to the database first. Also indeed this function is deprecated.



        You can start with deleting mysql_real_escape_string from the code to see that it works.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited May 14 '15 at 22:21









        Ferrrmolina

        2,10821838




        2,10821838










        answered May 14 '15 at 21:53









        Axalix

        2,34911130




        2,34911130



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f30247727%2fphp-if-statement-form%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3

            Museum of Modern and Contemporary Art of Trento and Rovereto