Page 1 of 2
Defining A Varible
Posted: Thu Dec 12, 2002 6:10 pm
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.
Posted: Thu Dec 12, 2002 11:30 pm
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?
Posted: Fri Dec 13, 2002 8:04 am
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.
Posted: Fri Dec 13, 2002 8:32 am
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.
Posted: Fri Dec 13, 2002 9:02 am
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
Posted: Fri Dec 13, 2002 9:39 am
by volka
I thought so but wasn't sure how to ask

Everything that shall be returned is selected right after the SELECT in a query.
will include only the values of the
fieldA-column
Code: Select all
SELECT fieldA, fieldB, fieldC FROM xyz
-->
fieldA,
fieldB and
fieldCall columns will be in the resultset.
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
Posted: Fri Dec 13, 2002 2:04 pm
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";
Posted: Fri Dec 13, 2002 2:36 pm
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.
Posted: Fri Dec 13, 2002 2:43 pm
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);
Posted: Fri Dec 13, 2002 2:50 pm
by nathus
Posted: Fri Dec 13, 2002 2:55 pm
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.
Posted: Fri Dec 13, 2002 3:01 pm
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");
Posted: Fri Dec 13, 2002 3:44 pm
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;
}
?>
Posted: Fri Dec 13, 2002 3:51 pm
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.
Posted: Fri Dec 13, 2002 4:12 pm
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