473,394 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Simple Login using Ms Access ODBC

23
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="tblCustomer";

$connstr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" . realpath("2PointB.mdb").";";
$conn=odbc_connect($connstr,'','') or die(Print "connect error: ".odbc_error());

$username=$_POST['username'];
$password=$_POST['password'];

$username = stripslashes($username);
$password = stripslashes($password);

$sql="SELECT * FROM $tbl_name WHERE username='$username' and password='$password'";
$stmt=odbc_prepare($conn, $sql) or die (Print "odbc prepare error".odbc_error());
$result=odbc_exec($stmt) or die (Print "result error ".odbc_error());

$count=odbc_num_rows($result);

if($count==1){

session_register("username");
session_register("password");
header("location:/wfolder/Logon2.php");
}
else {
echo "Wrong Username or Password";
}

odbc_close($stmt);

?>[/PHP]

Thanks
Apr 23 '08 #1
15 7959
ronverdonk
4,258 Expert 4TB
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
Apr 24 '08 #2
Yew12
23
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.
Apr 24 '08 #3
ronverdonk
4,258 Expert 4TB
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_register, it is deprecated.
If you want your script to work regardless of register_globals, you need to instead use the $_SESSION array as $_SESSION entries are automatically registered. If your script uses session_register(), it will not work in environments where the PHP directive register_globals is disabled.
Ronald
Apr 24 '08 #4
Yew12
23
I get the result error. with out a odbc error number.
Apr 24 '08 #5
ronverdonk
4,258 Expert 4TB
Your problem is in this statement[php]$result=odbc_exec($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_execute($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
Apr 24 '08 #6
Yew12
23
Thanks very much, but unfortunatly i still get an 07001.
Apr 24 '08 #7
ronverdonk
4,258 Expert 4TB
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_execute($stmt) or die (Print "result error ".odbc_error().'-'.odbc_errormsg());[/php]so you can see the error reason.

Ronald
Apr 24 '08 #8
Yew12
23
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.
Apr 24 '08 #9
ronverdonk
4,258 Expert 4TB
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
Apr 24 '08 #10
Yew12
23
I have changed it so it now reads

[PHP]$sql="SELECT * FROM $tbl_name WHERE username='$username' and password='$password'";
$stmt=odbc_prepare($conn, $sql) or die (Print "odbc prepare error".odbc_error());
$result=odbc_execute($stmt) or die (Print "result error ".odbc_error().'-'.odbc_errormsg());
[/PHP]

Any other ideas.

Thanks so much
Apr 24 '08 #11
ronverdonk
4,258 Expert 4TB
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
Apr 24 '08 #12
Yew12
23
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="#FFFFFF">
<tr>
<td colspan="3"><strong>Login </strong></td>
</tr>
<tr>
<td width="78">Username</td>
<td width="6">:</td>
<td width="294"><input 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="tblCustomer";

$connstr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" . realpath("2PointB.mdb").";";
$conn=odbc_connect($connstr,'','') or die(Print "connect error: ".odbc_error());


$username=$_POST['username'];
$password=$_POST['password'];


$username = stripslashes($username);
$password = stripslashes($password);

$sql="SELECT * FROM $tbl_name WHERE username='$username' and password='$password'";
$stmt=odbc_prepare($conn, $sql) or die (Print "odbc prepare error".odbc_error());
$result=odbc_execute($stmt) or die (Print "result error ".odbc_error().'-'.odbc_errormsg());


$count=odbc_num_rows($result);


if($count==1){

session_register("username");
session_register("password");
header("location:/wfolder/Logon2.php");
}
else {
echo "Wrong Username or Password";
}

odbc_close($stmt);

?>[/PHP]

Thaks for your continued support.
Apr 25 '08 #13
ronverdonk
4,258 Expert 4TB
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="tblCustomer";

$connstr = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" . realpath("2PointB.mdb").";";
$conn=odbc_connect($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='$username' and password='$password'";

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

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

// else: all is okay
else {
$_SESSION['username']=$username;
$_SESSION['password']=$password;
header("location:/wfolder/Logon2.php");
}
odbc_close($conn);
?>[/php]
Apr 25 '08 #14
Yew12
23
Sorry for the late repaly.

Everything working as planned

Thanks.
Apr 27 '08 #15
ronverdonk
4,258 Expert 4TB
Sorry for the late repaly.

Everything working as planned

Thanks.
You mean my last post contributed or did you find the solution yourself? If the latter, please show us that solution, so we all can learn from it.

Ronald
Apr 27 '08 #16

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

Similar topics

0
by: A.P. Madura | last post by:
Need help in debugging this problem. I have an access database which I use to demonstrate Java - ODBC connectivity and it works perfectly fine. This allows me to assume that the Data Source name...
2
by: TBone | last post by:
Anyone, I have a user "john" whose machine is part of the "job" domain. He is trying to establish an odbc connection to an MS SQL 2000 server on the "school" domain. He uses Windows...
5
by: calaha | last post by:
Hi all, I have been working with this since last night, and can't quite figure out why it's not working. I have a simple login box form that is set to be my startup form in my Access app (upon...
2
by: jmev7 | last post by:
Any way to avoid having to manually enter my user name & password for the Oracle login box? I normally run a query on an attached Oracle table and have to enter the un/pw before the query will run....
4
by: anand | last post by:
Hi, I have an Access 2000 database, which contains some native tables, and some linked tables which belong to an ORACLE database, through ODBC. Using VB.NET, I am trying to fetch some data by...
6
by: N. Graves | last post by:
Thank you for taking your time to read my question... please offer your knowledge it will be appreciated! I'm writing a ASP Web page to access a Access Database that has a Database Password set....
7
by: carl.manaster | last post by:
I'm new to this game. I can find my way around C# without any trouble, and I've used Access, a little bit, in the past. Now a friend wants an application of mine to read from his Access database....
1
by: lucy.randles | last post by:
We're implementing automated logins to Oracle, using the users NT password. However, when trying to link in tables through the ODBC datasource in MS Access, we're encountering errors. Because...
0
by: Greg Corradini | last post by:
Hello all, I'm having trouble inserting an SQL selection into a new MS Access table. I get a parameter error on my insert statement when I try this (see below for code and error msg). I'm not sure...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.