Jump to content

Login if condition problem

- - - - -

  • Please log in to reply
16 replies to this topic

#1
newphpcoder

newphpcoder

    Programming Professional

  • Members
  • PipPipPipPipPipPip
  • 479 posts
Good day!

I felt difficulties today because my boss told me that I should separate my html and php code and I should use template to call my html code and used function in template. In opendb function they used mysql_fetch_array, in my old code I used mysql_num_rows how can I adopt the function of opendb for my if condition

here is my code:

<?php

error_reporting(E_ERROR | E_WARNING | E_PARSE);

include('includes/config.sender.php');

include('includes/template.inc');



session_start();


  if (isset($_SESSION['logged_in'])) {

     header('Location:machine1.php');

     die();

  }



 if (isset($_POST['submit'])) {

	$username=$_POST['username']; 

	$password=$_POST['password'];

	

	$username = addslashes_gpc($username);

	$password = addslashes_gpc($password);

	

	

	$sql_select = "SELECT

					username,

					password

			   FROM

					machine_problem_rhoda_user

			   WHERE

			   		username='$username'

					AND

					password='$password'

					";

					

	$result = $_DB->opendb($sql_select);

	

	var_dump($result);

	

	

	//var_dump($sql_select);


	//$result=mysql_query($sql_select);

	//$count = $_DB->countdata($result

	

	//$count=mysql_num_rows($result);

//as I sain in opendb them used mysql_fetch_array now my problem is what should i put in my if condition and also on my var_dump($result) the output is array(),it means it cannot get or see the data in my database?


	if($count==1){  

		$_SESSION['logged_in'] = true;

		header("location:machine1.php");

	}

	else {

	echo "<center>";

	echo "Wrong Username or Password";

	echo "</center>";

	}

}


$tpl = new Template('.', 'keep');

$tpl->set_file(array('handle' => 'html/index.html'));

$tpl->parse('handle', array('handle'));

$tpl->p('handle');

?>


I'm sorry, but the template is not my code and i dont have the rights to change it.

I hope somebody can help me.

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
It would be wise to study the functions you are using. PHP: mysql_fetch_array - Manual

In this case mysql_fetch_array will return false when no arrays can be fetched, so you can check if it is false instead of 1.

As for separating code, it is likely meant that they want you to store the error within the IF column in a variable, or in example a session variable to be displayed in a template rather than right away.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
newphpcoder

newphpcoder

    Programming Professional

  • Members
  • PipPipPipPipPipPip
  • 479 posts
I tried this code:

<?php

error_reporting(E_ERROR | E_WARNING | E_PARSE);

include('includes/config.sender.php');

include('includes/template.inc');



session_start();


  if (isset($_SESSION['logged_in'])) {

     header('Location:machine1.php');

     die();

  }



 if (isset($_POST['submit'])) {

	$username=$_POST['username']; 

	$password=$_POST['password'];

	

	$username = addslashes_gpc($username);

	$password = addslashes_gpc($password);

	

	

	$sql_select = "SELECT

					username,

					password

			   FROM

					machine_problem_rhoda_user

			   WHERE

			   		username='$username'

					AND

					password='$password'

					";

					

	$result = $_DB->opendb($sql_select);

	

	//var_dump($result);

	print_r($result);

	

	//var_dump($sql_select);


	//$result=mysql_query($sql_select);

	//$count = $_DB->countdata($result

	

	//$count=mysql_num_rows($result);


	if($result==true){  

		$_SESSION['logged_in'] = true;

		header("location:machine1.php");

	}

	else {

	echo "<center>";

	echo "Wrong Username or Password";

	echo "</center>";

	}

}









$tpl = new Template('.', 'keep');

$tpl->set_file(array('handle' => 'html/index.html'));

$tpl->parse('handle', array('handle'));

$tpl->p('handle');

?>


when I input my correct username and password the output is wrong username or password then array()

#4
ghost_x47

ghost_x47

    Learning Programmer

  • Members
  • PipPipPip
  • 45 posts
array() says that sql query returned an empty result.
since you printing it out in print_r($result);
and array() is not equal true, i think.

#5
newphpcoder

newphpcoder

    Programming Professional

  • Members
  • PipPipPipPipPipPip
  • 479 posts
when I print the $count the result is 0. Why it is happen and how can i resolved it?

Thank you

#6
ghost_x47

ghost_x47

    Learning Programmer

  • Members
  • PipPipPip
  • 45 posts
when there is no data , it's actually always 2 things you want to do
1.Echo sql query itself and see if it generated properly
2.Dump the result to see what you are getting.
then you localize the problem to :
1- my sql query is not generated properly
2- my result parsing wrong

#7
newphpcoder

newphpcoder

    Programming Professional

  • Members
  • PipPipPipPipPipPip
  • 479 posts
I echo my $sql_select
and the output is:

SELECT username, password FROM machine_problem_rhoda_user WHERE username='rhodarose' AND password='20110101' 0

#8
ghost_x47

ghost_x47

    Learning Programmer

  • Members
  • PipPipPip
  • 45 posts
You can execute it from mysql console or phpMyAdmin - and see if is there an user for that credentials.
if there is, then you must dump an $result.

#9
newphpcoder

newphpcoder

    Programming Professional

  • Members
  • PipPipPipPipPipPip
  • 479 posts

ghost_x47 said:

You can execute it from mysql console or phpMyAdmin - and see if is there an user for that credentials.
if there is, then you must dump an $result.

I used SQLYog

and when I run it I got this:

Error Code : 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '0' at line 1
(0 ms taken)

#10
ghost_x47

ghost_x47

    Learning Programmer

  • Members
  • PipPipPip
  • 45 posts
SELECT username, password FROM machine_problem_rhoda_user WHERE username='rhodarose' AND password='20110101' - this is your query - 0 is output for something else i think.

#11
newphpcoder

newphpcoder

    Programming Professional

  • Members
  • PipPipPipPipPipPip
  • 479 posts
how can I solved it?I really need to solved it today:(

#12
ghost_x47

ghost_x47

    Learning Programmer

  • Members
  • PipPipPip
  • 45 posts
SELECT username, password FROM machine_problem_rhoda_user WHERE username='rhodarose' AND password='20110101' - run this only - this is 100% valid




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users