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

Re: Code Issue

Postby TheHappyPeanut » Thu Apr 12, 2012 10:02 am

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?
Syntax: [ Download ] [ Hide ]
var_dump($_POST);


There are no changes after placing that into the code. Is there a specific place I need to put it?
TheHappyPeanut
Forum Commoner
 
Posts: 50
Joined: Wed Apr 11, 2012 8:54 am
Location: United States

Re: Code Issue

Postby Celauran » Thu Apr 12, 2012 10:05 am

You just need to be sure it's executing. May as well place it right at the top. var_dump always produces output.
User avatar
Celauran
Moderator
 
Posts: 2785
Joined: Tue Nov 09, 2010 3:39 pm
Location: Montreal, Canada

Re: Code Issue

Postby TheHappyPeanut » Thu Apr 12, 2012 10:17 am

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

Re: Code Issue

Postby Celauran » Thu Apr 12, 2012 10:19 am

Absolutely.

Syntax: [ Download ] [ Hide ]
SELECT columnA, columnB FROM users WHERE username = 'provided_username' OR email = 'provided_username'
User avatar
Celauran
Moderator
 
Posts: 2785
Joined: Tue Nov 09, 2010 3:39 pm
Location: Montreal, Canada

Re: Code Issue

Postby TheHappyPeanut » Thu Apr 12, 2012 10:23 am

Celauran wrote:Absolutely.

Syntax: [ Download ] [ Hide ]
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?
TheHappyPeanut
Forum Commoner
 
Posts: 50
Joined: Wed Apr 11, 2012 8:54 am
Location: United States

Re: Code Issue

Postby Celauran » Thu Apr 12, 2012 10:28 am

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

Re: Code Issue

Postby TheHappyPeanut » Thu Apr 12, 2012 10:37 am

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

Postby TheHappyPeanut » Fri Apr 13, 2012 7:44 am

Would it look something like this?

Syntax: [ Download ] [ Hide ]
$query = mysql_query ("SELECT columnA, columnB FROM users WHERE username = 'username' OR email = 'username'");
TheHappyPeanut
Forum Commoner
 
Posts: 50
Joined: Wed Apr 11, 2012 8:54 am
Location: United States

Re: Code Issue

Postby Celauran » Fri Apr 13, 2012 7:53 am

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.
Syntax: [ Download ] [ Hide ]
$query  = "SELECT columnA, columnB FROM users WHERE username = '{$username}' OR email = '{$username}'";
$result = $sql->query($query);
User avatar
Celauran
Moderator
 
Posts: 2785
Joined: Tue Nov 09, 2010 3:39 pm
Location: Montreal, Canada

Re: Code Issue

Postby TheHappyPeanut » Fri Apr 13, 2012 8:06 am

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.
Syntax: [ Download ] [ Hide ]
$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.

Syntax: [ Download ] [ Hide ]
<?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");    
}
   
?>
TheHappyPeanut
Forum Commoner
 
Posts: 50
Joined: Wed Apr 11, 2012 8:54 am
Location: United States

Re: Code Issue

Postby Celauran » Fri Apr 13, 2012 8:10 am

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.

Syntax: [ Download ] [ Hide ]
<?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.
User avatar
Celauran
Moderator
 
Posts: 2785
Joined: Tue Nov 09, 2010 3:39 pm
Location: Montreal, Canada

Re: Code Issue

Postby TheHappyPeanut » Fri Apr 13, 2012 8:12 am

Yeah, I'm not getting the die() message. I tried echoing and that function works fine.
TheHappyPeanut
Forum Commoner
 
Posts: 50
Joined: Wed Apr 11, 2012 8:54 am
Location: United States

Re: Code Issue

Postby Celauran » Fri Apr 13, 2012 8:15 am

That would suggest that the first if is failing, meaning $_POST is empty. var_dump($_POST) to confirm.
User avatar
Celauran
Moderator
 
Posts: 2785
Joined: Tue Nov 09, 2010 3:39 pm
Location: Montreal, Canada

Re: Code Issue

Postby TheHappyPeanut » Fri Apr 13, 2012 8:19 am

This is what I get when I try that:

Syntax: [ Download ] [ Hide ]
Parse error: syntax error, unexpected T_IF in C:\xampp\htdocs\Tracker\login.php on line 5
TheHappyPeanut
Forum Commoner
 
Posts: 50
Joined: Wed Apr 11, 2012 8:54 am
Location: United States

Re: Code Issue

Postby Celauran » Fri Apr 13, 2012 8:20 am

Without seeing what line 5 is (and the surrounding lines, really), I can't help you much.
User avatar
Celauran
Moderator
 
Posts: 2785
Joined: Tue Nov 09, 2010 3:39 pm
Location: Montreal, Canada

PreviousNext

Return to PHP - Code

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 21 guests