Jump to content


Photo

Noobie Stuff: Very Basics Explained Easily


  • Please log in to reply
117 replies to this topic

#41 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 04 August 2006 - 03:04 PM

and I apologize for being rude, stuck on a dumb ass under-priced huge project, and I am a little pissed.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#42 Pudgemeister

Pudgemeister
  • Members
  • PipPipPip
  • Advanced Member
  • 94 posts
  • LocationCornwall, England, UK,

Posted 04 August 2006 - 03:11 PM

nah im sory as wel-i startin college in a month and am lookin foreward to it so much i sorta tryin to get head start.

i am learning php for a reason though-i have planned-my own big project that wil take a while-ts why im learning php in the first place.

do u understand that code in ur comment?

i remember it from when i couldnt even do if else codes hehe-think thats why he posted-to explain.

if u dont i wil explain but i am guessing u do hehehe.

ok i got my own prob now but i think i can sort it (i think)

even when i put in the right username and password, now it still says somethin sis wrong :S

oh well itl b ok.

cheers
Pudgemeister

ps sorry again
[div align="center"][a href="http://imageshack.us" target="_blank"][/a]
[/div]

#43 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 04 August 2006 - 03:16 PM

yes at this point, I understand almost everything I see with php/mysql, because of the hardships I faced in recent projects, I feel as thought I can do almost anything with php.  My current difficulty is pagination but I am not doing that right now, I will create a new system myself later on down the road when I have time.  The reason I learnt so fast, is I started creating everything I did from scratch.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#44 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 04 August 2006 - 03:18 PM

what is the error, line, or error message.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#45 Pudgemeister

Pudgemeister
  • Members
  • PipPipPip
  • Advanced Member
  • 94 posts
  • LocationCornwall, England, UK,

Posted 04 August 2006 - 03:29 PM

its not an error-its the fact i been trying to get something but doing the wrong thing to get it.

like i said erlier-in the table "users" in the database there is a field called "id" which is auto_increment.

well i expected the

$query_2="SELECT id FROM users WHERE username = '$username' && password = '$password'";
		$result_2=mysql_query($query_2) or die(mysql_error());
		$id=mysql_num_rows($result_2);

to get the id for that user that has successfully logged in correctly.

of course that is totally wrong as all the $id variable is going to be is the number of rows that have the username and password to be what the user typed in (which since its a log in script, will always be 1 as no 2 usernames can be the same.

i am trying to grab the id from the database that corresponds to the username and password and have it assigned to the variable "$id".

corresponds? big word for me hehehe :S

lol

Pudgemeister
[div align="center"][a href="http://imageshack.us" target="_blank"][/a]
[/div]

#46 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 04 August 2006 - 03:36 PM

when you are allowing them to sign up simple have
<?php
$select = "SELECT username FROM userinfo WHERE username = '$username';";
$query = mysql_query($select);
if (mysql_fetch_array($query)) {
echo "I apologize but the username is already taken";
}
?>
that's it,
as for pulling the info where user name and password is tested

after they click submit to login it's
<?php
$select = "SELECT username, password FROM userinfo WHERE username = '$username' AND password = '$password';";
$query = mysql_query($select);
if ($row = mysql_fetch_array($query)) {
echo "You have logged in successfully";
}
?>

these examples won't work but it'll give you an idea on wher eto start.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#47 Pudgemeister

Pudgemeister
  • Members
  • PipPipPip
  • Advanced Member
  • 94 posts
  • LocationCornwall, England, UK,

Posted 04 August 2006 - 03:50 PM

oki could understand all of that but me being me i still made a bolocks up of my code.

if ($num===0) {
    echo 'Your Password And/Or Username Are Not Correct-Please Try Again <a href="index.php">Here</a>';
    } else {
		$query_2="SELECT id FROM users WHERE username = '$username' && password = '$password'";
		$result_2=mysql_query($query_2) or die(mysql_error());
		$id=mysql_fetch_array($result_2); // returns the data found-hopefully.
		print $id;

all i need now is:

to assign the id number found in the table in the database to the variable $id and then print it.

but no...me being me-i cant do it.

Pudgemeister
[div align="center"][a href="http://imageshack.us" target="_blank"][/a]
[/div]

#48 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 04 August 2006 - 03:57 PM

<?php
if ($num===0) {
    echo 'Your Password And/Or Username Are Not Correct-Please Try Again <a href="index.php">Here</a>';
    } else {
		$query_2="SELECT id FROM users WHERE username = '$username' && password = '$password'";
		$result_2=mysql_query($query_2) or die(mysql_error());
		$id=mysql_fetch_array($result_2); // returns the data found-hopefully.
		print $id;
?>
ok if you don't clean this up like I said you'll regret it later your echo.
And your trying to access your id wrongly.  Here is the thing you don't understand right now
mysql_fetch_array()
is meant to capture an ARRAY into the variable, meaning you have to understand arrays to do this, that's whY i said earlier basic's first, you can't jump straight to mysql queries without knowing other stuff, like arrays and general variables.

THis is what your code should look like
print is fine, but I find echo better, if you use print 1 itme in your script use all print, if you use echo, then use all echo it's neater.

here is your code again, with what it should look like if you want it to do what you want.
and I cleaned it up a bit.

<?php
if ($num===0) {
    echo "Your Password And/Or Username Are Not Correct-Please Try Again <a href=\"index.php\">Here</a>";
}else {
$query_2 = "SELECT id FROM users WHERE username = '$username' AND password = '$password';";
$result_2 = mysql_query($query_2)
$id = mysql_fetch_array($result_2); // returns the data found-hopefully.
echo $row['id'];
?>
as you can see a few thigns you were doing are bad habits.  Now the $row calls the array if you had of use
$shithead = mysql_fetch_array($result_2);
then you access it
$shithead['id'];
same thing
you access the array, followed by the value
$row['id'];
if you had other stuff you just replace the variable with the field name in your database
$row['variable']:

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#49 nethnet

nethnet
  • Members
  • PipPipPip
  • Advanced Member
  • 284 posts
  • LocationPhiladelphia, PA, USA

Posted 04 August 2006 - 04:07 PM

You also have to realize that MySQL syntax is different than PHP syntax.  PHP uses comparison operators (such as == and &&), MySQL doesn't.  You had these PHP operators in your MySQL query which was causing your errors.  I know you want to learn everything as simple as posible, and avoid all of the technical jargon and whatnot, but you're going to kick yourself later when you HAVE to go back and relearn everything as you should have learnt it the first way.  Believe me, it's not that hard if you just take the time to work with it.  Like in the previous post, you are going to have to understand both MySQL and arrays before doing yoru database queries.  You can't just echo your array without accessing an individual element.  If what I'm saying confuses you, do a quick google search for PHP arrays and read some articles.  This is very basic stuff and what you are trying to accomplish cannot be done without understanding it.

Good luck.
"Creativity is allowing yourself to make mistakes. Art is knowing which ones to keep."  --Scott Adams

Current project: nethnet.com

#50 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 04 August 2006 - 04:10 PM

yes when I rewrote the code I took that out, you can use some comparison operators but on variables, like less than greater than sometimes using sql operators are more helpful than pulling them into php and doing it, but for statements you HAve to use

AND
OR


------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#51 nethnet

nethnet
  • Members
  • PipPipPip
  • Advanced Member
  • 284 posts
  • LocationPhiladelphia, PA, USA

Posted 04 August 2006 - 04:12 PM

Right, I didn't mean to say that MySQL doesn't have ANY comparison operators, just not the two which were in the script.  AND and OR must be used.
"Creativity is allowing yourself to make mistakes. Art is knowing which ones to keep."  --Scott Adams

Current project: nethnet.com

#52 Pudgemeister

Pudgemeister
  • Members
  • PipPipPip
  • Advanced Member
  • 94 posts
  • LocationCornwall, England, UK,

Posted 04 August 2006 - 04:20 PM

ok-thank you.

il try to understand the technical stuff as well in that case.

thanx for info people
[div align="center"][a href="http://imageshack.us" target="_blank"][/a]
[/div]

#53 Pudgemeister

Pudgemeister
  • Members
  • PipPipPip
  • Advanced Member
  • 94 posts
  • LocationCornwall, England, UK,

Posted 04 August 2006 - 04:31 PM

ok.

if ne1 is getting annoyed with me getting this wrong-please give up.

but i have done what you have said and now it just shows a blank page.

the code is now this and i am confident it is right according to what u said.

<?php

include ("dbinfo.inc.php");

$username=$_POST['username'];
$password=$_POST['password'];

$query_1="SELECT * FROM users WHERE username='$username' AND password='$password'";
$result_1=mysql_query($query_1) or die(mysql_error());
$num=mysql_num_rows($result_1); // returns numbers of matches found.

if ($num===0) {
    echo 'Your Password And/Or Username Are Not Correct-Please Try Again <a href="index.php">Here</a>';
    } else {
		$query_2="SELECT id FROM users WHERE username = '$username' AND password = '$password'";
		$result_2=mysql_query($query_2) or die(mysql_error());
		$id=mysql_fetch_array($result_2); // returns the data found-hopefully.
		echo $row['id'];
};
?>

blank page-no id from id field.

i know i know its my fault.

i am still working on it and reading your posts after i send this
[div align="center"][a href="http://imageshack.us" target="_blank"][/a]
[/div]

#54 nethnet

nethnet
  • Members
  • PipPipPip
  • Advanced Member
  • 284 posts
  • LocationPhiladelphia, PA, USA

Posted 04 August 2006 - 04:49 PM

That should be $id['id'] instead of $row['id'];  There is no $row array in your script.
"Creativity is allowing yourself to make mistakes. Art is knowing which ones to keep."  --Scott Adams

Current project: nethnet.com

#55 Pudgemeister

Pudgemeister
  • Members
  • PipPipPip
  • Advanced Member
  • 94 posts
  • LocationCornwall, England, UK,

Posted 04 August 2006 - 04:52 PM

lol i thought that but didnt change in case
[div align="center"][a href="http://imageshack.us" target="_blank"][/a]
[/div]

#56 nethnet

nethnet
  • Members
  • PipPipPip
  • Advanced Member
  • 284 posts
  • LocationPhiladelphia, PA, USA

Posted 04 August 2006 - 05:04 PM

try echoing $query2 inside your else statement so we can see exactly what is being queried to the database, that way we can also see if the else statement is being executed.
"Creativity is allowing yourself to make mistakes. Art is knowing which ones to keep."  --Scott Adams

Current project: nethnet.com

#57 Pudgemeister

Pudgemeister
  • Members
  • PipPipPip
  • Advanced Member
  • 94 posts
  • LocationCornwall, England, UK,

Posted 04 August 2006 - 05:11 PM

all working fine!!!!!

thank u all so much-uve taught me alot today.

thanx for all your help-if you come across any info that peeps might wana know please post it in here still so everyone can find it.

cheers all

Pudgemeister
[div align="center"][a href="http://imageshack.us" target="_blank"][/a]
[/div]

#58 Pudgemeister

Pudgemeister
  • Members
  • PipPipPip
  • Advanced Member
  • 94 posts
  • LocationCornwall, England, UK,

Posted 05 August 2006 - 09:40 AM

Hi All Again.

im having a prob with the sessions now.

im confused about it all.

i know i have to have session_start() at the top of every page right under <?php and not have any echos or prints near it.

but its the variable set and everything i dont understand.

this is the two pages where the sessions are involved:

login.php:

<?php

include ("dbinfo.inc.php");

$username=$_POST['username'];
$password=$_POST['password'];

$query_1="SELECT * FROM users WHERE username='$username' AND password='$password'";
$result_1=mysql_query($query_1) or die(mysql_error());
$num=mysql_num_rows($result_1); // returns numbers of matches found.

if ($num===0) {
    echo "Your Password And/Or Username Are Not Correct-Please Try Again <a href=\"index.php\">Here</a>";
}else {
$query_2 = "SELECT id FROM users WHERE username = '$username' AND password = '$password';";
$result_2 = mysql_query($query_2);
$id = mysql_fetch_array($result_2); // returns the data found-hopefully.
$_SESSION[$id['id']] = '$username';
echo 'You Are Now Logged In, Click <a href="logged_in.php">Here</a> to Gain Access To Your Account';
};
?>


logged_in.php:

<?php
session_start();
$_SESSION[$id['id']] = '$username';
echo 'You Are Now Logged In';
?>

i think ive got things muddles up.


basically-login.php handles the login data inputted by the user to find the data in the database and to make sure they have registered and logged_in.php is my test page which should only be viewable by logged in users.

as it is at the moment-logged_in.php is viewable to everyone.

cheers for reading.

Pudgemeister
[div align="center"][a href="http://imageshack.us" target="_blank"][/a]
[/div]

#59 Pudgemeister

Pudgemeister
  • Members
  • PipPipPip
  • Advanced Member
  • 94 posts
  • LocationCornwall, England, UK,

Posted 05 August 2006 - 10:11 AM

cmon people i reli need help
[div align="center"][a href="http://imageshack.us" target="_blank"][/a]
[/div]

#60 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 05 August 2006 - 10:34 AM

I dont quite understand what you are trying to do with the sessions, so im going to modify your code a little but. Ill comment it.

login.php
<?php
session_start();
include ("dbinfo.inc.php");

$username=$_POST['username'];
$password=$_POST['password'];

$query_1="SELECT * FROM users WHERE username='$username' AND password='$password'";
$result_1=mysql_query($query_1) or die(mysql_error());
$num=mysql_num_rows($result_1); // returns numbers of matches found.

if ($num===0) {
    echo "Your Password And/Or Username Are Not Correct-Please Try Again <a href=\"index.php\">Here</a>";
}else {
$query_2 = "SELECT id FROM users WHERE username = '$username' AND password = '$password';";
$result_2 = mysql_query($query_2);
$num = mysql_num_rows($result_2);//the number of rows the search returns
if($num > 1){//This is to check the user exists. If there are less than 1 rows, the query found no matches
echo 'Incorrect login information';
exit;
}
$row = mysql_fetch_array($result_2); // returns the data found-hopefully. Renamed to $row to avoid confusion
$_SESSION['username'] = $row['username'];//this prevents and issues of capitilisation. MySQL is case insenstive whereas php is not. This will put the username from the database into the session.
echo 'You Are Now Logged In, Click <a href="logged_in.php">Here</a> to Gain Access To Your Account';
};
?>

logged_in.php:
<?php
session_start();
if(empty($_SESSION['username'])){//if there is nothing in the session
echo 'you are not logged in';
exit;//quit the page so they cant view anything else
}else{
echo 'You Are Now Logged In';
}
?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users