This question already has an answer here:
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.
</div>
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.
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 thePDO
ormysqli_
database extensions, and here is some help to decide which to use