Forum Moderators: coopster

Message Too Old, No Replies

new to mysql - very simple prob for those who know!

         

nshack31

2:15 pm on Jan 22, 2005 (gmt 0)

10+ Year Member



Hi I have created a site in ODBC - as I would like to teach myself MYSQL I am now converting the site from ODBC into MYSQL. I just need someone to start the ball rolling! my simple login code is as follows, with the prob in BOLD..

<?php
include 'open.php';
if( get_magic_quotes_gpc() ) {
$me=$_REQUEST['username'];
$pass=$_REQUEST['password'];
$me = str_replace( "'", "''", $me );
$pass = str_replace( "'", "''", $pass );
}

mysql_select_db($dbname) or die('Cannot select database');
$query = "SELECT * FROM users WHERE username = '$me' AND password = '$pass'";

$result = mysql_query($query);

$username=mysql_result($result,"username");
$password=mysql_result($result,"password");
$id=mysql_result($result,"id");

if ($username == $me && $password == $pass)
{
setcookie ("id1", $id,time()+36000);
setcookie ("name", $username,time()+36000);
header ('location: clanadmin.php');
}
else
{
header ('location: error.php');
}

include 'close.php';
;?>

Basically the part in bold is not working! I am not being forwared if the username and password match. I am sorry that this is simple to those who know MYSQL but I am completely new to it

nshack31

3:37 pm on Jan 22, 2005 (gmt 0)

10+ Year Member



update.. I've just completed my code conversion from ODBC to MYSQL :) bye bye microsoft!

Still haven't solved this problem in bold though :-(

dreamcatcher

3:38 pm on Jan 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi,

No problem, we are here to help. Nothing is asumed as being easy if you don`t understand it.

Try changing:


$username=mysql_result($result,"username");
$password=mysql_result($result,"password");
$id=mysql_result($result,"id");

if ($username == $me && $password == $pass)

to:

$row = mysql_fetch_object($result);

if ($row->username == $me && $row->password == $pass)

See if that helps.

dc

nshack31

3:46 pm on Jan 22, 2005 (gmt 0)

10+ Year Member



hmm exactly the same problem - it is ignoring the If statement and rather than redirecting the user to clanadmin.php, it produces a blank page

edit.. it appears that the problem is coming from the include open.php part as it is modifying the header

dreamcatcher

5:49 pm on Jan 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Have you made sure that your variables are containing the correct data? Try echoing $me and $pass just before the database query to make sure they are ok.

Also, before your closing PHP tag you have a rogue semi colon.

;?>

One more thing, the include at the end of your code isnt going to do anything because its called after the header functions.

nshack31

6:03 pm on Jan 22, 2005 (gmt 0)

10+ Year Member



Ah ok found the error

$me and $pass were ok, they equaled...

admin and mypass

BUT the following is returning the wrong values..

$username=mysql_result($result,"username");
$password=mysql_result($result,"password");

it returns "1" and "1" when it should be returning "admin" and "mypass"

However, If for test purposes i try a simple select query and select the values that are in the table and use a while loop to display them then it works fine! but obviously i dont want a while loop. It just wont work when I use SELECT FROM WHERE and then try and use this data

..edit the values it returns (1 and 1) are the ID, it is therefore only reading column 1 (ID)

nshack31

7:08 pm on Jan 22, 2005 (gmt 0)

10+ Year Member



i solved it by replacing ..

$username=mysql_result($result,"username");
$password=mysql_result($result,"password");

with

$record= mysql_fetch_assoc($result);
$username = $record['username'];
$password = $record['password'];

I hope thats correct and valid PHP!
Thanks for your help guys, I'm glad I'm now a step further away from microsoft!

dmmh

10:33 pm on Jan 22, 2005 (gmt 0)

10+ Year Member



hope you have some sort of access right checking on clanadmin.php too btw ;)

nshack31

5:52 pm on Jan 23, 2005 (gmt 0)

10+ Year Member



hope you have some sort of access right checking on clanadmin.php too btw ;)

sorry? i dont understand what you mean