5 Replies - 416 Views - Last Post: 29 November 2011 - 08:55 PM Rate Topic: -----

#1 impacter  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 16-November 11

problem using select

Posted 29 November 2011 - 07:50 PM

Hi guys, i have problem using select function in php. I dont know whats the problem but it doesnt print any characters. i tried replacing the .$_SESSION['id'] to just one of the id on my database but it works. im confused now i dont know what to do.
here is the code btw.
<?php
	session_start();	
	include 'connect.php';
	
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<link href="style/style.css" rel="stylesheet" type="text/css" />
</head>
<body>


<div id = "user">

<?php 
    // this is where the select part that im having problem with

	$sql="SELECT * from admin WHERE idno ='".$_SESSION['id']."'"; 
		$container = mysql_query($sql);
		while($row=mysql_fetch_array($container))
		{					
	echo $row['FNAME'].'<br/>';
    echo $row['LNAME'].'<br/>';
    echo $row['MNAME'].'<br/>';
    echo $row['IDNO'].'<br/>';
	}
							

?>



</div>
<div id = "userlink">
<a href="user.php"><h2>View Profile</h2></a>
<a href=""><h2>View Medical Record</h2></a>
</div>
<a href = "home.php">
<div id="Layer1" style="position:absolute; left:784px; top:151px; width:61px; height:42px; z-index:1">
</div>
</div>
<a href = "index.php"><div id = "logout"></div>
</a>
<div id="index"><img src="image/user%20page.jpg"></div>
</body>
</html>


checks for the user whether he/she is an admin , patient or doctor
<?php
session_start();
	include 'connect.php';

$user = $_REQUEST['user'];
$pass = $_REQUEST['pass'];


if($user && $pass)
{
$sql2="SELECT * FROM `admin` WHERE IDNO='$user' AND `IDNO`='$pass' AND ACCESS='admin'" ;
$sql3="SELECT * FROM `admin` WHERE IDNO='$user' AND `IDNO`='$pass' AND ACCESS='patient'" ;
$sql4="SELECT * FROM `admin` WHERE IDNO='$user' AND `IDNO`='$pass' AND ACCESS='doctor'" ;
$query2=mysql_query($sql2) or die(mysql_error());
$query3=mysql_query($sql3) or die(mysql_error());
$query4=mysql_query($sql4) or die(mysql_error());
//$query2=mysql_query($sql2) or die(mysql_error());
 
    if(mysql_num_rows($query2) > 0)
    {
      $row = mysql_fetch_assoc($query2);
      $_SESSION['id'] = $row['id'];
      //$_SESSION['username'] = $row['username'];
	  
	  echo "<script type=\"text/javascript\">window.location=\"admin.php\"</script>";
	  //set user to on
	//$sql3 =	"UPDATE `users` SET `status`='ON' WHERE `id`='".$_SESSION['id']."'";
	//$res3 = mysql_query($sql3) or die(mysql_error());
	 
	 }else if(mysql_num_rows($query3) > 0)
	 {
	  $row = mysql_fetch_assoc($query3);
      $_SESSION['id'] = $row['id'];
      //$_SESSION['username'] = $row['username'];
	  
	  echo "<script type=\"text/javascript\">window.location=\"user.php\"</script>";
	  
    }else if(mysql_num_rows($query4) > 0)
    {
      $row = mysql_fetch_assoc($query2);
      $_SESSION['id'] = $row['id'];
      $_SESSION['fullname'] = $row['firstname'];
      //$_SESSION['username'] = $row['username'];  
      echo "<script type=\"text/javascript\">window.location=\"doctor.php\"</script>";
	
    }else
   {
    echo "<script type=\"text/javascript\">
	alert(\"User Name or Password  is incorrect!\");
	window.location=\"index.php\"</script>";
   }	
}
else
{		
   echo "<script type=\"text/javascript\">
	alert(\"You need to enter a ID number and Password!\");
	window.location=\"index.php\"</script>";
	
}

?>



and lastly, the connection to the databse
<?php
$con = mysql_connect("localhost","root","") or die(mysql_error());
$db = mysql_select_db("eng",$con);



?>



i really dont know what to do now im just that dumb at php, any help would be appreciated :)

Is This A Good Question/Topic? 0
  • +

Replies To: problem using select

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2993
  • View blog
  • Posts: 10,345
  • Joined: 08-August 08

Re: problem using select

Posted 29 November 2011 - 08:00 PM

There's a lot wrong with your code, so I'll just start with the most important items.


You primary goal should be to make it easy to read your code. After all, if you could read it (meaning: understand it) then you wouldn't need help, would you? It may seem odd, but focusing on readability will get it working faster. If you focus on getting it working then much later, when it does work, you'll be afraid to "shake the jello" because you'll know that you could easily break it to the point where you might not be able to fix it.
Was This Post Helpful? 0
  • +
  • -

#3 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 947
  • View blog
  • Posts: 2,355
  • Joined: 15-February 11

Re: problem using select

Posted 29 November 2011 - 08:13 PM

Dump $_SESSION['id'] just before you use it with var_dump. If you say it works when you hard-code the value then it must be that $_SESSION['id'] isn't initialized or isn't a 'valid' id.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10575
  • View blog
  • Posts: 39,151
  • Joined: 27-December 08

Re: problem using select

Posted 29 November 2011 - 08:16 PM

Also, don't use $_REQUEST, as you leave yourself open to cookie injection attacks. Use either the $_GET or $_POST superglobal instead.
Was This Post Helpful? 0
  • +
  • -

#5 impacter  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 16-November 11

Re: problem using select

Posted 29 November 2011 - 08:54 PM

Hi guys thanks for the reply.

View Postcodeprada, on 29 November 2011 - 08:13 PM, said:

Dump $_SESSION['id'] just before you use it with var_dump. If you say it works when you hard-code the value then it must be that $_SESSION['id'] isn't initialized or isn't a 'valid' id.

It seems youre right, the value in the session isnt initialized. I think i have done the 'right' thing to do to put values in it so i really dont know what to do now, :( could you please help me fill that tiny missing field? i would really appreciate it.
Was This Post Helpful? 0
  • +
  • -

#6 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,990
  • Joined: 08-June 10

Re: problem using select

Posted 29 November 2011 - 08:55 PM

View Postimpacter, on 30 November 2011 - 02:50 AM, said:

checks for the user whether he/she is an admin , patient or doctor

I'd just like to point out how extremely inefficiency the code for this part is.

  • You are ALWAYS executing three SQL queries: one to test each of your three ACCESS values. - You do this even if the first or second test would succeed, in which case the other/s would be executed and then ignored.

  • Those three queries return ALL fields for their rows, even though you only use one of them ("id"). - As a general rule, never use the wild-card in a SELECT query used in code; rather consider it nothing more than a useful debugging tool. Instead specify each field you need to use: SELECT field1, field2, etc FROM ...


You could combine all three queries by simply returning the "ACCESS" field, which you could then test in your PHP code.
<?php
// One query to replace them all.
$sql = "SELECT `id`, `ACCESS` FROM `admin` 
		WHERE IDNO='$user' AND `IDNO`='$pass'";
$result = mysql_query($sql) or die(mysql_error());

// Make sure the result is valid and has a row.
if ($result && mysql_num_rows($result) == 1) {
    $row = mysql_fetch_assoc($result);
    $_SESSION['id'] = $row['id'];
	
	// A list of ACCESS values and their respective pages.
    $pages = array(
		"doctor" => "doctor.php",
		"patient" => "user.php",
		"admin" => "admin.php"
	);
	
	// Find the correct page for the role, defaulting to "index.php".
	$page = "index.php";
	if (in_array($row["ACCESS"], $pages)) {
		$page = $pages[$row["ACCESS"]];
	}
	
	// Redirect the user. It's better to use PHP's header
	// function rather than printing a Javascript snippet
	// to alter window.location.
	header("Location: {$page}");
}


Was This Post Helpful? 2
  • +
  • -

Page 1 of 1