0

I am having a problem with the execution of this particular php page. I'm using a wamp 2 server on windows7. The output is always "Unable to Open Database", no matter what changes I make to the code. Please help! I am a newbie in php!

The php code is as follows-

<?php


//initilaize the mysql user name and password

$username="localhost";

$pwd="12345";


//database name

$database="project";


//get the information from login form

$username=$_POST['adminname'];

$pwd=$_POST['adminpwd'];


//connect the mysql database

$link=@mysql_connect("localhost","root","");

@mysql_select_db($database,$link) or die("Unable to Open Database");


//execute the query

$sql ="SELECT * FROM administrator";

$result = mysql_query($sql,$link);

while ($row = mysql_fetch_array($result, MYSQL_ASSOC))

{

if($row['vid']==$username && $row['pwd']==$pwd)

{

$flag =1;

break;

}

}

if($flag == 1)

{
header("Location:http://localhost/project/Loginmodule/adminmenu.html");

}

else

{

echo "You Are an Unauthorised User";

}


mysql_free_result($result);

mysql_close($link);

?>

The HTML source code is:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script>
function funcval()
{
if(document.adminlogin.adminname.value==" ")
{
alert("Please provide all the required details");
} 
if(document.adminlogin.adminpwd.value==" ")
{
alert("Please provide all the required details");
}
}
</script> 

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Administrator Login to the Voting System</title>
</head>
<body>
<form name ="adminlogin" action="loginconnect1.php" method ="POST">
<BR><BR><center>
<H1><FONT SIZE=+7>ENTER YOUR LOGIN</H1>

<HR WIDTH=700>
<BR><BR>

<table>
<tr>
<td><FONT SIZE=+2>LOGIN </td>
<td WIDTH=300 ALIGN="CENTER"><input type="text" name="adminname"></td>
</tr>
<tr>
<td><FONT SIZE=+2>PASSWORD<SUP><FONT COLOR=RED> *</FONT></SUP> </td>
<td WIDTH=300 ALIGN="CENTER"><input type="PASSWORD" name="adminpwd"></td>
</tr>
<TR><TD></TD></TR>
<TR><TD></TD></TR>
<TR><TD></TD></TR>
<TR><TD></TD></TR>
<TR><TD></TD></TR>
<TR><TD></TD></TR>
<TR><TD ALIGN="CENTER"><INPUT TYPE="SUBMIT" NAME="BUTTON1" VALUE="SUBMIT" ONCLICK="funcval()"></TD>
<TD ALIGN="CENTER"><INPUT TYPE="RESET" NAME="BUTTON2" VALUE="REFRESH"></TD></TR> 
</TABLE>
</center>
<bR>
</form>

<bR>
<bR>









<ADDRESS><font size=+1 color=red>* : Mandatory Field</address>
4
Contributors
9
Replies
10
Views
6 Years
Discussion Span
Last Post by utthu
0

In the first file you set two variables with the username and password for MySQL.
When connecting however you don't use these variables but two constants.

Also, or die(mysql_error()) can be appended to any MySQL function, including mysql_connect.
If you would've done that, mysql_connect would throw the first error instead of mysql_select_db.

Edited by Insensus: n/a

0

Use the following lines of code to check ur database connection

$con = mysql_connect('localhost','root','');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
else
{
 echo "Connected";
}
$sel=mysql_select_db("employdb1");
if (!$sel)
  {
  die('Could not Select: ' . mysql_error());
  }
else
{
 echo "Selected";
}

Run this file first and find out whether it is running properly or not

Edited by divyakrishnan: n/a

1

Your login script is incorrect, try this code:

<?php
//initilaize the mysql user name and password
//Database Config
$dbHost='localhost'; //Database server 
$dbName='project';  // Name of the database
$dbUser='root'; // Database username
$dbPass=''; // Database password


$source = mysql_connect($dbHost, $dbUser, $dbPass);

if (!$source) {
	die('Not connected : ' . mysql_error());
}

$db_selected = mysql_select_db($DB, $source);

if (!$db_selected) {
	die ('Can\'t use $DB : ' . mysql_error());
}

//get the information from login form

$username = addslashes($_POST['adminname']);

$pwd = addslashes($_POST['adminpwd']);


//execute the query

$sql ="SELECT * FROM administrator WHERE vid = '$username' AND pwd = '$pwd'";

$result = mysql_query($sql);

if ($row = mysql_fetch_assoc($result)) {
	$flag =1;
}
if($flag == 1){
	header("Location:http://localhost/project/Loginmodule/adminmenu.html");
}else{
	echo "You Are an Unauthorised User";
}
?>
0

I tried the above login script. The problem still persists. It still says, "Unable to Open Database" no mater what Login ID and Password I enter. I'm so vexed! Now I am beginning to wonder where exactly the problem lies!

0

DivyaKrishnan, Could you please tell me how exactly do I execute the code mentioned by you?

Edited by utthu: n/a

1

utthu, with the above code I am posted be sure to change this values:

$dbName='project'; // Name of the database
$dbUser='root'; // Database username
$dbPass=''; // Database password

and also be sure that the database you are about to select exists in your database server.
Be sure also that the user you are using to connect exists and have proper permission to open the database.

0

Yes, the database exists in the server. How do I check the permissions?

0

Reyborn, you are a GENIUS!! It worked!!! I am elated!

But, I seem to be having a problem with the subsequent pages! :( Need help with those!

0

I made the required changes to the entire code. Now it is successfully working!

Thank you, Rey! God Bless!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.