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
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