-4

I have searched on stackoverflow about the undifined variable, and I couldn't find anything that solved my problem.

this is my PHP code:

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

    $username =mysql_real_escape_string( $_POST['naam']);
    $password =md5( $_POST['wacht']);

    $check_user = "select * from users where gebruikersnaam='$gebruikersnaam' AND wachtwoord='$wachtwoord'";

    $run =mysql_query($check_user);

    if(mysql_num_rows($run)>0){

    $_SESSION['naam']=$gebruikersnaam;

        echo "<script>window.open('welkom.php','_self')</script>";
    }

This is my HTML code:

<div id=content>
    <form method='POST' action='login.php'>
        Gebruikersnaam:<br>
        <input type='text' name='naam' />
        <br>
        <br>
        Wachtwoord:<br>
        <input type='password' name='wacht' />
        <br>
        <br>
        <input type='submit' name='login' value='Inloggen' />
    </form>
    <br>
    Niet geregistreerd? <a href='registratie.php'>Klik hier.</a>
</div>  

I can't see where my problem is, it says I have a undefined variable on this line: $check_user = "select * from users where gebruikersnaam='$gebruikersnaam' AND wachtwoord='$wachtwoord'";

The names I use are from my database so I wouldn't make a mistake with the names.

Could you people help me? It would be a real time saver because I can't find the problem.

Zagonine
  • 2,213
  • 3
  • 22
  • 29
Gandalf
  • 1
  • 5

2 Answers2

0

You do not check if the following variables are set before you assign them.

$_POST['naam']
$_POST['wacht']

As you did with $_POST['login'] you can use isset to check they exist before assignment.

if (isset($_POST['naam'] && isset($_POST['wacht']) {
    // Do something...
}

In addition to this in the query you are using the variables $gebruikersnaam and $wachtwoord which you don't appear to be referencing anywhere else? So after some google translating I'm guessing that you intended for this bit of code:

$username =mysql_real_escape_string( $_POST['naam']);
$password =md5( $_POST['wacht']);

To be the following:

$gebruikersnaam = mysql_real_escape_string($_POST['naam']);
$wachtwoord     = md5($_POST['wacht']);

Hopefully that helps, just a bit of a side note though, I would really advise reading over http://www.phptherightway.com/ and getting familiar with some of the best practices for PHP.

In your code I would attempt to refactor it and utilise password_hash() and mysqli_* as MD5() is not secure and the mysql_ extension has been removed in the latest version of PHP and was deprecated before that.

Mikey
  • 2,606
  • 1
  • 12
  • 20
  • mmm ok, can i replace the MD5 with password_hash on the same place? – Gandalf Jan 15 '16 at 11:52
  • There is a little bit more to it than that, the manual would be a good place to start: http://php.net/manual/en/function.password-hash.php I would like to give you a bit more information on it, but I don't have the time right now, – Mikey Jan 15 '16 at 12:29
0

Its just a typo I think, you create 2 variables called $username and $password but you are not using them in the query, the query is using $gebruikersnaam and $wachtwoord, which of course have not been defined.

So amend the query to this

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

    $username =mysql_real_escape_string( $_POST['naam']);
    $password =md5( $_POST['wacht']);

    $check_user = "SELECT * 
                   FROM users 
                   WHERE gebruikersnaam='$username' 
                     AND wachtwoord='$password'";

    $run =mysql_query($check_user);

    if(mysql_num_rows($run)>0){

    $_SESSION['naam']=$gebruikersnaam;

        echo "<script>window.open('welkom.php','_self')</script>";
}

Additional Note:

Please dont use the mysql_ database extension, it is deprecated (gone for ever in PHP7) Especially if you are just learning PHP, spend your energies learning the PDO or mysqli_ database extensions, and here is some help to decide which to use

Community
  • 1
  • 1
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • can i simply replace the msql with mysli? – Gandalf Jan 15 '16 at 11:56
  • No I am afraid it is not quite that simple – RiggsFolly Jan 15 '16 at 11:57
  • yea, i have been reading now and it's really complex for me i was just getting the hang of mysql so yea.... – Gandalf Jan 15 '16 at 12:01
  • The problem is the `mysql_` extension no longer exists in the new PHP7. So any code you write with `mysql_` will not run on that version of PHP. There is unfortunately nothing to do other than learn either `mysqli_` or `PDO` before you waste any more time on the `mysql_` extension – RiggsFolly Jan 15 '16 at 12:03