This is my code :
[syntax=php]<body>
<?php
include 'config.php' ;
if (isset($_POST['username'], $_POST['password'])) {
$username = $_POST['username'] ;
$password = md5($_POST['password']) ;
$errors = array() ;
if (empty ($username) && empty ($password) ) {
$errors[] = 'Please enter your username and password';
} else
if (empty ($username)) {
$errors[] = 'Please enter your username';
} else
if (empty($password)) {
$errors[] = 'Please enter your password' ;
} else {
$login_check = mysql_query("SELECT `id` FROM `users` WHERE `username`='$username' AND `password`='$password'");
$rows = mysql_num_rows ($login_check);
if ($rows == 0) {
$errors[] = 'Incorrect username/password' ;
} else if ($rows == 1) {
$_SESSION['user_id'] = $user_id;
header('Location: profile.php');
exit() ;
}
} /* else if (!empty ($errors)) {
foreach ($errors as $error) {
echo $error ; */
/* I want this to be below in the div tag.
If I put it in the div, it won't work.
I need to place it where it'll work! */
}
}
?>
<!--The whole body is wrapped in 'whole_body_wrapper'-->
<div id="whole_body_wrapper">
<!--Header with login form and title-->
<div id="main_header">
<h1>Budzzem</h1>
<!--Login form-->
<div id="login_form">
<div id="phpErrors" style="color:#F00"><?php //HERE ?></div>
<form action="" method="POST">
<input type="text" class="login_inputs" placeholder="Username" name="username" />
<input type="password" value="" class="login_inputs" placeholder="Password" name="password"/>
<input type="submit" value="Sign In" class="login_inputs" id="signInBtn" />
</form>
</div>
</div>[/syntax]
I don't know where to place the foreach loop! I want the errors to be viewed in the div tag where 'HERE' is commented!
Thank you!
Login form - Viewing Errors!
Login form - Viewing Errors!
Just A PHP Beginner!
Re: Login form - Viewing Errors!
[syntax=php]<div id="phpErrors" style="color:#F00"><?php if(!empty($errors)){
foreach($errors as $error){
echo $error;
}
}?></div>
[/syntax]
I don't see why that wouldn't work.
foreach($errors as $error){
echo $error;
}
}?></div>
[/syntax]
I don't see why that wouldn't work.
Re: Login form - Viewing Errors!
Temor wrote:[syntax=php]<div id="phpErrors" style="color:#F00"><?php if(!empty($errors)){
foreach($errors as $error){
echo $error;
}
}?></div>
[/syntax]
I don't see why that wouldn't work.
Thanks! =)
Just A PHP Beginner!
Re: Login form - Viewing Errors!
One more thing!
[syntax=php]if (empty ($username) || empty ($password)) {
$errors[] = 'Please enter your username and password';
} else {
$login_check = mysql_query("SELECT `id` FROM `users` WHERE `username`='$username' AND `password`='$password'");
$rows = mysql_num_rows ($login_check);
if ($rows == 0) {
$errors[] = 'Incorrect username/password' ;
} else if ($rows == 1) {
$_SESSION['user_id'] = $user_id;
header('Location: profile.php?id='.$user_id.'');
exit() ;
}
} [/syntax]
When I login, I get this error : Warning: mysql_result() [function.mysql-result]: Unable to jump to row 0 on MySQL result index 7 in C:\xampp\htdocs\Budzzem\func.php on line 6
And the URL is profile.php?id=
[syntax=php]if (empty ($username) || empty ($password)) {
$errors[] = 'Please enter your username and password';
} else {
$login_check = mysql_query("SELECT `id` FROM `users` WHERE `username`='$username' AND `password`='$password'");
$rows = mysql_num_rows ($login_check);
if ($rows == 0) {
$errors[] = 'Incorrect username/password' ;
} else if ($rows == 1) {
$_SESSION['user_id'] = $user_id;
header('Location: profile.php?id='.$user_id.'');
exit() ;
}
} [/syntax]
When I login, I get this error : Warning: mysql_result() [function.mysql-result]: Unable to jump to row 0 on MySQL result index 7 in C:\xampp\htdocs\Budzzem\func.php on line 6
And the URL is profile.php?id=
Just A PHP Beginner!
Re: Login form - Viewing Errors!
Well, the error does not make much sense to me to be honest.
I can't see the mysql_result function being used anywhere so I can't see how it can fail :S
$user_id is not set anywhere. What you need to do is store the ID you took from the database and then use that to put in the $_SESSION['user_id'] variable.
I can't see the mysql_result function being used anywhere so I can't see how it can fail :S
$user_id is not set anywhere. What you need to do is store the ID you took from the database and then use that to put in the $_SESSION['user_id'] variable.
Re: Login form - Viewing Errors!
I'll try to fix it. But what about this :
[syntax=php]
if ($rows == 1) {
$_SESSION['user_id'] = $user_id;
header('Location: profile.php?id='.$_SESSION['user_id'].'');
exit() ;
} [/syntax]
When I do this, It takes me to this page : profile?id=
Any problem with sessions?
[syntax=php]
if ($rows == 1) {
$_SESSION['user_id'] = $user_id;
header('Location: profile.php?id='.$_SESSION['user_id'].'');
exit() ;
} [/syntax]
When I do this, It takes me to this page : profile?id=
Any problem with sessions?
Just A PHP Beginner!
Re: Login form - Viewing Errors!
where are you giving $user_id a value? if you're not giving it a value, it will be blank, which means you're making $_SESSION['user_id'] an empty variable.
you need to fetch the users ID and then store it in the $user_id variable.
you need to fetch the users ID and then store it in the $user_id variable.
Re: Login form - Viewing Errors!
This is my complete code :
[syntax=php]<?php
include 'init.php' ;
session_start() ;
if (isset($_POST['username'], $_POST['password'])) {
$username = $_POST['username'] ;
$password = md5($_POST['password']) ;
$errors = array() ;
if (empty ($username) || empty ($password)) {
$errors[] = 'Please enter your username and password';
} else {
$login_check = mysql_query("SELECT `id` FROM `users` WHERE `username`='$username' AND `password`='$password'");
$rows = mysql_num_rows ($login_check);
if ($rows == 0) {
$errors[] = 'Incorrect username/password' ;
} else if ($rows == 1) {
$_SESSION['user_id'] = $user_id;
header('Location: profile.php?id='.$_SESSION['user_id'].'');
exit() ;
}
}
}
?>[/syntax]
What more should I add?
[syntax=php]<?php
include 'init.php' ;
session_start() ;
if (isset($_POST['username'], $_POST['password'])) {
$username = $_POST['username'] ;
$password = md5($_POST['password']) ;
$errors = array() ;
if (empty ($username) || empty ($password)) {
$errors[] = 'Please enter your username and password';
} else {
$login_check = mysql_query("SELECT `id` FROM `users` WHERE `username`='$username' AND `password`='$password'");
$rows = mysql_num_rows ($login_check);
if ($rows == 0) {
$errors[] = 'Incorrect username/password' ;
} else if ($rows == 1) {
$_SESSION['user_id'] = $user_id;
header('Location: profile.php?id='.$_SESSION['user_id'].'');
exit() ;
}
}
}
?>[/syntax]
What more should I add?
Just A PHP Beginner!
Re: Login form - Viewing Errors!
Line 17 and 18:
[syntax=php]} else if ($rows == 1) {
$_SESSION['user_id'] = $user_id;[/syntax]
you're giving $_SESSION['user_id'] the value that is inside $user_id;
$user_id is not given a value, which makes it empty, which makes the code above useless. You're just making sure that $_SESSION['user_id'] is empty.
On line 13, you're running a query to get the user ID from the database, but you never assign the ID to a variable.
What you need to do is run
[syntax=php]mysql_result($login_check,0);[/syntax]
and store that result in the $user_id variable.
[syntax=php]} else if ($rows == 1) {
$_SESSION['user_id'] = $user_id;[/syntax]
you're giving $_SESSION['user_id'] the value that is inside $user_id;
$user_id is not given a value, which makes it empty, which makes the code above useless. You're just making sure that $_SESSION['user_id'] is empty.
On line 13, you're running a query to get the user ID from the database, but you never assign the ID to a variable.
What you need to do is run
[syntax=php]mysql_result($login_check,0);[/syntax]
and store that result in the $user_id variable.