By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,569 Members | 1,350 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,569 IT Pros & Developers. It's quick & easy.

Compare Form Data to Database

P: 5
Hi

I am trying to write a (very) basic script to compare data retrieved from a form with a mySQL database. In this case it is a simple name and password entry - if there is a corresponding entry in the database, then it prints a simple sucess message, and if not a failure messge.....

The script looks like this, and I get ' parse error, line 25,( somewhere after $query)

Any ideas?


<?php>

// compares values entered in login page form with mySQL database, and then directs either to protected page or to a failure page

$fn = trim($_POST['nameone']);
$pw = trim($_POST['password']);

$link = mysql_connect ('localhost', 'root', 'purple');
mysql_select_db ("phptest");


// if $fn and $pw match a record, then display page, else display failure page

$query = "SELECT firstname, password FROM mailinglist WHERE (firstname = '$fn' AND password = '$pw')";

$result = mysql_query ($query, $link);
if (mysql_num_rows($result) == 1) {
print 'Good to go' ;
} else {
print 'Sorry, this login is invalid' ;
}
Aug 10 '06 #1
Share this Question
Share on Google+
7 Replies


ronverdonk
Expert 2.5K+
P: 4,258
Copied that code to my editor and it runs fine.

Ronald :cool:
Aug 10 '06 #2

Banfa
Expert Mod 5K+
P: 8,916
<?php>

is wrong

should be

<?php

/* Your code here */

?>
Aug 10 '06 #3

P: 34
i think you went wrong with the AND keyword in the select query.It must be &&
try this.

$sel=mysql_query("select * from login");
while($log=mysql_fetch_array($sel))
{

if($_POST['uname']==$log['uname'] && $_POST['pword']==$log['pword'])
{
echo"success";
}
}
Aug 12 '06 #4

ronverdonk
Expert 2.5K+
P: 4,258
That is not correct Hemashiki. You can use either the && or the AND in these expressions (also || or OR, etc.).

Ronald :cool:
Aug 12 '06 #5

P: 5
OK

1) Yes <?php> is wrong - slapped wrist! - ( and I left the ?> off....) but removing the extra bracket still produces the same parse error

2) Hemashiki's script produces this error:_

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource.

3) Ronverdonk - could I ask you what OS and version of php you are running? - I am on Apple OSX 10.3.9, and php version 4.3.10


All help is gratefully received and acknowledged - this is as simple as it gets, and really ought to work. All the scripts inputting or retrieving data from mySQL are fine -i t's just this comparison of HTML form data to the mySQL database......
Aug 14 '06 #6

P: 34
OK

1) Yes <?php> is wrong - slapped wrist! - ( and I left the ?> off....) but removing the extra bracket still produces the same parse error

2) Hemashiki's script produces this error:_

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource.

3) Ronverdonk - could I ask you what OS and version of php you are running? - I am on Apple OSX 10.3.9, and php version 4.3.10


All help is gratefully received and acknowledged - this is as simple as it gets, and really ought to work. All the scripts inputting or retrieving data from mySQL are fine -i t's just this comparison of HTML form data to the mySQL database......
For this warning u have to first create a table called "login" with two fields namely uname and pword.and then execute it. Surely u will not get warning
eg:

$con=mysql_connect("localhost","root","") or die("error".mysql_error);
mysql_select_db("asc",$con);
$sel=mysql_query("select * from login");
while($log=mysql_fetch_array($sel))
{
if($_POST['uname']==$log['uname'] && $_POST['pword']==$log['pword'])
{
echo"Success";
}
else
{
echo"login failed";
}
}
Aug 14 '06 #7

P: 5
Thanks for that - the script is now returning messages nicely. ;)
Aug 14 '06 #8

Post your reply

Sign in to post your reply or Sign up for a free account.