...

View Full Version : print(); problems please read...



DiarYofaMadmaN
05-01-2004, 12:08 AM
Code for the form page: (showing you I do have the username and password)

CODE


<form action="logincheck.php" method="post">
<table width="229" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#006BB3" style="border:2px solid black">
<tr bgcolor="#FFFFCC">
<td colspan="2"><div align="center"><strong>Login</strong></div></td>
</tr>
<tr bgcolor="#006BB3">
<td width="90"><strong><span class="style1">Username:</span></strong></td>
<td width="139"><input name="username" type="text" id="username"></td>
</tr>
<tr bgcolor="#006BB3">
<td><strong>Password:</strong></td>
<td><input name="password" type="text" id="password"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><div align="right">
<input type="submit" name="Submit" value="Login">
</div></td>
</tr>
</table>
</form>




Coding for matching the variables to database. IF found then print("Welcome usernamehere");


CODE


<?php
session_start();
header("Cache-control: private"); //IE6 Fix
/* Connecting */
// Variables for the database access + table information:
//not show here for security purposes

$link = mysql_connect($Host, $User, $Password);

// Running the Query //
$Query = "SELECT * FROM $TableName WHERE Login_Name = '" & $_POST['username'] & "' AND Login_Password = '" & $_POST['password'] & "'";
$Result = mysql_db_query ($DBName, $Query, $Link);

// Checking if info matches database results //
print("$_POST['username'] && $_POST['password']"); //used for error trapping
while($Row = mysql_fetch_array($Result)){
print("row"); //use for error trapping
if($_POST['username'] == $Row["Login_Name"] && $_POST['password'] == $Row["Login_Password"]) {
print("Welcome $_POST['username']");
}
Else {
print("Couldn't find you in the database");
}
}
mysql_close ($link);
?>



I used two prints for error trapping the first one is:
print("$_POST['username'] && $_POST['password']"); //use for error trapping which is supposed to grab the the username and password in the top form code, and the second:
print("row"); //use for error trapping which is supposed to print out row as many time as the rows I have. Well, That's not being printed out. Which brings me to thinking maybe the query syntax isn't correct:
$Query = "SELECT * FROM $TableName WHERE Login_Name = '" & $_POST['username'] & "' AND Login_Password = '" & $_POST['password'] & "'"; or that could be correct and the $_POSTs aren't working.

I have no clue. Sorry for such a long post but I didn't want to fill multiple threads and just ask all at once in one thread. Thank you for your time

-Thanks,
Rich

missing-score
05-01-2004, 12:19 AM
could it be that you are selecting from the database only where the username and password are equal to the post variables?

firepages
05-01-2004, 02:04 AM
..a few things , as missing-score points out , if you are SELECTing against the username and password fields , you are only going to receive 1 row maximum from the database so you do not need to loop through it , you simply need to check if you actually got a result or not ...



<?
session_start();
header("Cache-control: private"); //IE6 Fix
?>
note that mysql_db_query() is depracated ,
use mysql_select_db() and mysql_query() instead
<?
$link = mysql_connect($Host, $User, $Password);
mysql_select_db($DBname);

$Query = "
SELECT * FROM $TableName
WHERE Login_Name = '".$_POST['username']."'
AND Login_Password = '".$_POST['password']."'
";
$Result = mysql_query ( $Query , $Link ) ;

echo $_POST['username'].' && '.$_POST['password'];
?>
If we got a result of any sort , fetch the row , else don't bother
<?
if(@mysql_num_rows($Result)){
$Row = mysql_fetch_array($Result) ;
print_r($Row);
echo 'Welcome' .$_POST['username'] ;
}else{
echo 'Couldn\'t find you in the database' ;
}
?>


note also that depending on your PHP version you can substitute ..

$Query = "
SELECT * FROM $TableName
WHERE Login_Name = '".$_POST['username']."'
AND Login_Password = '".$_POST['password']."'
";

for

$Query = "
SELECT * FROM $TableName
WHERE Login_Name = '{$_POST['username']}'
AND Login_Password = '{$_POST['password']}'
";

DiarYofaMadmaN
05-01-2004, 02:08 AM
Well what I wanted to do was scan through the database and if the username and password matches then do something...

if($link = mysql_connect ($Host, $User, $Password)) {
print("Connected to database");
print("$_POST["username"] $_POST["password"]");
}

Another issue i ran across is the $_POST syntax problem. I'm not too sure why $username and $password works but $_POST["username"] and $_POST["password"] doesn't work. Thanks for the help.

-peace,
Rich

firepages
05-01-2004, 02:21 AM
<edit>
regardless of the below
print("$_POST["username"] $_POST["password"]");
is wrong and will cause a parse error
you have to
print($_POST['username']. " " . $_POST['password']);
or (if you have a later version of PHP)
print("{$_POST['username']} {$_POST['password']}");
</edit>


what verision of PHP are you running ? <?echo phpversion();?>

if <=4.06 , at the top of your scripts add this ..


<?
$_POST=$HTTP_POST_VARS;
?>


you could also use the echo $HTTP_POST_VARS['varname'] ; syntax but that too is depracated , using the above will future proof your scripts.

DiarYofaMadmaN
05-01-2004, 02:48 AM
Great! eheh even before you posted back i was playing around with the syntax and did what you said: print($_POST['username']. " " . $_POST['password']); and it worked. Except, I'm still getting Query couldn't not execute on the page. I used an if statement to see if the query executed and it didn't. Hrm, I check my database and there is a database called maxforcepc and a table called adminaccounts, and there also are two columns called Login_Name and Login_Password. I'm stumped....

edit:
I also recieved a print out of both my username and password that i entered in the html form so the variables are correct and getting set.
-Thanks,
Rich

firepages
05-01-2004, 03:10 AM
hi use MySQL to let you know whats wrong .. after any MySQL command you can call mysql_error() to get the last returned error message ...



<?
$Result = mysql_query ( $Query , $Link ) or die(mysql_error());
?>


see what that produces .

DiarYofaMadmaN
05-01-2004, 03:31 AM
hi use MySQL to let you know whats wrong .. after any MySQL command you can call mysql_error() to get the last returned error message ...



<?
$Result = mysql_query ( $Query , $Link ) or die(mysql_error());
?>


see what that produces .

it doesn't produce anything, now my page comes up with nothing.. So there's a parse error somewhere which is probably at: $Result = mysql_query ( $Query , $Link ) or die(mysql_error()); .... By the way my PHP version is: 4.3.3. I even tried echo mysql_error(); on it's own seperate line but it didn't print out anything. I'm assuming that means there is no error?

DiarYofaMadmaN
05-01-2004, 05:49 AM
$link = mysql_connect($Host, $User, $Password);
mysql_select_db($DBname, $link);

$Query = "
SELECT * FROM $TableName
WHERE Login_Name = '".$_POST['username']."'
AND Login_Password = '".$_POST['password']."'
";

$result = mysql_query($Query, $link);
$num_rows = mysql_num_rows($result);

echo "$num_rows Rows\n";

I did that to see how many rows came back matching the info I posted on the form. I went and added the user name Test and password Test to the database. if you go to www.maxforcepc.com/edit/login.php and insert Test in the username and Test in the password field and click login, it only shows Row and nothing else. Do you think the Query is wrong? How can I fix this problem?

firepages
05-01-2004, 07:07 AM
try this to get an error message of some sort..


<?
$link = mysql_connect($Host, $User, $Password)or die(mysql_error());
mysql_select_db($DBname, $link) or die(mysql_error());

$Query = "
SELECT * FROM $TableName
WHERE Login_Name = '".$_POST['username']."'
AND Login_Password = '".$_POST['password']."'
";

$result = mysql_query($Query, $link)or die(mysql_error());

if(@mysql_num_rows($result)){
print_r(mysql_fetch_assoc($result));
}else{
echo "username & password not found or incorrect";
}
?>

DiarYofaMadmaN
05-01-2004, 11:34 PM
Thanks for the hint! I had a syntax error :-) It said Database not selected so I went and looked up the function and found out i was missing the $DBname :-). I added that and it works great! Thanks agian :-)

Now, I'm back to my same problem as before... The only reason why I wanted to print out how many rows matched the query is to check if my query is correct and it is...

My code now is:

$link = mysql_connect ($Host, $User, $Password);

$Query = "
SELECT * FROM $TableName
WHERE Login_Name = '".$_POST['username']."'
AND Login_Password = '".$_POST['password']."'
";

$result = mysql_query ($DBName, $Query, $link);

if(@mysql_num_rows($Result)) {
print_r(mysql_fetch_assoc($result));
echo 'Welcome' .$_POST['username'];
} else {
echo 'Access denied. Could not find that account. Please click back and try agian.';
}

I still recieve:
Access denied. Could not find that account. Please click back and try agian. ahk this is driving me up the wall but I will figure it out with some help problem ehhh.. If you can help me agian that would be greatly appreciated :-)

I'm thinking maybe i'm not supposed to be using mysql_num_rows because that gets the number of rows affected by the query? Or am I wrong?
-Thanks,
Rich

DiarYofaMadmaN
05-02-2004, 02:48 AM
Well i fixed the problem I used:


$link = mysql_connect ($Host, $User, $Password);

$Query = "
SELECT * FROM $TableName
WHERE Login_Name = '".$_POST['username']."'
AND Login_Password = '".$_POST['password']."'
";
$Result = mysql_db_query ($DBName, $Query, $link);

if ((mysql_numrows($Result) > 0)) {
echo 'worked';
}

and that worked wonders. I really don't undersatnd why I was having such a hard time the other way...

-peace

missing-score
05-02-2004, 03:03 AM
As has already been mentioned, you should use mysql_select_db() and mysql_query(), not mysql_db_query().

This is how you should rewrite the code:


$link = mysql_connect ($Host, $User, $Password);
mysql_select_db($DBName, $link);

$Query = "
SELECT * FROM $TableName
WHERE Login_Name = '".$_POST['username']."'
AND Login_Password = '".$_POST['password']."'
";
$Result = mysql_query ($Query);

if ((mysql_numrows($Result) > 0)) {
echo 'worked';
}

DiarYofaMadmaN
05-02-2004, 03:48 AM
Ok cool, it's all changed now...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum