Simple Login using Ms Access ODBC

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Yew12
    New Member
    • Mar 2008
    • 23

    Simple Login using Ms Access ODBC

    I am trying to create a simple login for my webpage, but Im a bit of a novice and cannot see where Im going wrong any help would be much appreciated.

    I'm using an access database and ODBC.

    The code I have got is below;

    [PHP]<?php

    $tbl_name="tblC ustomer";

    $connstr = "DRIVER={Micros oft Access Driver (*.mdb)}; DBQ=" . realpath("2Poin tB.mdb").";";
    $conn=odbc_conn ect($connstr,'' ,'') or die(Print "connect error: ".odbc_error()) ;

    $username=$_POS T['username'];
    $password=$_POS T['password'];

    $username = stripslashes($u sername);
    $password = stripslashes($p assword);

    $sql="SELECT * FROM $tbl_name WHERE username='$user name' and password='$pass word'";
    $stmt=odbc_prep are($conn, $sql) or die (Print "odbc prepare error".odbc_err or());
    $result=odbc_ex ec($stmt) or die (Print "result error ".odbc_error()) ;

    $count=odbc_num _rows($result);

    if($count==1){

    session_registe r("username") ;
    session_registe r("password") ;
    header("locatio n:/wfolder/Logon2.php");
    }
    else {
    echo "Wrong Username or Password";
    }

    odbc_close($stm t);

    ?>[/PHP]

    Thanks
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    Assuming you have a separate login script/form that you have not shown here.

    I am unable to guess about "where Im going wrong " and I really don't want to, so: what is the problem you encounter? Any error messages? What exactly does not work? What have you debugged so far in your code?

    Ronald

    Comment

    • Yew12
      New Member
      • Mar 2008
      • 23

      #3
      Sorry I will try to clarify,

      I have one HTML page with two input boxes on and it passes the variables username and password to this php script where I want to check to see if they are valid and if so send them to another page, if not then display message "Wrong Login".

      Any other questions just ask.

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        I understand how it is supposed to work, but I asked you how you know it does not work and what problems you have! Like an error messgae, system hang, nothing at all, sql error, etc.

        A more general remark on the code: do not use session_registe r, it is deprecated.
        Originally posted by php doc
        If you want your script to work regardless of register_global s, you need to instead use the $_SESSION array as $_SESSION entries are automatically registered. If your script uses session_registe r(), it will not work in environments where the PHP directive register_global s is disabled.
        Ronald

        Comment

        • Yew12
          New Member
          • Mar 2008
          • 23

          #5
          I get the result error. with out a odbc error number.

          Comment

          • ronverdonk
            Recognized Expert Specialist
            • Jul 2006
            • 4259

            #6
            Your problem is in this statement[php]$result=odbc_ex ec($stmt) or die (Print "result error ".odbc_error()) ;[/php]This statement says: prepare and execute an sql statement. You have already prepared, so you must use[php]$result=odbc_ex ecute($stmt) or die (Print "result error ".odbc_error()) ;[/php]So:
            odbc_exec — Prepare and execute a SQL statement
            odbc_execute — Execute an already prepared statement

            Ronald

            Comment

            • Yew12
              New Member
              • Mar 2008
              • 23

              #7
              Thanks very much, but unfortunatly i still get an 07001.

              Comment

              • ronverdonk
                Recognized Expert Specialist
                • Jul 2006
                • 4259

                #8
                I cannot find the 7001 error code in the MySQL documentaiton, so why don't you just add the odbc error message to your output like[php]$result=odbc_ex ecute($stmt) or die (Print "result error ".odbc_error(). '-'.odbc_errormsg ());[/php]so you can see the error reason.

                Ronald

                Comment

                • Yew12
                  New Member
                  • Mar 2008
                  • 23

                  #9
                  Thanks, added it in the error reads

                  result error 07001-[Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 2.

                  I have tried looking checking that the names are right and everything but still no luck.

                  Also thanks for helping a novice you are helping more that you think.

                  Comment

                  • ronverdonk
                    Recognized Expert Specialist
                    • Jul 2006
                    • 4259

                    #10
                    Means you have not changed your odbc_exec into the odbc_execute statement. I.e. odbc_exec requires 2 parameters, odbc_execute requires 1 parameter.

                    And you really must use odbc_execute because you have already prepared the sql statement in the odbc_prepare!

                    Ronald

                    Comment

                    • Yew12
                      New Member
                      • Mar 2008
                      • 23

                      #11
                      I have changed it so it now reads

                      [PHP]$sql="SELECT * FROM $tbl_name WHERE username='$user name' and password='$pass word'";
                      $stmt=odbc_prep are($conn, $sql) or die (Print "odbc prepare error".odbc_err or());
                      $result=odbc_ex ecute($stmt) or die (Print "result error ".odbc_error(). '-'.odbc_errormsg ());
                      [/PHP]

                      Any other ideas.

                      Thanks so much

                      Comment

                      • ronverdonk
                        Recognized Expert Specialist
                        • Jul 2006
                        • 4259

                        #12
                        And you still have an error? If so, please show your entire code here and I will try to re-create your problem. If not, fine.

                        Btw: you do not need the prepare because you have to variables to fill in.

                        Ronald

                        Comment

                        • Yew12
                          New Member
                          • Mar 2008
                          • 23

                          #13
                          Ok first form the one which get the users input

                          [PHP]<form name="form1" method="post" action="Logon1. php">
                          <td>
                          <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFF F">
                          <tr>
                          <td colspan="3"><st rong>Login </strong></td>
                          </tr>
                          <tr>
                          <td width="78">User name</td>
                          <td width="6">:</td>
                          <td width="294"><in put name="username" type="text" id="username"> </td>
                          </tr>
                          <tr>
                          <td>Password</td>
                          <td>:</td>
                          <td><input name="password" type="text" id="password"> </td>
                          </tr>
                          <tr>
                          <td>&nbsp;</td>
                          <td>&nbsp;</td>
                          <td><input type="submit" name="Submit" value="Login"></td>
                          </tr>
                          </table>
                          </td>
                          </form>[/PHP]

                          The second;

                          [PHP]<?php

                          $tbl_name="tblC ustomer";

                          $connstr = "DRIVER={Micros oft Access Driver (*.mdb)}; DBQ=" . realpath("2Poin tB.mdb").";";
                          $conn=odbc_conn ect($connstr,'' ,'') or die(Print "connect error: ".odbc_error()) ;


                          $username=$_POS T['username'];
                          $password=$_POS T['password'];


                          $username = stripslashes($u sername);
                          $password = stripslashes($p assword);

                          $sql="SELECT * FROM $tbl_name WHERE username='$user name' and password='$pass word'";
                          $stmt=odbc_prep are($conn, $sql) or die (Print "odbc prepare error".odbc_err or());
                          $result=odbc_ex ecute($stmt) or die (Print "result error ".odbc_error(). '-'.odbc_errormsg ());


                          $count=odbc_num _rows($result);


                          if($count==1){

                          session_registe r("username") ;
                          session_registe r("password") ;
                          header("locatio n:/wfolder/Logon2.php");
                          }
                          else {
                          echo "Wrong Username or Password";
                          }

                          odbc_close($stm t);

                          ?>[/PHP]

                          Thaks for your continued support.
                          Last edited by Yew12; Apr 25 '08, 12:08 AM. Reason: spelling

                          Comment

                          • ronverdonk
                            Recognized Expert Specialist
                            • Jul 2006
                            • 4259

                            #14
                            1. according to the php documentation, many odbc drivers (Access, DB2, etc) return a wrong result with odbc_num_rows (). So do not use that, use the fetch instead.

                            2. in order to use the $_SESSION array, start a session at the very beginning of each script file that is going to use the $_SESSION array.

                            3. since you have no variables to prepare, do not prepare separately but use the obc_exec which is a porepare and execute in one statement.

                            4. the close had the wrong resource id variable.

                            5. The following should work[php]<?php
                            session_start() ;

                            $tbl_name="tblC ustomer";

                            $connstr = "DRIVER={Micros oft Access Driver (*.mdb)}; DBQ=" . realpath("2Poin tB.mdb").";";
                            $conn=odbc_conn ect($connstr,'' ,'')
                            or die("connect error: ".odbc_error()) ;

                            // get passed parameters
                            $username=trim( stripslashes($_ POST['username']));
                            $password=trim( stripslashes($_ POST['password']));

                            $sql="SELECT * FROM $tbl_name WHERE username='$user name' and password='$pass word'";

                            // prepare and execute in 1 statement
                            $result=odbc_ex ec($conn,$stmt)
                            or die ("result error ".odbc_error(). '-'.odbc_errormsg ());

                            // if no result: no rows read
                            if (!odbc_fetch_ro w($result))
                            die("Wrong Username or Password");

                            // else: all is okay
                            else {
                            $_SESSION['username']=$username;
                            $_SESSION['password']=$password;
                            header("locatio n:/wfolder/Logon2.php");
                            }
                            odbc_close($con n);
                            ?>[/php]

                            Comment

                            • Yew12
                              New Member
                              • Mar 2008
                              • 23

                              #15
                              Sorry for the late repaly.

                              Everything working as planned

                              Thanks.

                              Comment

                              Working...