-2

I had written the code in below but i saw Undefined variable problem:

In this code, there is 2 file. One file is for login and another file is for the classes. its about create and login page. Registration is not a problem its work correctly but when i want to do in existing login i face the problem. they told Undefined variable problem.

login.php(file name)
<?php include "inc/header.php"; ?>
<?php  
    $cmr = new Customer();
    if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['login'])) {
        $custLogin = $cmr->customerLogin($_POST);
    }
?>
 <div class="main">
    <div class="content">
      <div class="login_panel">
<?php
 if (isset($custLogin)) {
  echo $custLogin;
 }
?>
         <h3>Existing Customers</h3>
         <p>Sign in with the form below.</p>
         <form action="" method="post">
                 <input name="email" placeholder="Enter your email" type="text"/>
                    <input name="pass" placeholder="Enter your password" type="password"/>
                     <p class="note">If you forgot your passoword just enter your email and click <a href="#">here</a></p>
                    <div class="buttons"><div><button class="grey" name="login">Sign In</button></div></div>
                </div>
            </form>
                    
<?php  
    if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['register'])) {
        $customerReg = $cmr->customerRegistration($_POST);
    }
?>
     <div class="register_account">
<?php
 if (isset($customerReg)) {
  echo $customerReg;
 }
?>
      <h3>Register New Account</h3>
      <form action="" method="post">
         <table>
         <tbody>
      <tr>
      <td>
       <div>
       <input type="text" name="name" placeholder="Name"/>
       </div>
       
       <div>
          <input type="text" name="city" placeholder="City"/>
       </div>
       
       <div>
        <input type="text" name="zip" placeholder="Zip-Code"/>
       </div>
       <div>
        <input type="text" name="email" placeholder="Email"/>
       </div>
          </td>
         <td>
      <div>
       <input type="text" name="address" placeholder="Address"/>
      </div>
         <div>
       <input type="text" name="country" placeholder="Country"/>
      </div>  
 
              <div>
               <input type="text" name="phone" placeholder="Phone"/>
             </div>
      
       <div>
      <input type="text" name="pass" placeholder="Password"/>
       </div>
        </td>
      </tr> 
      </tbody></table> 
     <div class="search"><div><button class="grey" name="register">Create Account</button></div></div>
      <p class="terms">By clicking 'Create Account' you agree to the <a href="#">Terms &amp; Conditions</a>.</p>
      <div class="clear"></div>
      </form>
     </div>   
       <div class="clear"></div>
    </div>
 </div>
<?php include "inc/footer.php"; ?>



//////////////////////////////////////////////////

Customer.php (file name)

/////////////////////////////////////////////////

<?php
 $filepath = realpath(dirname(__FILE__)); 
 include_once ($filepath."/../lib/Database.php");
 include_once ($filepath."/../helpers/Format.php");
?>
<?php

 class Customer{
 private $db;
 private $fm;
  
 public function __construct(){
  $this->db = new Database();    
  $this->fm = new Format();   
 }

 public function customerRegistration($data){
  $name     = $this->fm->validation($data['name']);
  $address  = $this->fm->validation($data['address']);
  $city   = $this->fm->validation($data['city']);
  $country  = $this->fm->validation($data['country']);
  $zip   = $this->fm->validation($data['zip']);
  $phone    = $this->fm->validation($data['phone']);
  $email    = $this->fm->validation($data['email']);
  $pass    = $this->fm->validation($data['pass']);

  $name     = mysqli_real_escape_string($this->db->link, $data['name']);
  $address  = mysqli_real_escape_string($this->db->link, $data['address']);
  $city    = mysqli_real_escape_string($this->db->link, $data['city']);
  $country  = mysqli_real_escape_string($this->db->link, $data['country']);
  $zip    = mysqli_real_escape_string($this->db->link, $data['zip']);
  $phone    = mysqli_real_escape_string($this->db->link, $data['phone']);
  $email    = mysqli_real_escape_string($this->db->link, $data['email']);
  $pass     = mysqli_real_escape_string($this->db->link, md5($data['pass']));

  if ($name == "" || $address == "" || $city == "" || $country == "" || $zip == "" || $phone == "" || $email == "" || $pass == ""){
      $msg = "<span class='error'>Field must not be empty !</span>";
   return $msg;
     }
     $mailquery = "select * from tbl_customer where email='$email' limit 1";
     $mailchk   = $this->db->select($mailquery);
     if ($mailchk != false) {
      $msg = "<span class='error'>Email already exist!</span>";
   return $msg;
     } else {
      $query = "insert into tbl_customer(name,address,city,country,zip,phone,email,pass) 
           values ('$name','$address','$city','$country','$zip','$phone','$email','$pass')";
   $userinsert = $this->db->insert($query);
   if ($userinsert) {
    $msg = "<span class='success'>Customer Data Added Successfully !</span>";
    return $msg;
   } else {
    $msg = "<span class='error'>Customer Data not added !</span>";
    return $msg;
   }
     }
 }

 public function customerLogin($date){
  $email    = $this->fm->validation($data['email']);
  $pass    = $this->fm->validation($data['pass']);

  $email    = mysqli_real_escape_string($this->db->link, $data['email']);
  $pass     = mysqli_real_escape_string($this->db->link, md5($data['pass']));

  if (empty($email) || empty($pass)) {
   $msg = "<span class='error'>Field must not be empty !</span>";
   return $msg;
  }

  $query  = "select * from tbl_customer where email = '$email' AND pass = '$pass'";
  $result = $this->db->select($query);
  if ($result != false) {
   $value = $result->fetch_assoc();
   Session::set("cuslogin",true);
   Session::set("cmrId",$value['id']);
   Session::set("cmrName",$value['name']);
   header("Location:order.php");
  } else {
   $msg = "<span class='error'>Email or Password doesnot match!</span>";
   return $msg;
  }
 }
}
?>

public function customerLogin($date){ $email = $this->fm->validation($data['email']); $pass = $this->fm->validation($data['pass']);

    $email    = mysqli_real_escape_string($this->db->link, $data['email']);
    $pass     = mysqli_real_escape_string($this->db->link, md5($data['pass']));

these 4 line i faced a problem and the problem is below:

Notice: Undefined variable: data in D:\xampp\htdocs\shop\classes\Customer.php on line 60

Notice: Undefined variable: data in D:\xampp\htdocs\shop\classes\Customer.php on line 61

Notice: Undefined variable: data in D:\xampp\htdocs\shop\classes\Customer.php on line 63

Notice: Undefined variable: data in D:\xampp\htdocs\shop\classes\Customer.php on line 64

Milad Bahmanabadi
  • 946
  • 11
  • 27
  • Your method is called with `customerLogin($date){` and should be `customerLogin($data){` – Nigel Ren Jan 21 '19 at 07:20
  • the function has a parameter `$date` yet you try to access `$data`?? typo! Also - the code is vulnerable to sql injection potentially – Professor Abronsius Jan 21 '19 at 07:21
  • Unsalted passwords hashes by md5 is a bad idea for the last 10 years. Aside from that you should really look up some high quality teaching books or tutorials. Debugging such a problem should be a matter of 1 minute. – msphn Jan 21 '19 at 08:07

1 Answers1

0

on row 59 - type-o

change this

public function customerLogin($date){

to

public function customerLogin($data){
Brainmaniac
  • 2,203
  • 4
  • 29
  • 53