Hi,
I have been playing around with a simple php login script and im getting an error message when i attempt to log in with the username and password i set in the sql table. The error message is as follows:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/greenpos/public_html/test4/checklogin.php on line 28
Wrong Username or Password
The code: - <?php
-
$host="localhost"; // Host name
-
$username="greenpos_admin1"; // Mysql username
-
$password="carlo"; // Mysql password
-
$db_name="greenpos_test"; // Database name
-
$tbl_name="greenpos_test.members"; // Table name
-
-
// Connect to server and select databse.
-
mysql_connect("$host", "$username", "$password")or die("cannot connect");
-
mysql_select_db("$db_name")or die("cannot select DB");
-
-
// username and password sent from signup form
-
$myusername=$_POST['myusername'];
-
$mypassword=$_POST['mypassword'];
-
-
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
-
$result=mysql_query($sql);
-
-
// Mysql_num_row is counting table row
-
$count=mysql_num_rows($result);
-
// If result matched $myusername and $mypassword, table row must be 1 row
-
-
if($count==1){
-
// Register $myusername, $mypassword and redirect to file "login_success.php"
-
session_register("myusername");
-
session_register("mypassword");
-
header("location:login_success.php");
-
}
-
else {
-
echo "Wrong Username or Password";
-
}
-
?>
--
I had created a database in sql called greenpos_test and created a table called members. -
CREATE TABLE `members` (
-
`id` INT( 4 ) NOT NULL ,
-
`myusername` VARCHAR( 65 ) NOT NULL ,
-
`mypassword` VARCHAR( 65 ) NOT NULL
-
) ENGINE = MYISAM ;
in the table i created 2 fields, myusername and mypassword and each field i put username as jason and password as jason. EG:
i put in these details in the table:
myusername - type (varchar(65) - no function - no null - and value = jason
mypassword - type (varchar(65) - no function - no null - and value = jason
If that makes any sense.
Please can someone tell me why it wont allow me to login in with the my username and password.
Regards,
Jason
[REMOVED]
20 2357
Are you using PHP 4.1.0 or higher?
The use of session_register() is depreciated
This is the preferred way.... -
$_SESSION['zim'] = "An invader from another planet.";
Write this: -
session_register("myusername")
like this: -
$_SESSION['myusername']=$myusername;
same on the password
And make sure you put
before you try to access or store Session varables.
It's preferred to put session_start() at the beginning of your php code
Please also put a exit; after you redirect a page to ensure that it will redirect to the page.
Like this: -
header("location:login_success.php");
-
exit;
Hi thanks soooo much for the reply.
i have done what you asked.
below is the code: - <?php
-
-
$host="localhost"; // Host name
-
$username="greenpos_admin1"; // Mysql username
-
$password="carlo"; // Mysql password
-
$db_name="greenpos_test"; // Database name
-
$tbl_name="members"; // Table name
-
-
// Connect to server and select databse.
-
mysql_connect("$host", "$username", "$password")or die("cannot connect");
-
mysql_select_db("$db_name")or die("cannot select DB");
-
-
// username and password sent from signup form
-
session_start();
-
$myusername=$_POST['myusername'];
-
$mypassword=$_POST['mypassword'];
-
-
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
-
$result=mysql_query($sql);
-
-
// Mysql_num_row is counting table row
-
$count=mysql_num_rows($result);
-
// If result matched $myusername and $mypassword, table row must be 1 row
-
-
if($count==1){
-
// Register $myusername, $mypassword and redirect to file "login_success.php"
-
$_SESSION['myusername']=$myusername;
-
$_SESSION['mypassword']=$mypassword;
-
header("location:login_success.php");
-
exit;
-
}
-
else {
-
echo "Wrong Username or Password";
-
}
-
?>
--
THE ERROR MESSAGE APPEARING NOW IS:
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/greenpos/public_html/test4/checklogin.php:9) in /home/greenpos/public_html/test4/checklogin.php on line 22
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/greenpos/public_html/test4/checklogin.php:9) in /home/greenpos/public_html/test4/checklogin.php on line 22
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/greenpos/public_html/test4/checklogin.php on line 30
Wrong Username or Password
please if you can please reply!!.
thanks so much.
jason
Can you try this code: -
<?php
-
-
$host="localhost"; // Host name
-
$username="greenpos_admin1"; // Mysql username
-
$password="carlo"; // Mysql password
-
$db_name="greenpos_test"; // Database name
-
$tbl_name="members"; // Table name
-
-
// Connect to server and select databse.
-
mysql_connect("$host", "$username", "$password")or die("cannot connect");
-
mysql_select_db("$db_name")or die("cannot select DB");
-
-
// username and password sent from signup form
-
-
$myusername=$_POST['myusername'];
-
$mypassword=$_POST['mypassword'];
-
-
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
-
$result=mysql_query($sql);
-
-
// Mysql_num_row is counting table row
-
$count=mysql_num_rows($result);
-
// If result matched $myusername and $mypassword, table row must be 1 row
-
-
if($count==1){
-
// Register $myusername, $mypassword and redirect to file "login_success.php"
-
session_start();
-
$_SESSION['myusername']=$myusername;
-
$_SESSION['mypassword']=$mypassword;
-
header("location:login_success.php");
-
exit;
-
}
-
else {
-
header("location: invalid.php");
-
exit;
-
}
-
?>
-
Please create an invalid.php page...
Hi Thanks once again!!!,
you have been great.
i modified the code to your code.
i still get the error message:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/greenpos/public_html/test4/checklogin.php on line 30
Wrong Username or Password
although i didnt add the invalid.php but i didnt think that mattered.
i think my problem lies in my table members..
this is what i added via a sql command to the members table in the greenpos_test database: -
CREATE TABLE `test_mysql` (
-
`myusername` varchar(65) NOT NULL default '',
-
`mypassword` varchar(65) NOT NULL default '',
-
PRIMARY KEY (`myusername`)
-
) TYPE=MyISAM AUTO_INCREMENT=0 ;
Do you think that has anything to do with it?
cheers,
jason.
this is what i put in the username and password fields in the members table:
INSERT INTO `members` VALUES (jason', 'jason');
?
Replace this code: -
// Connect to server and select databse.
-
mysql_connect("$host", "$username", "$password")or die("cannot connect");
-
mysql_select_db("$db_name")or die("cannot select DB");
-
with this: -
// Connect to server and select databse.
-
$link=mysql_connect("$host", "$username", "$password")or die("cannot connect");
-
mysql_select_db($db_name, $link)or die("cannot select DB");
-
It should work now...
Please always put :
mysql_close( $link );
after you use the database. It's a good practice
Hi,
nope that didnt work either..
error message:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/greenpos/public_html/test4/checklogin.php on line 30
Wrong Username or Password
Did you already set this connections on your db?
$host="localhost"; // Host name
$username="greenpos_admin1"; // Mysql username
$password="carlo"; // Mysql password
on the config.inc
just to let you know i am using cpanel on my own server
in the mysql section in cpanel this info comes up:
Users in greenpos_test
greenpos_admin1 (Privileges: ALL PRIVILEGES)
Connection Strings
Perl $dbh = DBI->connect("DBI:mysql:greenpos_test:localhost","gree npos_admin1","<PASSWORD HERE>");
PHP $dbh=mysql_connect ("localhost", "greenpos_admin1", "<PASSWORD HERE>") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("greenpos_test");
so i guess i have added the correct username/password for that database etc?
i dont know, im new to this, i think i have done something wrong somewhere in the setup..
cheers,
thanks for getting back to me so quick.
just to let you know i am using cpanel on my own server
in the mysql section in cpanel this info comes up:
Users in greenpos_test
greenpos_admin1 (Privileges: ALL PRIVILEGES)
Connection Strings
Perl $dbh = DBI->connect("DBI:mysql:greenpos_test:localhost","gree npos_admin1","<PASSWORD HERE>");
PHP $dbh=mysql_connect ("localhost", "greenpos_admin1", "<PASSWORD HERE>") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("greenpos_test");
so i guess i have added the correct username/password for that database etc?
i dont know, im new to this, i think i have done something wrong somewhere in the setup..
cheers,
thanks for getting back to me so quick.
Set the password to carlo according to your code.
i have.. i just cut and copied that from cpanel to show that i have setup the database and made the user and set the password and gave the user all rights.
ill reset the password and see if this helps.
any other ideas?
cheers mate.
ok so i reset the user and re-added greenpos_admin1 with password carlo and i get this error now:
Parse error: syntax error, unexpected T_OBJECT_OPERATOR in /home/greenpos/public_html/test4/checklogin.php on line 18
Any ideas?
Thanks for your help.
----
CODE BELOW: - <?php
-
-
$host="localhost"; // Host name
-
$username="greenpos_admin1"; // Mysql username
-
$password="carlo"; // Mysql password
-
$db_name="greenpos_test"; // Database name
-
$tbl_name="members"; // Table name
-
-
// Connect to server and select databse.
-
$dbh = DBI->connect("DBI:mysql:greenpos_test:localhost","greenpos_admin1","<carlo>");
-
PHP $dbh=mysql_connect ("localhost", "greenpos_admin1", "<carlo>") or die ('I cannot connect to the database because: ' . mysql_error());
-
mysql_select_db ("greenpos_test");
-
-
-
// username and password sent from signup form
-
-
$myusername=$_POST['myusername'];
-
$mypassword=$_POST['mypassword'];
-
-
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
-
$result=mysql_query($sql);
-
-
// Mysql_num_row is counting table row
-
$count=mysql_num_rows($result);
-
// If result matched $myusername and $mypassword, table row must be 1 row
-
-
if($count==1){
-
// Register $myusername, $mypassword and redirect to file "login_success.php"
-
session_start();
-
$_SESSION['myusername']=$myusername;
-
$_SESSION['mypassword']=$mypassword;
-
header("location:login_success.php");
-
exit;
-
}
-
else {
-
echo "Wrong Username or Password";
-
exit;
-
}
-
?>
code green 1,726
Recognized Expert Top Contributor
Parse error: syntax error, unexpected T_OBJECT_OPERATOR in /home/greenpos/public_html/test4/checklogin.php on line 18
This is exactly what it says it is. A syntax error on line 18. Can't work out where that is but the chevrons round the password look dodgy.
This is exactly what it says it is. A syntax error on line 18. Can't work out where that is but the chevrons round the password look dodgy.
i just removed the <> from around the passwords and still the same thing....
any ideas?
i just removed the <> from around the passwords and still the same thing....
any ideas?
no sorry: this is the new error message:
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/greenpos/public_html/test4/checklogin.php on line 30
Welcome
code green 1,726
Recognized Expert Top Contributor
This query has failed. - $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
-
$result=mysql_query($sql);
Check you have the expected values in $_POST
[PHP]$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword']; [/PHP]
It is advisable to use LIKE when comparing strings in MYSql instead of '='
pbmods 5,821
Recognized Expert Expert
Jason, please use CODE tags when posting source code. See the REPLY GUIDELINES on the right side of the page next time you post.
This query has failed. - $sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
-
$result=mysql_query($sql);
Check you have the expected values in $_POST
[PHP]$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword']; [/PHP]
It is advisable to use LIKE when comparing strings in MYSql instead of '='
Hi Codegreen,
THanks for your help also!.
you guys rock!
anyways, im very new to this all..
what exactly do you mean with your post?
i just dont understand whats going on, im positive i have followed everything right.
please get back to me, i would really appreciate it.
Cheers,
Jason.
Hi gyus,
I think i have confused everyone and myself.
Below is the code im using: - <?php
-
$host="localhost"; // Host name
-
$username="greenpos_admin1"; // Mysql username
-
$password="carlo"; // Mysql password
-
$db_name="greenpos_test"; // Database name
-
$tbl_name="members"; // Table name
-
-
// Connect to server and select databse.
-
// Connect to server and select databse.
-
$link=mysql_connect("$host", "$username", "$password")or die("cannot connect");
-
mysql_select_db($db_name, $link)or die("cannot select DB");
-
-
-
// username and password sent from signup form
-
-
$myusername=$_POST['myusername'];
-
$mypassword=$_POST['mypassword'];
-
-
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
-
$result=mysql_query($sql);
-
-
// Mysql_num_row is counting table row
-
$count=mysql_num_rows($result);
-
// If result matched $myusername and $mypassword, table row must be 1 row
-
-
if($count==1){
-
// Register $myusername, $mypassword and redirect to file "login_success.php"
-
session_start();
-
$_SESSION['myusername']=$myusername;
-
$_SESSION['mypassword']=$mypassword;
-
header("location:login_success.php");
-
exit;
-
}
-
else {
-
echo "Wrong Username or Password";
-
exit;
-
}
-
?>
This is what i did to create the table in mysql: - CREATE TABLE `members` (
-
`id` int(4) NOT NULL auto_increment,
-
`username` varchar(65) NOT NULL default '',
-
`password` varchar(65) NOT NULL default '',
-
PRIMARY KEY (`id`)
-
) TYPE=MyISAM AUTO_INCREMENT=2 ;
I then:
Dumped the data for table `members` - INSERT INTO `members` VALUES (1, 'jason', 'jason'');
Now when i use my login screen and type in my username and password
i get this error message:
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/greenpos/public_html/test4/checklogin.php:9) in /home/greenpos/public_html/test4/checklogin.php on line 37
Warning: Cannot modify header information - headers already sent by (output started at /home/greenpos/public_html/test4/checklogin.php:9) in /home/greenpos/public_html/test4/checklogin.php on line 40
line 37:
session_start();
Line 40:
header("location:login_success.php");
Im not sure if im diong everything right...
The code for my main_login.php: - <table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
-
<tr>
-
<form name="form1" method="post" action="checklogin.php">
-
<td>
-
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
-
<tr>
-
<td colspan="3"><strong>Member Login </strong></td>
-
</tr>
-
<tr>
-
<td width="78">Username</td>
-
<td width="6">:</td>
-
<td width="294"><input name="myusername" type="text" id="myusername"></td>
-
</tr>
-
<tr>
-
<td>Password</td>
-
<td>:</td>
-
<td><input name="mypassword" type="text" id="mypassword"></td>
-
</tr>
-
<tr>
-
<td> </td>
-
<td> </td>
-
<td><input type="submit" name="Submit" value="Login"></td>
-
</tr>
-
</table>
-
</td>
-
</form>
-
</tr>
-
</table>
All i can think of is that when i added the table members to my database, i added username and password as the fields and not myusername and mypassword?
Please i would really appreciate your help.
Thanks.
Jason.
-
else {
-
echo "Wrong Username or Password";
-
exit;
-
}
-
I told you to replace that part already. You cannot echo when you use session that's why you keep seeing the error about header already sent....
The alternative solution is to redirect to a page...
Are you sure you can connect to your mysql?
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Steve Fitzgerald |
last post by:
The below login script does work. The form does not seem to be
submitting. I keep getting the username and password fields. The only
errors I get are notices that email and password and undefined...
|
by: bagsmode |
last post by:
Hi,
I'm trying to set a session cookie and then redirect, however I get the
error:
Status: 302 Moved Location: /index.cgi
I thought I recall getting an error like this when I first tried...
|
by: Richard Jones |
last post by:
I'm proud to release this, the 1.1.1 release of Roundup.
Fixed in this release:
- failure with browsers not sending "Accept-Language" header
(sf bugs 1429646 and 1435335)
- translate class...
|
by: James Thomas |
last post by:
I have an issue with the login process on my site since the upgrade to
ASP .NET 2.0 (from 1.1).
I have the standard textbox and button for logging and my own custom
login process. I'd like to...
|
by: focussys |
last post by:
hi
i am a student and am doing this for one of my assignments
i am trying to create a login using perl and mysql database.i was succesful in doing that
now i want to use cookies for...
|
by: www.web20developers.com |
last post by:
http://www.web20developers.com
http://www.web20developers.com/index.php?option=com_content&task=view...
Ajallerix : AJAX, simple, fast Web image gallery demo ; at Novell
AJAX -...
|
by: warezguy05 |
last post by:
Hello
I'm experiencing a problem;
I've written a small script where volunteers can be booked for work-activities at a festival.
The festival has 5 different departments so i've created a...
|
by: Ben |
last post by:
Hello, I'll bet this has been asked a million times but I can't seem to find
a thread that gives the clear example I need.
This PC has MySQL and IIS configured and running. The MySQL database is...
|
by: mejpark |
last post by:
Good afternoon PHPers,
This morning I downloaded "PHP Users" from sourceforge to implement a
user registration system. I followed the instructions in INSTALL.txt,
and successfully configured...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |