-1

i have a serious problem!

I want to use different values from different tables in my database. My tables:

users

user_id | name | email | password | color | created

parents

parent_id | email | password

So, i want to connect these tables with email col. But passwords cols are different.

Here is my code:

        $_SESSION['logged_in2'] = false;
    if( !empty( $data ) ){

        // Trim all the incoming data:
        $trimmed_data = array_map('trim', $data);

        // escape variables for security
        $email = mysqli_real_escape_string( $this->_con,  $trimmed_data['email'] );
        $password = mysqli_real_escape_string( $this->_con,  $trimmed_data['password'] );

        if((!$email) || (!$password) ) {
            throw new Exception( PARENTS_FIELDS_MISSING );
        }
        $password = md5( $password );

        $query = "SELECT parent_id, email FROM parents where email = '$email' and password = '$password' union all SELECT name FROM users ";
        $result = mysqli_query($this->_con, $query);
        $data = mysqli_fetch_assoc($result);
        $count = mysqli_num_rows($result);
        mysqli_close($this->_con);
        if( $count == 1){
            $_SESSION = $data;
            $_SESSION['logged_in2'] = true;
            return true;
        }else{
            throw new Exception( PARENTS_FAIL );
        }
    } else{
        throw new Exception( PARENTS_FIELDS_MISSING );
    }

How can i use these tables in one code? Please help! By the way, the error is this:

mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given

K. Ayaz
  • 21
  • 6

1 Answers1

0

Perhaps use a combined query(join) like this :

select a.*, b.* from table users as a join parents as b on a.email = b.email where a.email = "$email" and a.password = "$password"

updated :

SELECT b.user_id, a.email, b.email as parent_email, a.name, a.color, b.password as parent_password FROM users AS a INNER JOIN parents AS b ON a.email = b.email WHERE b.email = '$email' AND b.password  = '$password' ;

$email = mysqli_real_escape_string( $this->_con, $trimmed_data['parent_email'] );
$password = mysqli_real_escape_string( $this->_con, $trimmed_data['parent_password'] ); 
thepiyush13
  • 1,321
  • 1
  • 8
  • 9
  • It worked. But something is missing. What can i write here: $trimmed_data['email'] ); $trimmed_data['password'] ); I have an error about email and password. – K. Ayaz Dec 06 '15 at 21:21
  • what exactly is missing ? – thepiyush13 Dec 06 '15 at 21:22
  • Well, it is a registration system. And i have two tables. One is users, the other is parents. So, there are two login pages. One for children, the other one for parents. But i can't login parents page now. It gives me an error like 'email and password aren't match.' What should i do? – K. Ayaz Dec 06 '15 at 21:29
  • You can modify above query to get a.password as children_pass and b.password as parent_pass in your query , then use whichever you want. – thepiyush13 Dec 06 '15 at 21:33
  • please show your changes so I can identify the errors – thepiyush13 Dec 06 '15 at 21:44
  • SELECT b.user_id, a.email, b.email, a.name, a.color, b.password FROM users AS a INNER JOIN parents AS b ON a.email = b.email WHERE b.email AS '2' = '$email' AND b.password AS '1' = '$password' – K. Ayaz Dec 06 '15 at 21:48
  • $email = mysqli_real_escape_string( $this->_con, $trimmed_data['2'] ); $password = mysqli_real_escape_string( $this->_con, $trimmed_data['1'] ); – K. Ayaz Dec 06 '15 at 21:49
  • I'm about to kill myself! It gives me Undefined index: parent_email and Undefined index: parent_password. But why??? – K. Ayaz Dec 06 '15 at 22:10
  • I'm sorry. But I found what is the problem is. I changed '$count == 1' with '$count > 0' I'm sorry for your time. But I'm so happy now. :) – K. Ayaz Dec 06 '15 at 22:15