|
-
Oct 9th, 2006, 09:03 PM
#1
Thread Starter
WiggleWiggle
[RESOLVED] Set user_id on login
Hey there,
i have this login code, works great, but i need to have it get the user_id from the db and set is as $logged_in_userid
here is the code that i have:
PHP Code:
<?PHP
$links = "<A HREF='main.php'>Click here to proceed to the main page</A><BR>
You Will Be Redirected In 2 Seconds.<br>
<META HTTP-EQUIV='refresh' content='2;URL=http://customer.homtek.net/main.php'>";
if ($username && $pass) {
if ($logged_in_customer == $username) {
echo $username.", you are already logged in.<BR><BR>";
echo $links;
exit;
}
$dbh = mysql_connect ("localhost", "homtek_dclamp", "moiola") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("homtek_homtek");
$result = mysql_query("SELECT * FROM customer_login WHERE username = '".$username."'
AND password = PASSWORD('".$pass."')");
if (!$result) {
echo "Sorry, there has been an error. Please try again, or contact technical support.";
exit;
}
if (mysql_num_rows($result) > 0) {
$logged_in_customer = $username;
session_register("logged_in_customer");
echo "Welcome, ".$logged_in_customer.". <BR><BR>";
echo $links;
exit;
} else {
echo "<font color=red>Invalid login. Please try again.</font><BR><BR>";
}
} else if ($username || $pass) {
echo "<font color=red>Please fill in both fields.</font><BR><BR>";
}
?>
<FORM METHOD=POST ACTION="login.php">
Your username:
<INPUT NAME="username" TYPE=TEXT MAXLENGTH=20 SIZE=20>
<BR>
Your password:
<INPUT NAME="pass" TYPE=PASSWORD MAXLENGTH=10 SIZE=10>
<BR>
<INPUT TYPE=SUBMIT VALUE="Login">
</FORM>
My usual boring signature: Something
-
Oct 9th, 2006, 09:12 PM
#2
Re: Set user_id on login
just query the database and grab that field.. when they are successfully logged in.
here is a changed bit of your code, from when the login is successful:
PHP Code:
if (mysql_num_rows($result) > 0) {
$logged_in_customer = $username;
session_register("logged_in_customer");
echo "Welcome, ".$logged_in_customer.". <BR><BR>";
echo $links;
//query the database for the user_id
$useridq = mysql_query("SELECT user_id FROM tablename WHERE customer='" . $logged_in_customer . "' LIMIT 1");
$userid = mysql_fetch_array($useridq);
echo $userid['user_id'];
exit;
-
Oct 9th, 2006, 09:42 PM
#3
Thread Starter
WiggleWiggle
Re: Set user_id on login
thanks!. is there a way that i can do a query for 2 diffrent tables? i have register code and i need to have info go to 2 diffrent tables.
here is the code that i am using:
PHP Code:
<?PHP
if ($customer && $pass) {
$dbh=mysql_connect ("localhost", "homtek_dclamp", "password") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("homtek_homtek");
$result = mysql_query ("SELECT * FROM customer_login WHERE username = '".$customer."'");
if (mysql_num_rows($result) == 0) {
$result = mysql_query ("INSERT INTO customer_login (username, password, email, last_name, first_name) VALUES
('".$customer."', PASSWORD('".$pass."'), '".$email."', '".$last."', '".$first."')");
if ($result) {
$logged_in_customer = $customer;
session_register("logged_in_customer");
echo "The customer, ".$customer.", has been added.<BR><BR>";
echo "<A HREF='main.php'>Click here to proceed to the main page.</A><BR><BR>";
echo "You Will Be Redirected In 2 Seconds.<br>";
echo "<META HTTP-EQUIV='refresh' content='2;URL=http://www.homtek.net/admin/main.php'>";
echo "<A HREF='logout.php'>Click here to log out.</A>";
exit;
} else {
echo "<font color=red>Sorry, an error has occured, please contact technical support.</font>";
exit;
}
} else {
echo "<font color=red>Sorry, that username has been taken. Please try another.</font><BR>";
}
} else if ($customer || $pass) {
echo "<font color=red>Please fill in both fields.</font>";
}
?>
<FORM METHOD=POST ACTION="register_customer.php">
First Name:<br>
<INPUT NAME="first" TYPE=TEXT MAXLENGTH=10 SIZE=10><br><br>
Last Name:<br>
<INPUT NAME="last" TYPE=TEXT MAXLENGTH=10 SIZE=10><br><br>
Email Address:<br>
<INPUT NAME="email" TYPE=TEXT MAXLENGTH= SIZE=10><br><br>
Username:<br>
<INPUT NAME="customer" TYPE=TEXT MAXLENGTH=30 SIZE=10><BR><br>
Password:<br>
<INPUT NAME="pass" TYPE=TEXT MAXLENGTH=10 SIZE=10><br><br>
<BR>
<INPUT TYPE=SUBMIT VALUE="Register">
</FORM>
My usual boring signature: Something
-
Oct 9th, 2006, 10:29 PM
#4
Re: Set user_id on login
to eliminate whitespace in a query, you should break it up using PHP, and not actually query something with a line break in it.
if you want to insert the same thing into two tables, i'm not sure if you can just do it both at the same time, because if the tables didn't match you'd get errors.. you can just do two separate queries though, to two different tables.
Also, just a tip.. instead of defining your UPDATE and INSERTs as a variable to check if they were successful, it's sometimes easier to add the die() function onto it..
ie: mysql_query("blahblah") or die(mysql_error());
This will end the script at that point and print your MySQL error, so it will give you an idea of what went wrong. Or, you can always just add your text into the "die" instead of calling mysql_error.. (ie: or die("sorry, an error occurred") . doing this, you could also append the mysql_error() function using a period, "or die('sorry, error occurred<br>' . mysql_error());", or other things if you please!
anyway, i changed some of your stuff around and added the second insert thing..
PHP Code:
<?PHP
if ($customer && $pass) {
$dbh=mysql_connect ("localhost", "homtek_dclamp", "password") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("homtek_homtek");
$result = mysql_query ("SELECT * FROM customer_login WHERE username = '".$customer."'");
if (mysql_num_rows($result) == 0) {
mysql_query("INSERT INTO customer_login (username, password, email, last_name, first_name) VALUES " .
"('".$customer."', PASSWORD('".$pass."'), '".$email."', '".$last."', '".$first."')") or die(mysql_error());
mysql_query("INSERT INTO othertable (field1, field2, field3) VALUES('value1', 'value2', 'value3')") or die(mysql_error());
$logged_in_customer = $customer;
session_register("logged_in_customer");
echo "The customer, ".$customer.", has been added.<BR><BR>";
echo "<A HREF='main.php'>Click here to proceed to the main page.</A><BR><BR>";
echo "You Will Be Redirected In 2 Seconds.<br>";
echo "<META HTTP-EQUIV='refresh' content='2;URL=http://www.homtek.net/admin/main.php'>";
echo "<A HREF='logout.php'>Click here to log out.</A>";
exit;
} else {
echo "<font color=red>Sorry, that username has been taken. Please try another.</font><BR>";
}
} else if ($customer || $pass) {
echo "<font color=red>Please fill in both fields.</font>";
}
?>
-
Oct 9th, 2006, 10:46 PM
#5
Thread Starter
WiggleWiggle
Re: Set user_id on login
ok thanks. i am getting an error. http://www.homtek.net/admin/register_customer.php
you may need to login, username&password = test
My usual boring signature: Something
-
Oct 9th, 2006, 11:20 PM
#6
Re: Set user_id on login
post the exact code you used (aside from the mysql username/password), because submitting that doesn't tell me much in the terms of detail without having something to go by.
-
Oct 10th, 2006, 08:48 PM
#7
Thread Starter
WiggleWiggle
Re: Set user_id on login
PHP Code:
<?PHP
if ($customer && $pass) {
$dbh=mysql_connect ("localhost", "homtek_dclamp", "moiola") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("homtek_homtek");
$result = mysql_query ("SELECT * FROM customer_login WHERE username = '".$customer."'");
if (mysql_num_rows($result) == 0) {
mysql_query("INSERT INTO customer_login (username, password) VALUES('".$customer."', PASSWORD('".$pass."')')") or die(mysql_error());
mysql_query("INSERT INTO customer (username, email, first_name, last_name) VALUES('".$username."', '".$email."', '".$first_name."', '".$last_name."')") or die(mysql_error());
$logged_in_customer = $customer;
session_register("logged_in_customer");
echo "The customer, ".$customer.", has been added.<BR><BR>";
echo "<A HREF='main.php'>Click here to proceed to the main page.</A><BR><BR>";
echo "You Will Be Redirected In 2 Seconds.<br>";
echo "<META HTTP-EQUIV='refresh' content='2;URL=http://www.homtek.net/admin/main.php'>";
echo "<A HREF='logout.php'>Click here to log out.</A>";
exit;
} else {
echo "<font color=red>Sorry, that username has been taken. Please try another.</font><BR>";
}
} else if ($customer || $pass) {
echo "<font color=red>Please fill in both fields.</font>";
}
?>
<FORM METHOD=POST ACTION="register_customer.php">
First Name:<br>
<INPUT NAME="first" TYPE=TEXT MAXLENGTH=10 SIZE=10><br><br>
Last Name:<br>
<INPUT NAME="last" TYPE=TEXT MAXLENGTH=10 SIZE=10><br><br>
Email Address:<br>
<INPUT NAME="email" TYPE=TEXT MAXLENGTH= SIZE=10><br><br>
Username:<br>
<INPUT NAME="customer" TYPE=TEXT MAXLENGTH=30 SIZE=10><BR><br>
Password:<br>
<INPUT NAME="pass" TYPE=TEXT MAXLENGTH=10 SIZE=10><br><br>
<BR>
<INPUT TYPE=SUBMIT VALUE="Register">
</FORM>
My usual boring signature: Something
-
Oct 10th, 2006, 09:28 PM
#8
Re: Set user_id on login
you must change this line:
Code:
mysql_query("INSERT INTO customer_login (username, password) VALUES('".$customer."', PASSWORD('".$pass."')')") or die(mysql_error());
to this:
Code:
mysql_query("INSERT INTO customer_login (username, password) VALUES('".$customer."', PASSWORD('".$pass."'))") or die(mysql_error());
you had an extra quote that was not opened beforehand.
-
Oct 10th, 2006, 09:34 PM
#9
Thread Starter
WiggleWiggle
My usual boring signature: Something
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|