Defining A Varible

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

kkurkowski
Forum Commoner
Posts: 53
Joined: Mon Dec 09, 2002 4:44 pm
Location: Michigan

Defining A Varible

Post by kkurkowski »

Code: Select all

<?php
$sql = "SELECT * FROM $table_name
	WHERE email = "$email" AND username = "$username" AND password = password("$password")
	"; 
?>
Can I have this kind of command in that format. I have a sign up script and then it ask for your email. When I add email in this script it doesn't let me login. That is not the whole script though. But when I login into the site then it says invalid login. When I get rid of email=\"$email\" then it works. I can't figure out how to take the information from email to define it to $email and then put it in the post so they are in 2 different tables in mysql so people can view there in email in post that they post. It also adds it into the cookie to keep it defined but I can't get it to define it before the cookie.

If this seems confusing ask me the questions you need to.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Don't know where to start asking, so I try this ;) :
WHERE email = \"$email\" AND username = \"$username\" AND password = password(\"$password\")
users have to type their email-adress, loing and password to enter your site?
kkurkowski
Forum Commoner
Posts: 53
Joined: Mon Dec 09, 2002 4:44 pm
Location: Michigan

Post by kkurkowski »

No, they do not type it in when they login. I was trying to get it so that when they are logged in then it loads there email into a varible so then when the news thing says posted by $user then you can click it so when you click $user then it will email them. I know hwo to do that it just isn't opening the email up into a varible.
User avatar
nathus
Forum Commoner
Posts: 49
Joined: Thu Dec 12, 2002 6:23 pm

Post by nathus »

the reason thats not working with the email=\"$emai\" is because if they don't enter their email, its the same as looking for

Code: Select all

<?php 
$sql = "SELECT * FROM $table_name 
   WHERE email = "" AND username = "$username" AND password = password("$password") 
   ";  
?>
which will find all results where username=$username AND password=password("$password") AND email="" (no email set). Also, you said that the email is in a second table? if thats the case, you'd need to add that table to the select query, but maybe I just misunderstood you.
kkurkowski
Forum Commoner
Posts: 53
Joined: Mon Dec 09, 2002 4:44 pm
Location: Michigan

Post by kkurkowski »

ok, I understand that now. ok, I have 2 tables in MySQL. news_news and news_users. news_isers has the email stored in it. I want to open it and make it defined so when they post a article then it shows there email in the post. But it doesn';t have anything to add to the email tables in news_news
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

I thought so but wasn't sure how to ask :D
Everything that shall be returned is selected right after the SELECT in a query.

Code: Select all

SELECT fieldA FROM xyz
will include only the values of the fieldA-column

Code: Select all

SELECT fieldA, fieldB, fieldC FROM xyz
-->fieldA, fieldB and fieldC

Code: Select all

SELECT * FROM xyz
all columns will be in the resultset.

Code: Select all

SELECT fieldA FROM xyz WHERE id=1
will include all records that have the value 1 in the field id and will return only the column fieldA. So there WHERE-clause is only to select certain records, not certain columns ;)
Since these are the very basics of SQL I suggest you read one or two tutorials for beginners before going on - it'll help

e.g. http://www.w3schools.com/sql/default.asp but here are many more
kkurkowski
Forum Commoner
Posts: 53
Joined: Mon Dec 09, 2002 4:44 pm
Location: Michigan

Post by kkurkowski »

Code: Select all

<?php
if ((!$username) || (!$password)) {
	header("Location: invalid_login.php");
	exit;
} 

$db_name = "kkurkowski";
$table_name = "news_users";

$connection = @mysql_connect("localhost", "username", "password") 
	or die("Couldn't connect.");

$db = mysql_select_db($db_name, $connection)
	or die("Couldn't select database.");

$sql = "SELECT * FROM $table_name
	WHERE username = "$username" AND password = password("$password")
	";

$email = "SELECT email FROM $table_name";

$result = mysql_query($sql) 
        or die ("Can't execute query."); 

$num = mysql_numrows($result); 

if ($num != 0) { 

	$cookie_name_auth = "status";
	$cookie_value_auth = "ok";
	$cookie_expire_auth = 0;
	$cookie_domain_auth = ".bve.32k.org";
	
	setcookie($cookie_name_auth, $cookie_value_auth, $cookie_expire_auth, "/" , $cookie_domain_auth, 0);

	$cookie_name_user = "user";
	$cookie_value_user = "$username";
	$cookie_name_email = "email";
	$cookie_value_email = "$email";
	$cookie_expire_user = 0;
	$cookie_domain_user = ".bve.32k.org";
	
	setcookie($cookie_name_user, $cookie_value_user, $cookie_name_email, $cookie_value_email,  $cookie_expire_user, "/" , $cookie_domain_user, 0);

	header("Location: news.php");

} else { 

	header("Location: invalid_login.php");
	exit;
} 
?>
I put this in here to select the email that is in there and now I did try to put it in $results but that did not work. Gave me errors. I want to make it load email to the cookie so it will work right with my script.

Code: Select all

$email = "SELECT email FROM $table_name";
User avatar
nathus
Forum Commoner
Posts: 49
Joined: Thu Dec 12, 2002 6:23 pm

Post by nathus »

ok, where you have the

Code: Select all

$sql = "SELECT * FROM $table_name 
   WHERE username = "$username" AND password = password("$password")";
what you are doing is getting all fields from the table $table_name that match the supplied username, and password. if you want to get the email of that user you would do something like

Code: Select all

$result = mysql_query($sql);
$result = mysql_fetch_array($result);
$email = $result['email'];
you are already getting the users email when you run the query. you just need to get it out of the result set the query returns.
kkurkowski
Forum Commoner
Posts: 53
Joined: Mon Dec 09, 2002 4:44 pm
Location: Michigan

Post by kkurkowski »

Now this is being a pain. When I add $cookie_name_email and $cookie_value_email into the setcookie then I get an error but when I take it out it it all works but I want to put the email into the cookie so it can be placed into the post.
Warning: Wrong parameter count for setcookie() in login.php on line 45

Code: Select all

$cookie_name_user = "user";
$cookie_value_user = "$username";
$cookie_name_email = "email";
$cookie_value_email = "$email";
$cookie_expire_user = time()+86400;
$cookie_domain_user = ".bve.32k.org";
	
	setcookie($cookie_name_user, $cookie_value_user, $cookie_name_email, $cookie_value_email,  $cookie_expire_user, "/" , $cookie_domain_user, 0);
User avatar
nathus
Forum Commoner
Posts: 49
Joined: Thu Dec 12, 2002 6:23 pm

Post by nathus »

kkurkowski
Forum Commoner
Posts: 53
Joined: Mon Dec 09, 2002 4:44 pm
Location: Michigan

Post by kkurkowski »

That site just tells you the normal setcookie like I had when I get rid of the email cookie. It doesn't tell you nothing about setting more values and names to it.
User avatar
nathus
Forum Commoner
Posts: 49
Joined: Thu Dec 12, 2002 6:23 pm

Post by nathus »

for each value you want to store in the cookie you need to have a setcookie() call.

for example, if you want to set name, login, and email in a cookie, you'd do it something like this.

setcookie("name", "$name");
setcookie("login", "$login");
setcooke("email", "$email");
kkurkowski
Forum Commoner
Posts: 53
Joined: Mon Dec 09, 2002 4:44 pm
Location: Michigan

Post by kkurkowski »

Ok, I understand why the cookie wasn't working now, but I am still trying to get email to define it self so it will add it in the table in news_news.
Warning: Supplied argument is not a valid MySQL result resource in login.php on line 26
The $result = mysql_fetch_array($result); is killing $num = mysql_numrows($result) from working.

Code: Select all

<?php
if ((!$username) || (!$password)) {
	header("Location: invalid_login.php");
	exit;
} 

$db_name = "kkurkowski";
$table_name = "news_users";

$connection = @mysql_connect("localhost", "user", "pass") 
	or die("Couldn't connect.");

$db = mysql_select_db($db_name, $connection)
	or die("Couldn't select database.");

$sql = "SELECT * FROM $table_name
	WHERE username = "$username" AND password = password("$password")
	";

$result = mysql_query($sql)
	or die ("Can't execute query.");
$result = mysql_fetch_array($result);
$email = $result["email"];

$num = mysql_numrows($result)

if ($num != 0) { 

	$cookie_name_auth = "status";
	$cookie_value_auth = "ok";
	$cookie_expire_auth = 0;
	$cookie_domain_auth = ".bve.32k.org";
	
	setcookie($cookie_name_auth, $cookie_value_auth, $cookie_expire_auth, "/" , $cookie_domain_auth, 0);

	$cookie_name_user = "user";
	$cookie_value_user = "$username";
	$cookie_expire_user = time()+86400;
	$cookie_domain_user = ".bve.32k.org";
	
	setcookie($cookie_name_user, $cookie_value_user, $cookie_expire_user, "/" , $cookie_domain_user, 0);

	$cookie_name_email = "email";
	$cookie_value_email = "$email";
	$cookie_expire_email = time()+86400;
	$cookie_domain_email = ".bve.32k.org";

	setcookie($cookie_name_email, $cookie_value_email, $cookie_expire_email, "/" , $cookie_domain_email, 0);

	header("Location: news.php");

} else { 

	header("Location: invalid_login.php");
	exit;
} 
?>
User avatar
BigE
Site Admin
Posts: 139
Joined: Fri Apr 19, 2002 9:49 am
Location: Missouri, USA
Contact:

Post by BigE »

First of all, always use mysql_num_rows() because mysql_numrows() is depriciated. Second, you cannot use mysql_num_rows() after you use mysql_fetch_*() you must do it before that. So in turn, try this

Code: Select all

<?php
$result = mysql_query($sql)
   or die ("Can't execute query.");
$num = mysql_numrows($result) 

$result = mysql_fetch_array($result);
$email = $result["email"];
?>
Hope that helps.
kkurkowski
Forum Commoner
Posts: 53
Joined: Mon Dec 09, 2002 4:44 pm
Location: Michigan

Post by kkurkowski »

Now, when I ,put that in it works, but now it still doesn't want to add email into the varible stil because it will not add email to the table in news_news
Post Reply