Compare Form Data to Database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dubdave
    New Member
    • Aug 2006
    • 5

    #1

    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' ;
    }
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    Copied that code to my editor and it runs fine.

    Ronald :cool:

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      <?php>

      is wrong

      should be

      <?php

      /* Your code here */

      ?>

      Comment

      • hemashiki
        New Member
        • Aug 2006
        • 34

        #4
        i think you went wrong with the AND keyword in the select query.It must be &&
        try this.

        $sel=mysql_quer y("select * from login");
        while($log=mysq l_fetch_array($ sel))
        {

        if($_POST['uname']==$log['uname'] && $_POST['pword']==$log['pword'])
        {
        echo"success";
        }
        }

        Comment

        • ronverdonk
          Recognized Expert Specialist
          • Jul 2006
          • 4259

          #5
          That is not correct Hemashiki. You can use either the && or the AND in these expressions (also || or OR, etc.).

          Ronald :cool:

          Comment

          • dubdave
            New Member
            • Aug 2006
            • 5

            #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_arr ay(): 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......

            Comment

            • hemashiki
              New Member
              • Aug 2006
              • 34

              #7
              Originally posted by dubdave
              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_arr ay(): 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_conn ect("localhost" ,"root","") or die("error".mys ql_error);
              mysql_select_db ("asc",$con) ;
              $sel=mysql_quer y("select * from login");
              while($log=mysq l_fetch_array($ sel))
              {
              if($_POST['uname']==$log['uname'] && $_POST['pword']==$log['pword'])
              {
              echo"Success";
              }
              else
              {
              echo"login failed";
              }
              }

              Comment

              • dubdave
                New Member
                • Aug 2006
                • 5

                #8
                Thanks for that - the script is now returning messages nicely. ;)

                Comment

                Working...