Page 2 of 6
Re: Code Issue
Posted: Thu Apr 12, 2012 10:02 am
by TheHappyPeanut
Celauran wrote:TheHappyPeanut wrote:Furthermore, the "Incorrect information" string is still there, even though I 100% used the correct user and password.
What does this give you?
There are no changes after placing that into the code. Is there a specific place I need to put it?
Re: Code Issue
Posted: Thu Apr 12, 2012 10:05 am
by Celauran
You just need to be sure it's executing. May as well place it right at the top. var_dump always produces output.
Re: Code Issue
Posted: Thu Apr 12, 2012 10:17 am
by TheHappyPeanut
I placed it at the top and it worked. I actually figured out my problem; I originally had the text-box use "email-address" when it should have used "username". That brings me to another question. Let's say that I have the following database fields:
- id
- email
- username
- password
How can I make it so that a user can log in if they provide either the email or the username. For example, a person could log in using "John" or "
john@example.com".
Re: Code Issue
Posted: Thu Apr 12, 2012 10:19 am
by Celauran
Absolutely.
Code: Select all
SELECT columnA, columnB FROM users WHERE username = 'provided_username' OR email = 'provided_username'
Re: Code Issue
Posted: Thu Apr 12, 2012 10:23 am
by TheHappyPeanut
Celauran wrote:Absolutely.
Code: Select all
SELECT columnA, columnB FROM users WHERE username = 'provided_username' OR email = 'provided_username'
So basically, that code saying "Username no longer equal to username. Username is equal to username or password"? And where would that code go? Is there any easy way to learn where I place this code other than trial and error and a lot of reading?
Re: Code Issue
Posted: Thu Apr 12, 2012 10:28 am
by Celauran
If you recognized that that's SQL and not PHP, then simply use it as a query string (obviously replacing the dummy values with real ones). If not, you will want to
become familiar with SQL.
Re: Code Issue
Posted: Thu Apr 12, 2012 10:37 am
by TheHappyPeanut
I did recognize it dealt with MySQL, but I haven't read up on that yet. I'll do that now. I REALLY appreciate your help.
Re: Code Issue
Posted: Fri Apr 13, 2012 7:44 am
by TheHappyPeanut
Would it look something like this?
Code: Select all
$query = mysql_query ("SELECT columnA, columnB FROM users WHERE username = 'username' OR email = 'username'");
Re: Code Issue
Posted: Fri Apr 13, 2012 7:53 am
by Celauran
Replacing columnA, columnB and 'username' with actual values, yes.
I also find it helpful for debugging to store the query itself as a variable, rather than just the result.
Code: Select all
$query = "SELECT columnA, columnB FROM users WHERE username = '{$username}' OR email = '{$username}'";
$result = $sql->query($query);
Re: Code Issue
Posted: Fri Apr 13, 2012 8:06 am
by TheHappyPeanut
Celauran wrote:Replacing columnA, columnB and 'username' with actual values, yes.
I also find it helpful for debugging to store the query itself as a variable, rather than just the result.
Code: Select all
$query = "SELECT columnA, columnB FROM users WHERE username = '{$username}' OR email = '{$username}'";
$result = $sql->query($query);
So the columns are actually my row values in the database?
Also, it seems as if something else went wrong. It shows a blank page when you log in regardless of whether you put in no information, incorrect information, or correct information.
Code: Select all
<?php
if (!empty($_POST))
{
if (isset($_POST['username']) && isset($_POST['password']))
{
$connect = mysql_connect("localhost","root","") or die("Could not connect");
mysql_select_db("tracker") or die("Could not find database");
}
else
die("Incorrect information");
}
?>
Re: Code Issue
Posted: Fri Apr 13, 2012 8:10 am
by Celauran
TheHappyPeanut wrote:So the columns are actually my row values in the database?
They're the columns you want returned
TheHappyPeanut wrote:Also, it seems as if something else went wrong. It shows a blank page when you log in regardless of whether you put in no information, incorrect information, or correct information.
Code: Select all
<?php
if (!empty($_POST))
{
if (isset($_POST['username']) && isset($_POST['password']))
{
$connect = mysql_connect("localhost","root","") or die("Could not connect");
mysql_select_db("tracker") or die("Could not find database");
}
else
die("Incorrect information");
}
?>
So you're not even getting the die() message? Try echoing some dummy text at the very top of the page to make sure the page is loading. If not, check your form's action against the page name.
Re: Code Issue
Posted: Fri Apr 13, 2012 8:12 am
by TheHappyPeanut
Yeah, I'm not getting the die() message. I tried echoing and that function works fine.
Re: Code Issue
Posted: Fri Apr 13, 2012 8:15 am
by Celauran
That would suggest that the first if is failing, meaning $_POST is empty. var_dump($_POST) to confirm.
Re: Code Issue
Posted: Fri Apr 13, 2012 8:19 am
by TheHappyPeanut
This is what I get when I try that:
[text]Parse error: syntax error, unexpected T_IF in C:\xampp\htdocs\Tracker\login.php on line 5[/text]
Re: Code Issue
Posted: Fri Apr 13, 2012 8:20 am
by Celauran
Without seeing what line 5 is (and the surrounding lines, really), I can't help you much.