Php Help
Moderator: General Moderators
-
MHardeman25
- Forum Commoner
- Posts: 42
- Joined: Mon Jul 12, 2010 10:34 am
Re: Php Help
I'm having a problem with my login in index though. I added some debug lines to test. I have one user already in the database. username: 'Test_User' password: 'testing12345'
and when I try to login as him, it wont take the password for some reason. I've checked everything. The username returns fine, the password doesn't though.
and when I try to login as him, it wont take the password for some reason. I've checked everything. The username returns fine, the password doesn't though.
Re: Php Help
You're missing a key component of your query. You're not fetching the result set.
That will execute the query but you haven't returned the result set yet.
If you're only expecting one row then do this:
In short, if you want to actually use the records you queried, you must fetch the results of the query in to another variable.
Code: Select all
$qry = mysql_query("SELECT * FROM table") or die(mysql_error());If you're only expecting one row then do this:
Code: Select all
$assoc_results = mysql_fetch_assoc($qry) //if you want to get the results by field name
//or
$index_results = mysql_fetch_row($qry) //if you want the results just by index
echo $assoc_results['first_field'];
echo $index_results[0];
//if you want the results of more than one row use the following:
$array_results = mysql_fetch_array($qry); //gives both associative and index results.
//Or you could do this:
While ($row = mysql_fetch_array($qry)) {
echo $row['first_field'].'<br />';
echo $row['second_field'].'<br />';
}
//the above code will loop through the entire array and print out whatever you tell it to.-
MHardeman25
- Forum Commoner
- Posts: 42
- Joined: Mon Jul 12, 2010 10:34 am
Re: Php Help
Isn't that what I did?
Code: Select all
$file_handle = mysql_query('SELECT * FROM tb_users') or die(mysql_error());
while ($db_field = mysql_fetch_assoc($file_handle)){
if( $db_field[$field] == $var ){
return true;
}
}
return false;
Re: Php Help
Ah, there it is. I wasn't making the connection.
First of all.. there's no need to loop through that array.
Try this for your function:
You could even consolidate it further and do this:
Adjust the code in index.php as needed.
Now... how did you plan to keep users logged in without a session?
First of all.. there's no need to loop through that array.
Try this for your function:
Code: Select all
function checkForMatch($file_handle, $field, $var){
$db_field = mysql_fetch_assoc($file_handle)
if( $db_field[$field] == $var ){
return true;
}
else {
return false;
}
}Code: Select all
function checkForMatch($file_handle, $username, $password){
$db_field = mysql_fetch_assoc($file_handle)
if( $db_field['users'] == $username && $db_field['password'] == $password){
return true;
}
else {
return false;
}
}Now... how did you plan to keep users logged in without a session?
-
MHardeman25
- Forum Commoner
- Posts: 42
- Joined: Mon Jul 12, 2010 10:34 am
Re: Php Help
I hadn't even heard about sessions until I got on here and started browsing the forums. I had planed on using some variables like loggedIn and IP. Basically, when the user logs in, loggedIn becomes 1 and we record the users IP, then send them over to the next page. There we look at their IP, if it matches that of a logged in users then that is the same user. There are so many ways that could mess up, but it was the only thing I could think of. I did hear about sessions while browsing the forums, and I think I'll start looking into that. Prolly will need some help later on though. Keep in mind, I just started PHP about 2 days ago. I don't know even half of the resources available to me yet.
As for the problem, that doesn't seem to work. Here is the pseudo code for what happens when the user clicks the button.
basically the checkForMatch(username) works just fine, it sends the variables off and Test_User == Test_User and it returns true. However, the checkForMatch(password) doesn't seem to run at all. It returns false, it doesn't print off any of the debug lines that I have in that function. I have no idea what happens there.
As for the problem, that doesn't seem to work. Here is the pseudo code for what happens when the user clicks the button.
Code: Select all
$file_handle = query('SELECT * FROM tb_users'); //get tb_users
//if it username and password match
if( checkForMatch($file_handle, 'username', $username) && checkForMatch($file_handle, 'password', $password) ){
closeDatabase(); //close database
header('Location: main.php'); //goto the main page
}
print 'Username or password are incorrect <br>';
-
MHardeman25
- Forum Commoner
- Posts: 42
- Joined: Mon Jul 12, 2010 10:34 am
Re: Php Help
if I use
then it prints at the top
Field = username Value = Test_User
Field = password Value = testing12345
Field = first_name Value = John
Field = last_name Value = Doe
Field = total_uploaded_kb Value = 4
Field = email Value = example1@test.com
Test_User Test_User
Warning: Invalid argument supplied for foreach() in C:\Program Files (x86)\EasyPHP-5.3.2i\www\Webbox\data\SQL_Connect.php on line 52
testing12345
Username or password are incorrect
Code: Select all
function checkForMatch($file_handle, $field, $var){
$db_field = mysql_fetch_assoc($file_handle);
foreach ($db_field as $element => $value) {
print "Field = " . $element . " Value = " . $value . "<BR>";
}
echo $db_field[$field].' '.$var.'<br>';
if( $db_field[$field] == $var ){
return true;
} else {
return false;
}
}
Field = username Value = Test_User
Field = password Value = testing12345
Field = first_name Value = John
Field = last_name Value = Doe
Field = total_uploaded_kb Value = 4
Field = email Value = example1@test.com
Test_User Test_User
Warning: Invalid argument supplied for foreach() in C:\Program Files (x86)\EasyPHP-5.3.2i\www\Webbox\data\SQL_Connect.php on line 52
testing12345
Username or password are incorrect
-
MHardeman25
- Forum Commoner
- Posts: 42
- Joined: Mon Jul 12, 2010 10:34 am
Re: Php Help
So for some reason, it can read the username from the SQL fine, but it cant read the password for some reason. I don't quite get why.
Re: Php Help
Ok, so if echo out the value of the password field what do you get?
Likewise, if you query the database password field and echo the result, what do you get?
Just for kicks. Go in to the database and change that password.
I also noticed you're not encrypting your passwords but you can get to that later.
Since code changes have been made, can you repost all the relevant code again? Then I can get a fresh look at it.
And you're doing a great job for just learning php. Seriously.
Likewise, if you query the database password field and echo the result, what do you get?
Just for kicks. Go in to the database and change that password.
I also noticed you're not encrypting your passwords but you can get to that later.
Since code changes have been made, can you repost all the relevant code again? Then I can get a fresh look at it.
And you're doing a great job for just learning php. Seriously.
-
MHardeman25
- Forum Commoner
- Posts: 42
- Joined: Mon Jul 12, 2010 10:34 am
Re: Php Help
Ok, here is the whole website as is
I know it looks like crap. I haven't really worked on looks. Right now it's just bare bones (I also learned css in the making of this).
I know it looks like crap. I haven't really worked on looks. Right now it's just bare bones (I also learned css in the making of this).
Last edited by MHardeman25 on Mon Jul 12, 2010 9:28 pm, edited 1 time in total.
Re: Php Help
Check your private messages here. I sent you my email address. Add me to Google Talk if you have it.
-
MHardeman25
- Forum Commoner
- Posts: 42
- Joined: Mon Jul 12, 2010 10:34 am
Re: Php Help
Resolved:
Apparently each query can be used only once.
Apparently each query can be used only once.