log in

A

Anonymous

Guest
hey all,

bit of a newbie here asking for a little help with some code. At the rate im going im probably gona be making a few requests for help over say ... the next 6 weeks.

Ok im currently doing my dissertation at univeristy and it mainly involves me creating a web site using php and mysql. I did a fair bit back in december, like a search page and a log in page. With the fear of a the deadline in 6 weeks time i decided to start again, a little rusty (although i was never that great to start off with). Anyway i need a little help with perhaos a fairly simple bit of code.

I have a table for the customers called "customers"
+------------+----------------+-------------------+---------
| customerid | name | address | city
+------------+----------------+-------------------+---------
| 1 | Travis Perkins | 12 Trinity Avenue | Notts
+------------+----------------+-------------------+---------

(just one line at the mo)
and a table for the passwords/usernames called "auth"

+----------+-------+------------+
| username | pass | customerid |
+----------+-------+------------+
| testuser | pass | 0 |
| travisp | pass1 | 1 |
+----------+-------+------------+

i have the code for the login page as
-----------START-----------------------
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?
if(!isset($name)&&!isset($password))
{
//Visitor needs to enter a name and password
?>
<h1>Please Log In</h1>
<form method = post action = "login.php">
<table bgcolor=#CCCC99>
<tr>
<td colspan=2>Members log in here:</td>
</tr>
<tr>
<td>Username: </td>
<td><input type = text name = name></td>
</tr>
<tr>
<td>Password: </td>
<td><input type = password name = password></td>
</tr>
<tr>
<td colspan =2 align =center>
<input type = submit value = "Log In">
</td>
</tr>
</table>
</form>

<?
}
else
{
//Connect to mysql
$mysql = mysql_connect('localhost', 'webauth', 'webauth');
if(!$mysql)
{
echo 'Cannot connect to database.';
exit;
}
//select appropriate database
$mysql = mysql_select_db('timbmets');
if(!$mysql)
{
echo 'Cannot select database.';
exit;
}

//query database for username and password
$query = "select count(*) from auth where
username = '$name' and
pass = '$password'";

$result = mysql_query($query);

if(!$result)
{
echo 'Cannot run query.';
exit;
}

$count = mysql_result($result, 0, 0);

if ( $count > 0)
{

//password and name correct
echo "<h1>Hi $valid_user</h1>";
echo "What would you like to do?:";
echo "<br>";
echo "<a href='search.html'>Search</a>";
}
else
{
//password and name incorrect
echo "<h1>Incorrect Username or Password, please try again.</h1>";
}
}
?>
</body>
</html>
------------------FINISH--------------------

what i want to do is to take the company name from the customers table that relates to the same customerid from the auth table so that if they logged in as "travisp" and used their password they would get the welcome screen as stated in the script, but i want it to say "Hi Travis Perkins" or whatever the company name would be that matches the username and password". Ive tried a few approaches but they dont work. Can someone help please

Cheers

Mark
 
For one your code is not written with the assumption that register_globals = off. this probably why it is not working.


Code:
$query = "select * from auth where username = '".$_POST['name']."' and pass = '".$_POST['password']."'"; 

$result = mysql_query($query); 

if(mysql_num_rows($result)) //Counts number of rows and gets content if a row exists
{
$row = mysql_fetch_array($result); //puts the row into an array
echo "<h1>Hi ".$row['name']."</h1>"; 
echo "What would you like to do?:"; 
echo "<br>"; 
echo "<a href='search.html'>Search</a>"; 
}
else 
{ 
//password and name incorrect 
echo "<h1>Incorrect Username or Password, please try again.</h1>"; 
}


see http://www.php-forum.com/p/viewtopic.php?t=1483 it may give a lil info that will save you many hours of scratching your head.
 
i dont want to sound like the ultimate newbie, but by using the code i still dont gt my variable comming up, am i to assume that i should use the session(); thingy at the beginning for it to work?

oh and just to be even more annoying, the code did assume that the name that i was after was from a different table than the passoword table didnt it?

i'm sorry for sounding like a total dunce, ive been out of the programming game for a while ad its taking longer than i want to get back into the groove.

Cheers, Mark :D
 
Back
Top