Is there a way avoid the appearance of notice msgs in PHP (in the executed page)? If yes, what's the method?

When I give random values for login and password i.e. values which are not present in the database, the output I get is--
"Notice: Undefined variable: flag in C:\wamp\www\project\Loginmodule\loginconnect2.php on line 49
You Are an Unauthorised User."

How can I get rid of the error? The code is as follows.

<?php
//initilaize the mysql user name and password
//Database Config
$dbHost='localhost'; //Database server 
$dbName='db';  // 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['votername']);
 
$pwd = addslashes($_POST['voterpwd']);

  //execute the query

  $sql ="SELECT * FROM voter";

  $result = mysql_query($sql);

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

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

	   {

	   	 $flag =1;

	   	 break;

	   }

    }

    if($flag == 1)
	{
     	 	if($row['voted']=='0')
     	 	{
     	 		$sql1="UPDATE voter SET voted='1' WHERE vid='".$row['vid']."'";
     	 		
     	 		$result1 = mysql_query($sql1);
     	 		if($row['dst']=='Chittoor')
     	 		{
     	 			header("Location:http://localhost/project/Votingmodule/ctrvoters_poll.html");
     	 		}
     	 		else if($row['dst']=='Goodoor')
     	 		{
     	 			header("Location:http://localhost/project/Votingmodule/gdrvoters_poll.html");
     	 		}
     	 		else if($row['dst']=='Hyderabad')
     	 		{
     	 			header("Location:http://localhost/project/Votingmodule/hydvoters_poll.html");
     	 		}
     	 		else if($row['dst']=='Vijayawada')
     	 		{
     	 			header("Location:http://localhost/project/Votingmodule/vjwvoters_poll.html");
     	 		}
     	 		else if($row['dst']=='Nalanda')
     	 		{
     	 			header("Location:http://localhost/project/Votingmodule/nldvoters_poll.html");
     	 		}
     	 		else if($row['dst']=='Patna')
     	 		{
     	 			header("Location:http://localhost/project/Votingmodule/ptnvoters_poll.html");
     	 		}
     	 		else if($row['dst']=='Rajgir')
     	 		{
     	 			header("Location:http://localhost/project/Votingmodule/rjrvoters_poll.html");
     	 		}
     	 		else if($row['dst']=='Vaishali')
     	 		{
     	 			header("Location:http://localhost/project/Votingmodule/vsivoters_poll.html");
     	 		}
     	 		else if($row['dst']=='Ahmedabad')
     	 		{
     	 			header("Location:http://localhost/project/Votingmodule/amdvoters_poll.html");
     	 		}
     	 		else if($row['dst']=='Gandhinagar')
     	 		{
     	 			header("Location:http://localhost/project/Votingmodule/gndvoters_poll.html");
     	 		}
     	 		else if($row['dst']=='Kaira')
     	 		{
     	 			header("Location:http://localhost/project/Votingmodule/kravoters_poll.html");
     	 		}
     	 		else if($row['dst']=='Vadodara')
     	 		{
     	 			header("Location:http://localhost/project/Votingmodule/vdrvoters_poll.html");
     	 		}
     	 		else if($row['dst']=='Ambala')
     	 		{
     	 			header("Location:http://localhost/project/Votingmodule/ambvoters_poll.html");
     	 		}
     	 		else if($row['dst']=='Jakal')
     	 		{
     	 			header("Location:http://localhost/project/Votingmodule/jklvoters_poll.html");
     	 		}
     	 		else if($row['dst']=='Sohna')
     	 		{
     	 			header("Location:http://localhost/project/Votingmodule/shnvoters_poll.html");
     	 		}
     	 		else if($row['dst']=='Panipat')
     	 		{
     	 			header("Location:http://localhost/project/Votingmodule/pptvoters_poll.html");
     	 		}
     	 		else if($row['dst']=='Ernakulam')
     	 		{
     	 			header("Location:http://localhost/project/Votingmodule/ekmvoters_poll.html");
     	 		}
     	 		else if($row['dst']=='Kozhikode')
     	 		{
     	 			header("Location:http://localhost/project/Votingmodule/kkdvoters_poll.html");
     	 		}
     	 		else if($row['dst']=='Thrissur')
     	 		{
     	 			header("Location:http://localhost/project/Votingmodule/tsrvoters_poll.html");
     	 		}
     	 		else if($row['dst']=='Trivandrum')
     	 		{
     	 			header("Location:http://localhost/project/Votingmodule/tvmvoters_poll.html");
     	 		}
     	 		else if($row['dst']=='Chennai')
     	 		{
     	 			header("Location:http://localhost/project/Votingmodule/chnvoters_poll.html");
     	 		}
     	 		else if($row['dst']=='Coimbatore')
     	 		{
     	 			header("Location:http://localhost/project/Votingmodule/cbrvoters_poll.html");
     	 		}
     	 		else if($row['dst']=='Thirunelveli')
     	 		{
     	 			header("Location:http://localhost/project/Votingmodule/tnvvoters_poll.html");
     	 		}
     	 		else if($row['dst']=='Vellore')
     	 		{
     	 			header("Location:http://localhost/project/Votingmodule/vlrvoters_poll.html");
     	 		}
     	 	}
 			else if($row['voted']=='1')
     	 	{
  				echo "Sorry!!!You can vote only once";   	 		
       	 	}
    }

    else

    {

      	  echo "You Are an Unauthorised User";

	  //how to call next \html page


    }


    mysql_free_result($result);

    mysql_close($source);

?>

It worked! Thank you so much! :)

as well as turning of reporting, why not fix the error

$flag is defined with a true value in a subroutine at line 34, if the subroutine is not executed there is nothing for line 49 to examine
before the subroutine in line 34, set $flag to false,
at line 33 $flag==0; so there is something for the test in line 49 to examine

<?php //initilaize the mysql user name and password
//Database Config
$dbHost='localhost'; //Database server 
$dbName='db'; // 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['votername']);
$pwd = addslashes($_POST['voterpwd']);
//execute the query
$sql ="SELECT * FROM voter";
$result = mysql_query($sql);
$flag = 0;
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
if($row['vid']==$username && $row['pwd']==$pwd) {
$flag =1;
break; } }
if($flag == 1) {
if($row['voted']=='0') {
$sql1="UPDATE voter SET voted='1' WHERE vid='".$row['vid']."'";
$result1 = mysql_query($sql1);
if($row['dst']=='Chittoor') { header("Location:http://localhost/project/Votingmodule/ctrvoters_poll.html"); }
elseif($row['dst']=='Goodoor') { header("Location:http://localhost/project/Votingmodule/gdrvoters_poll.html"); }
elseif($row['dst']=='Hyderabad') { header("Location:http://localhost/project/Votingmodule/hydvoters_poll.html"); }
elseif($row['dst']=='Vijayawada') { header("Location:http://localhost/project/Votingmodule/vjwvoters_poll.html"); }
elseif($row['dst']=='Nalanda') { header("Location:http://localhost/project/Votingmodule/nldvoters_poll.html"); }
elseif($row['dst']=='Patna') { header("Location:http://localhost/project/Votingmodule/ptnvoters_poll.html"); }
elseif($row['dst']=='Rajgir') { header("Location:http://localhost/project/Votingmodule/rjrvoters_poll.html"); }
elseif($row['dst']=='Vaishali') { header("Location:http://localhost/project/Votingmodule/vsivoters_poll.html"); }
elseif($row['dst']=='Ahmedabad') { header("Location:http://localhost/project/Votingmodule/amdvoters_poll.html"); }
elseif($row['dst']=='Gandhinagar') { header("Location:http://localhost/project/Votingmodule/gndvoters_poll.html"); }
elseif($row['dst']=='Kaira') { header("Location:http://localhost/project/Votingmodule/kravoters_poll.html"); }
elseif($row['dst']=='Vadodara') { header("Location:http://localhost/project/Votingmodule/vdrvoters_poll.html"); }
elseif($row['dst']=='Ambala') { header("Location:http://localhost/project/Votingmodule/ambvoters_poll.html"); }
elseif($row['dst']=='Jakal') { header("Location:http://localhost/project/Votingmodule/jklvoters_poll.html"); }
elseif($row['dst']=='Sohna') { header("Location:http://localhost/project/Votingmodule/shnvoters_poll.html"); }
elseif($row['dst']=='Panipat') { header("Location:http://localhost/project/Votingmodule/pptvoters_poll.html"); }
elseif($row['dst']=='Ernakulam') { header("Location:http://localhost/project/Votingmodule/ekmvoters_poll.html"); }
elseif($row['dst']=='Kozhikode') { header("Location:http://localhost/project/Votingmodule/kkdvoters_poll.html"); }
elseif($row['dst']=='Thrissur') { header("Location:http://localhost/project/Votingmodule/tsrvoters_poll.html"); }
elseif($row['dst']=='Trivandrum') { header("Location:http://localhost/project/Votingmodule/tvmvoters_poll.html"); }
elseif($row['dst']=='Chennai') { header("Location:http://localhost/project/Votingmodule/chnvoters_poll.html"); }
elseif($row['dst']=='Coimbatore') { header("Location:http://localhost/project/Votingmodule/cbrvoters_poll.html"); }
elseif($row['dst']=='Thirunelveli') { header("Location:http://localhost/project/Votingmodule/tnvvoters_poll.html"); }
elseif($row['dst']=='Vellore') { header("Location:http://localhost/project/Votingmodule/vlrvoters_poll.html"); }
}
elseif($row['voted']=='1') { echo "Sorry!!!You can vote only once"; }
}
else { echo "You Are an Unauthorised User"; }
//how to call next \html page
mysql_free_result($result);
mysql_close($source);?>

Thank you! I made that addition. :)

oh yeah, elseif() if minutely faster than else if()

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.