Code Issue

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

TheHappyPeanut
Forum Commoner
Posts: 50
Joined: Wed Apr 11, 2012 8:54 am
Location: United States

Re: Code Issue

Post 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?

Code: Select all

var_dump($_POST);
There are no changes after placing that into the code. Is there a specific place I need to put it?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Code Issue

Post 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.
TheHappyPeanut
Forum Commoner
Posts: 50
Joined: Wed Apr 11, 2012 8:54 am
Location: United States

Re: Code Issue

Post 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".
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Code Issue

Post by Celauran »

Absolutely.

Code: Select all

SELECT columnA, columnB FROM users WHERE username = 'provided_username' OR email = 'provided_username'
TheHappyPeanut
Forum Commoner
Posts: 50
Joined: Wed Apr 11, 2012 8:54 am
Location: United States

Re: Code Issue

Post 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?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Code Issue

Post 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.
TheHappyPeanut
Forum Commoner
Posts: 50
Joined: Wed Apr 11, 2012 8:54 am
Location: United States

Re: Code Issue

Post 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.
TheHappyPeanut
Forum Commoner
Posts: 50
Joined: Wed Apr 11, 2012 8:54 am
Location: United States

Re: Code Issue

Post 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'");
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Code Issue

Post 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);
TheHappyPeanut
Forum Commoner
Posts: 50
Joined: Wed Apr 11, 2012 8:54 am
Location: United States

Re: Code Issue

Post 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");    
}
   
?>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Code Issue

Post 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.
TheHappyPeanut
Forum Commoner
Posts: 50
Joined: Wed Apr 11, 2012 8:54 am
Location: United States

Re: Code Issue

Post by TheHappyPeanut »

Yeah, I'm not getting the die() message. I tried echoing and that function works fine.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Code Issue

Post by Celauran »

That would suggest that the first if is failing, meaning $_POST is empty. var_dump($_POST) to confirm.
TheHappyPeanut
Forum Commoner
Posts: 50
Joined: Wed Apr 11, 2012 8:54 am
Location: United States

Re: Code Issue

Post 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]
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Code Issue

Post by Celauran »

Without seeing what line 5 is (and the surrounding lines, really), I can't help you much.
Post Reply