hosted by liquidweb


Go Back   Web Hosting Talk : Web Hosting Main Forums : Programming Discussion : PHP & MySQL
Reply

Programming Discussion Discussions related to web programming languages and other related issues. Topics may include configuration, optimization, practical usage and database connectivity.
Forum Jump

PHP & MySQL

Reply Post New Thread In Programming Discussion Subscription
 
Send news tip View All Posts Thread Tools Search this Thread Display Modes
  #1  
Old 07-01-2005, 09:27 AM
UrlGuy UrlGuy is offline
Aspiring Evangelist
 
Join Date: Feb 2005
Location: Norway
Posts: 391

PHP & MySQL


Hi,

I have just started MySQL and now know how to insert and retrieve data. This is just a register script for the users to register, and their user+pass gets put in my database. So now I plan to do this member script. I have added users to the database using using a HTML form posting to a file named reg.php which code is as following:
PHP Code:

    $user 
$_POST['user'];
    
$pass $_POST['pass'];

    include(
"config.php");
    
mysql_connect($hostname,$username,$password);
    @
mysql_select_db($database) or die("Unable to select that database");
    
$query "INSERT INTO users VALUES ('','$user','$pass')";
    
$result=mysql_query($query);
    
mysql_close(); 
This works ok for entering the data: username, password and a uniqe ID into the 'users' table. But now I have to do the login form, I want it to look in the 'users' table to see if it finds their username+password. Can anyone help me with how I can do this? Thanks Alot.. have a good summer everyone!

Reply With Quote


Sponsored Links
  #2  
Old 07-01-2005, 10:20 AM
p.davies p.davies is offline
Newbie
 
Join Date: Dec 2004
Location: London
Posts: 5
UrlGuy,

A quick and simple example may be as follows:

PHP Code:
$user $_POST['user'];  
$pass $_POST['pass'];    
 
include(
"config.php");   
mysql_connect($hostname,$username,$password); 
@
mysql_select_db($database) or die("Unable to select that database");   
$query "SELECT * FROM users WHERE user = $user AND pass = $pass";   
$result $db->query($sql);    

if(
$result->numRows() < 1)  
{      
/* Access is denied */  
}  
else  
{      
/* Logged in */  
}    
mysql_close(); 
Naturally this is not fully secure but it’s a starting point.


Last edited by p.davies; 07-01-2005 at 10:24 AM.
Reply With Quote
  #3  
Old 07-01-2005, 11:59 AM
UrlGuy UrlGuy is offline
Aspiring Evangelist
 
Join Date: Feb 2005
Location: Norway
Posts: 391
Thanks for reply.

When I use the above code with my script I get this file:

PHP Code:

<?PHP
    
include("config.php");
    @
session_start();
    if (empty(
$user) || empty($pass)) {
        
$logged_in false;
        
$_SESSION['logged_in'] = false;
        echo 
"<B>You forgot to enter your username or password</B><BR>";
        include(
"login.php");
        exit;
    }



$user $_POST['user'];   
$pass $_POST['pass'];     
    
mysql_connect($hostname,$username,$password); 
@
mysql_select_db($database) or die("Unable to select that database");    
$query "SELECT * FROM users WHERE user = $user AND pass = $pass";    
$result $db->query($sql);     

if(
$result->numRows() < 1)   
{       
echo 
"Wrong username or password<BR>";
include(
"login.php");
exit;  
}   
else   
{       
$_SESSION['logged_in'] = true;
header('Location: members.php');  
}     
mysql_close();


?>

But then I get this error:
Quote:
Fatal error: Call to a member function on a non-object in /home/user/public_html/log_in.php on line 20

Here is line 20:
Quote:
$result = $db->query($sql);


By the way.. when I setup the 'users' table I used this code:
Quote:
$query="CREATE TABLE users (id int(6) NOT NULL auto_increment,first varchar(15) NOT NULL,last varchar(15) NOT NULL,phone varchar(20) NOT NULL,mobile varchar(20) NOT NULL,fax varchar(20) NOT NULL,email varchar(30) NOT NULL,web varchar(30) NOT NULL,PRIMARY KEY (id),UNIQUE id (id),KEY id_2 (id))";
I know this creates a little too many fields as I only need 3 but I copied&pasted this as I'm not sure how to edit it right.. do I have to name one field 'usernames' and another 'passwords' to use the "SELECT * FROM users WHERE user = $user AND pass = $pass" ? How do I do this? Or is all I needed to do is setup the 'users' table and it autoassigns the data?


Hope anyone can help me with these 2 problems... thanks!!


Last edited by UrlGuy; 07-01-2005 at 12:04 PM.
Reply With Quote
Sponsored Links
  #4  
Old 07-01-2005, 12:19 PM
Vulture Vulture is offline
Junior Guru Wannabe
 
Join Date: Jul 2004
Location: Scotland, UK
Posts: 81
For line 20 change
PHP Code:
$result $db->query($sql); 
to
PHP Code:
$result mysql_query($query); 
I'm not sure what you mean regarding your second problem, can you explain a bit more?

__________________
Scott - My Spiel


Reply With Quote
  #5  
Old 07-01-2005, 12:24 PM
UrlGuy UrlGuy is offline
Aspiring Evangelist
 
Join Date: Feb 2005
Location: Norway
Posts: 391
thanks.. think it worked, get a blank white page now and no error atleast..

I meant.. since its something like "SELECT * FROM users WHERE user = $user AND pass = $pass

with the WHERE user = $user
is 'user' a seperate column or row or something? How can I set up that? I have only setup the table 'users', no rows or any more.. Hope I wrote better now.. if not tell me and I'll try explain more. Thanks!!

Reply With Quote
  #6  
Old 07-01-2005, 12:36 PM
Vulture Vulture is offline
Junior Guru Wannabe
 
Join Date: Jul 2004
Location: Scotland, UK
Posts: 81
The SELECT query will go to the table named "users" (which your above CREATE query will have created). It will then look in the field "user" (which you have not yet created) for the value which was input, if there is a matching "pass" in the pass field it will return the entire row.

Since you don't have a table fully setup, try this sql code. It will drop your existing table, create a new one named "users", it has 3 fields - id (which is auto-inc), user (the username) and pass (the password):
Code:
DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
`id` INT( 5 ) NOT NULL AUTO_INCREMENT ,
`user` VARCHAR( 30 ) NOT NULL ,
`pass` VARCHAR( 40 ) NOT NULL ,
PRIMARY KEY ( `id` )
);
The pass field is probably a bit longer than you'll need...I've done this to allow you to encrypt the passes later...which is something you'll want to do.

Hope that helps, give me a shout if you need more explination

__________________
Scott - My Spiel


Reply With Quote
  #7  
Old 07-01-2005, 12:46 PM
UrlGuy UrlGuy is offline
Aspiring Evangelist
 
Join Date: Feb 2005
Location: Norway
Posts: 391
Thanks, seems to be what I'm looking for!


But now my setup.php file fails with that code :\
It looks like this:

PHP Code:
<?
require("config.php");
mysql_connect($hostname,$username,$password);
@
mysql_select_db($database) or die( "Unable to select database");

$query="DROP TABLE IF EXISTS `users`
CREATE TABLE `users` (
`id` INT( 5 ) NOT NULL AUTO_INCREMENT ,
`user` VARCHAR( 30 ) NOT NULL ,
`pass` VARCHAR( 40 ) NOT NULL ,
PRIMARY KEY ( `id` )
);

mysql_query(
$query);
mysql_close(); 
echo "
Database created";
?>

But gives this error:
Parse error: parse error, unexpected T_STRING in /home/user/public_html/setup.php on line 17
which is this line:
echo "Database created";

Any help apprecciated!

Reply With Quote
  #8  
Old 07-01-2005, 12:58 PM
Vulture Vulture is offline
Junior Guru Wannabe
 
Join Date: Jul 2004
Location: Scotland, UK
Posts: 81
Just a few errors. Try this:
PHP Code:
<?php
require("config.php");
mysql_connect($hostname,$username,$password);
@
mysql_select_db($database) or die("Unable to select database");

$query="DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
`id` INT( 5 ) NOT NULL AUTO_INCREMENT ,
`user` VARCHAR( 30 ) NOT NULL ,
`pass` VARCHAR( 40 ) NOT NULL ,
PRIMARY KEY ( `id` )
);"
;

mysql_query($query);
mysql_close();
echo 
"Database created";
?>

__________________
Scott - My Spiel


Reply With Quote
  #9  
Old 07-01-2005, 01:15 PM
UrlGuy UrlGuy is offline
Aspiring Evangelist
 
Join Date: Feb 2005
Location: Norway
Posts: 391
Thanks it worked!!



Although still some error..

This is my log_in.php:

PHP Code:
<?PHP
    
include("config.php");
    @
session_start();
    if (empty(
$bruker) || empty($pass)) {
        
$logged_in false;
        
$_SESSION['logged_in'] = false;
        echo 
"<B>$no_login</B><BR>";
        include(
"login.php");
        exit;
    }



$bruker $_POST['bruker'];   
$pass $_POST['pass'];     
    
mysql_connect($hostname,$username,$password); 
@
mysql_select_db($database) or die("Unable to select that database");    
$query "SELECT * FROM users WHERE user = $bruker AND pass = $pass";
$result mysql_query($query);   


if(
$result->numRows() < 1)   
{       
echo 
"$wrong_login<BR>";
include(
"login.php");
exit;  
}   
else   
{       
$_SESSION['logged_in'] = true;
header('Location: $hidden');  
}     
mysql_close();


?>

It says this:

Fatal error: Call to a member function on a non-object in /home/user/public_html/log_in.php on line 23

like 23 is this:
if($result->numRows() < 1)

It really seems ok.. can't really get whats wrong but.. hm..
full code is the one above..

thanks again!

Reply With Quote
  #10  
Old 07-01-2005, 01:20 PM
Vulture Vulture is offline
Junior Guru Wannabe
 
Join Date: Jul 2004
Location: Scotland, UK
Posts: 81
Change line 23 to:
PHP Code:
if (mysql_num_rows($result) < 1
Should solve the problem

__________________
Scott - My Spiel


Reply With Quote
  #11  
Old 07-01-2005, 01:24 PM
UrlGuy UrlGuy is offline
Aspiring Evangelist
 
Join Date: Feb 2005
Location: Norway
Posts: 391
Tried that but gives me this error:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/user/public_html/log_in.php on line 22

always gives this when I do mysql_num_rows :\

Reply With Quote
  #12  
Old 07-01-2005, 01:26 PM
Vulture Vulture is offline
Junior Guru Wannabe
 
Join Date: Jul 2004
Location: Scotland, UK
Posts: 81
Try inserting something into the table and try again

__________________
Scott - My Spiel


Reply With Quote
  #13  
Old 07-01-2005, 01:27 PM
laserlight laserlight is offline
Web Hosting Guru
 
Join Date: May 2004
Location: Singapore
Posts: 262
That means that there is a problem with your query (assuming you correctly connected to the database server and selected a database).

Most likely it has to do with failure to quote your input:
SELECT * FROM users WHERE user = '$bruker' AND pass = '$pass'

__________________
#include<cstdio>
char*s="#include<cstdio>%cchar*s=%c%s%c;%cint main(){std::printf(s,10,34,s,34,10);}";
int main(){std::printf(s,10,34,s,34,10);}

Reply With Quote
  #14  
Old 07-01-2005, 01:35 PM
UrlGuy UrlGuy is offline
Aspiring Evangelist
 
Join Date: Feb 2005
Location: Norway
Posts: 391
Quote:
Originally posted by Vulture
Try inserting something into the table and try again
still the same :\

Reply With Quote
  #15  
Old 07-01-2005, 01:44 PM
Vulture Vulture is offline
Junior Guru Wannabe
 
Join Date: Jul 2004
Location: Scotland, UK
Posts: 81
I would take up on laserlight's idea. However I would be tempted to change the query to something like this:
PHP Code:
$query "SELECT * FROM `users` WHERE user = '".$bruker."' AND pass = '".$pass."'"
Hopefully that'll fix it.

__________________
Scott - My Spiel


Reply With Quote
Reply

Related posts from TheWhir.com
Title Type Date Posted
Web Host Rackspace Launches Private Beta for MySQL Cloud Database Web Hosting News 2011-12-01 21:09:51
Parallels Now Offering Licenses for CloudLinux OS Web Hosting News 2011-10-26 17:29:25
Web Hosting Sales and Promos Roundup - October 7, 2011 Web Hosting News 2011-10-07 19:56:18
Web Hosting Software Firm CloudLinux Releases CloudLinux OS Version 6.1 Web Hosting News 2011-09-13 14:08:49
Web Host FireHost Partners with Cloud Security Firm Gazzang for Data Encryption Web Hosting News 2011-08-16 20:33:43


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
Postbit Selector

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump
Login:
Log in with your username and password
Username:
Password:



Forgot Password?
Advertisement:
Web Hosting News:



 

X

Welcome to WebHostingTalk.com

Create your username to jump into the discussion!

WebHostingTalk.com is the largest, most influentual web hosting community on the Internet. Join us by filling in the form below.


(4 digit year)

Already a member?