468,272 Members | 2,083 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,272 developers. It's quick & easy.

Compare Form Data to Database

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
7 17083
ronverdonk
4,258 Expert 4TB
Copied that code to my editor and it runs fine.

Ronald :cool:
Aug 10 '06 #2
Banfa
9,033 Expert Mod 8TB
<?php>

is wrong

should be

<?php

/* Your code here */

?>
Aug 10 '06 #3
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
4,258 Expert 4TB
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
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
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
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.

Similar topics

12 posts views Thread by Pete..... | last post: by
10 posts views Thread by Gregory A Greenman | last post: by
3 posts views Thread by POLILOP | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.