Jump to content

[SOLVED] I'm having trouble getting 2 seperate values from MySQL (should be easy fix)


adover

Recommended Posts

Hi all,

I'm basically having to update code that another designer done for the company a long time ago and I'm not the best at PHP/MySQL!

 

Basically, the login page did have something that checked the database, if there was a 1 in the SQL admin field then it would redirect to admin, if not, it would go to the index page. Now I need to add something that if another box has a 1 (mod5) then this directs to a third page! However I think my SQL query may be wrong and was wondering if someone can help as it doesn't seem to want to play ball!

<?php
if ($_POST) {
$user = $_POST['username'];
$password = $_POST['pass'];

require_once('connections/connection.php');
require_once('phpclasses/class_DB.php');

$connection = new Connection ();

$db = new DB ();
$db -> connect ($connection->database, $connection->hostname, $connection->username, $connection->password);

$sql = "SELECT * FROM pal_users WHERE username = '" . $user .  "' AND password = '" . $password . "'";

$ok = $db->get_count($sql);
$error =0;
if ($ok == 1) {
session_start();
$_SESSION['loggedin'] = "1";
$sql = "SELECT * FROM pal_users WHERE username = '" . $user .  "' AND password = '" . $password . "' AND mod5 = '1' AND admin = '1'";
$admin = $db->get_count($sql);
$mod5 = $db->get_count($sql);
$_SESSION['user'] = $user;

if ($admin == 1) {
	header("Location: admin/index.php");
} else {
	header("Location: index.php");
}
if ($mod5 == 1){
	header("Location: index_m5.php");
} else {
	header("Location: index.php");
}
} else {
$error =1;
}
}
?>

 

I'm thinking using 2 AND statements is what's causing the problem, it works fine if I take one out. Also is there a way of concatenating the 2 if's into 1? 

Cheers :)

Link to comment
Share on other sites

you dont need two queries, your original query should be enough to get the information you need.  Not entirely sure what get_count does though, does it just get the number of results ?

 

regardless, if you ran your first query and it returns one result, mysql_fetch_array on the result should contain the data from the mod5 and admin columns as well, so you just need to fetch it instead of running a second query (will be faster too)

Link to comment
Share on other sites

Not entirely sure what you mean, basically my main problem is with the

$sql = "SELECT * FROM pal_users WHERE username = '" . $user .  "' AND password = '" . $password . "' AND mod5 = '1' AND admin = '1'";

 

basically as there are 2 AND statements it seems to ignore the mod5 one, whichever way it is put in? Very confusing...

 

I've also trimmed the if statement down to

	
if ($admin == 1) {
	header("Location: admin/index.php");
} else if ($mod5 == 1){
	header("Location: index_m5.php");
} else {
	header("Location: index.php");
}

As I cannot test properly I am not sure this work.,..

Link to comment
Share on other sites

well I really dont understand this part of the code

   $admin = $db->get_count($sql);
   $mod5 = $db->get_count($sql);

you are assigning the same thing to both $admin and $mod5 as far as I can tell

Like I said before, I do not understand what get_count() does, so here is the same thing done slightly differently , at least so you can get the idea of what I am talking about

 

<?php
if ($ok == 1) {
   session_start();
   $_SESSION['loggedin'] = "1";
   $query = mysql_query($sql); //execute the query using the SQL string you provided earlier
   $row = mysql_fetch_array($query); //fetch the result of the query
   $_SESSION['user'] = $row['username'];
   
   if ($row['admin'] == 1) {
      header("Location: admin/index.php");
   } else {
      header("Location: index.php");
   }
   if ($row['mod5'] == 1){
      header("Location: index_m5.php");
   } else {
      header("Location: index.php");
   }
}
?>

Link to comment
Share on other sites

Line 112 is the end of the document! I've tried adding and removing curly braces but this is sooo confusing! :-/

I've included everything for good measure! Cheers for helping :)

<?php
if ($_POST) {
$user = $_POST['username'];
$password = $_POST['pass'];

require_once('connections/connection.php');
require_once('phpclasses/class_DB.php');

$connection = new Connection ();

$db = new DB ();
$db -> connect ($connection->database, $connection->hostname, $connection->username, $connection->password);

$sql = "SELECT * FROM pal_users WHERE username = '" . $user .  "' AND password = '" . $password . "'";

$ok = $db->get_count($sql);
$error =0;
if ($ok == 1) {
session_start();
$_SESSION['loggedin'] = "1";
$sql = "SELECT * FROM pal_users WHERE username = '" . $user .  "' AND password = '" . $password . "' AND mod5 = '1' AND admin = '1'";
$admin = $db->get_count($sql);
$mod5 = $db->get_count($sql);
$_SESSION['user'] = $user;

if ($admin == 1) {
	header("Location: admin/index.php");
} else if ($mod5 == 1){
	header("Location: index_m5.php");

} else {
	header("Location: index.php");
}
} else {
$error =1;
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Parents as Learners - Login</title>
<link href="login_ss.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
<!--
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//-->
</script>
</head>

<body class="oneColFixCtrHdr" onload="MM_preloadImages('images/login_over.gif')">

<div id="container">
  <div id="header">
    <h1> </h1>
  <!-- end #header --></div>
  <div id="mainContent">
<?php if ($error == 1) {?>
<h3>Error: username and password not found</h3>
<?php } ?>
    <form id="form1" name="form1" method="post" action="">
      <label for="username"><img src="images/usertext.jpg" /></label>
      <input name="username" type="text" id="username" />
      <br />
      <br /><label for="pass">
      <img src="images/passtxt.jpg" /></label>
      <p>
        <input name="pass" type="password" id="pass" />
      </p>
      <p> </p>
      <p><a href="mailto:pal@alite.co.uk?subject=Pal Password Reminder"><img src="images/arrow.jpg" width="15" height="14" border="0" class="imgOne"/>Forgot your password?</a></p>
      <div class="forgotpassworddiv"><!-- <input type="checkbox" name="remember" id="remember" style="margin:0 3px 0 0;width:13px;height:13px;overflow:hidden;" /><label for="remember">Remember Me</label> -->
  </div>
      <div id="submitdiv">
      <div align="right"><a href="#" onclick="document.form1.submit();">
        <img src="images/login_up.gif" alt="Submit this form" 
name="sub_but" 
width="60" height="20" border="0" onmouseover="MM_swapImage('sub_but','','images/login_over.gif',1)" onmouseout="MM_swapImgRestore()" /></a> </div>
    </div>
    </form>
<div style="clear:both;"></div>
  </div>
  <div id="footer">
    <p>© 2008 Accelerated Learning in Training and Education (Alite) Ltd. All  Rights Reserved. Registered in England and Wales. Company reg. no  04215609</p>
  <!-- end #footer --></div>
<!-- end #container --></div>
</body>
</html>

Link to comment
Share on other sites

it usually means that you are missing a closing brace or parenthesis somewhere.  if you are using a proper text editor, review all of your loops, if statements and functions to make sure they are properly closed

 

i took a quick look at it and dont see anything obviously out of place. 

note, proper indentation of code helps see where brace mismatches might be

Link to comment
Share on other sites

Hi

 

If you want to keep doing it the same way using counts then:-

 

<?php
if ($_POST) 
{
$user = $_POST['username'];
$password = $_POST['pass'];

require_once('connections/connection.php');
require_once('phpclasses/class_DB.php');

$connection = new Connection ();

$db = new DB ();
$db -> connect ($connection->database, $connection->hostname, $connection->username, $connection->password);

$sql = "SELECT * FROM pal_users WHERE username = '" . $user .  "' AND password = '" . $password . "'";

$ok = $db->get_count($sql);
$error =0;
if ($ok == 1) 
{
	session_start();
	$_SESSION['loggedin'] = "1";
	$sql = "SELECT * FROM pal_users WHERE username = '" . $user .  "' AND password = '" . $password . "' AND admin = '1'";
	$admin = $db->get_count($sql);
	$_SESSION['user'] = $user;

	if ($admin == 1) 
	{
		header("Location: admin/index.php");
	} 
	else 
	{
		$sql = "SELECT * FROM pal_users WHERE username = '" . $user .  "' AND password = '" . $password . "' AND mod5 = '1'";
		$mod5 = $db->get_count($sql);
		if ($mod5 == 1)
		{
			header("Location: index_m5.php");
		} 
		else 
		{
			header("Location: index.php");
		}
	}
} 
else 
{
	$error =1;
}
}
?>

 

However you would be better off just doing a single select similar to that suggested by lonewolf217. I don't know the syntax used by the database class you are using, but using standard php mysql calls:-

 

<?php
if ($_POST) 
{
$user = $_POST['username'];
$password = $_POST['pass'];

$conn = mysql_connect($connection->hostname,$connection->username,$connection->password);

mysql_select_db($connection->database,$conn);	

session_start();
$_SESSION['loggedin'] = "1";
$sql = "SELECT * FROM pal_users WHERE username = '" . $user .  "' AND password = '" . $password . "'";
$query = mysql_query($sql);
$row = mysql_fetch_array($query);
$_SESSION['user'] = $row['username'];

if ($row['admin'] == 1) 
{
	header("Location: admin/index.php");
} 
else 
{
	if ($row['mod5'] == 1)
	{
		header("Location: index_m5.php");
	} 
	else 
	{
		header("Location: index.php");
	}
}
}
?>

 

All the best

 

Keith

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.