-3

This is my registration form and it works and sends me to the index file without problem but when I combine it with login it gives me an error

<?php
session_start();

$username="";
$email="";
$password="";
$errors=array();
$success=array();
try{
    $host='localhost';
    $user='root';
    $password='';
    $dbname='login';

//Set dsn
$dsn='mysql:host='.$host.';dbname='.$dbname;

$pdo=new PDO($dsn,$user,$password);

$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_OBJ);


}catch(Exception $e){

echo "error".$e->getMessage();
}



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


  $email=trim($_POST['email']);
  $password=trim($_POST['password']);
  $password_2=trim($_POST['password_2']);

  if(empty($email)){
    array_push($errors,'Email SHOULd be filled ');
  }
  if(empty($password)){
    array_push($errors,'Password should be filled ');
  }
  if($password !=$password_2){
    array_push($errors,"Passwords do not match");

  }

  if(count($errors)==0){
  $password_hash=md5($password);
  $insert=$pdo->prepare("INSERT INTO users (email,password) VALUES (:email,:password)");
  $insert->bindParam(':email',$email);
  $insert->bindParam(':password',$password);
  $insert->execute();
  $_SESSION['email']=$email;
  $_SESSION['success']='You have registered ';
  header('Location:index.php');
  }


}

This is my login form it not works there is some problems with my code.

Firstly I do not know should my login page's column names match to registration column names

if(isset($_POST['login'])){
  $email=trim($_POST['email']);
  $password=trim($_POST['password']);

  if(empty($email)){
  array_push($errors,'Email should be filled ');
  }
  if(empty($password)){
    array_push($errors,'Password should be filled );
  }

  if(count($errors)==0){
      $select=$con->prepare("SELECT * FROM   users WHERE email='$email' and password='$password'");
      $select->execute();
      $count=$query->fetchColumn();
      echo '111';
    //  if($c11ount=="1"){
      //    header('Location:index.php');
    //  }


  }

}

This is just a logout sistem

if(isset($_GET['logout'])){
    session_destroy();
    header('location:login.php');
}

?>
  <link rel="stylesheet" href="inde.css">

    enter code here
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Amir
  • 15
  • 1

1 Answers1

0

i found some error in your script line

  • array_push($errors,'Password should be filled);

Give $conn as database connection & replace the login code with this:

<?php
    if(isset($_POST['login'])){
      $email=trim($_POST['email']);
      $password=trim($_POST['password']);

      if(empty($email)){
      array_push($errors,'Email should be filled ');
      }
      if(empty($password)){
        array_push($errors,'Password should be filled' );
      }
    //database connection is $conn
      if(count($errors)==0){
          $select=$conn->prepare("SELECT * FROM users WHERE email='$email' and password='$password'");
          $select->execute();
          $count=count($select->fetch(PDO::FETCH_ASSOC));//fetch array and count it
      if($count=='1'){
          header('Location:index.php');
     }
      }
?>
Mahfuzar Rahman
  • 303
  • 3
  • 11