Checking if email address exists in SQL database with php

Tamas Koos

I am doing a school assignment which has to contain a page for users to register. I am getting an error with checking if their email address is already in the database, but I can't figure out why. The error is:

Notice: Undefined variable: kapcsolat in /home/hallgatok/jyhv6c/www/wf2/php_bead/register.php on line 39

Fatal error: Uncaught Error: Call to a member function prepare() on null in /home/hallgatok/jyhv6c/www/wf2/php_bead/database.php:9 Stack trace: #0 /home/hallgatok/jyhv6c/www/wf2/php_bead/register.php(9): lekerdezes(NULL, 'SELECT * FROM `...', Array) #1 /home/hallgatok/jyhv6c/www/wf2/php_bead/register.php(39): letezik(NULL, 'koostamas199753...') #2 /home/hallgatok/jyhv6c/www/wf2/php_bead/register.php(53): validate(Array, Array, Array) #3 {main} thrown in /home/hallgatok/jyhv6c/www/wf2/php_bead/database.php on line 9

My database.php, where I connect:

database.php

<?php
function kapcsolodas($kapcsolati_szoveg, $felhasznalonev = '', $jelszo = '') {
  $pdo = new PDO($kapcsolati_szoveg, $felhasznalonev, $jelszo);
  $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  return $pdo;
}

function lekerdezes($kapcsolat, $sql, $parameterek = []) {
  $stmt = $kapcsolat->prepare($sql);
  $stmt->execute($parameterek);
  return $stmt->fetchAll();
}

function vegrehajtas($kapcsolat, $sql, $parameterek = []) {
  return $kapcsolat
    ->prepare($sql)
    ->execute($parameterek);
}

$kapcsolat = kapcsolodas(
  'mysql:host=localhost;dbname=****;charset=utf8',
  '****', '****');
?>

And the page which does the work:

register.php

<?php
include('database.php');

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

function letezik($kapcsolat, $felhasznalonev) {
  $felhasznalok = lekerdezes($kapcsolat,
    "SELECT * FROM `felhasznalok` WHERE `felhasznalonev` = :felhasznalonev",
    [ ":felhasznalonev" => $felhasznalonev ]
  );
  return count($felhasznalok) === 1;
}

function regisztral($kapcsolat, $teljes_nev, $felhasznalonev, $jelszo) {
  $db = vegrehajtas($kapcsolat,
    "INSERT INTO `felhasznalok` (`teljes_nev`, `felhasznalonev`, `jelszo`) 
        values (:teljes_nev, :felhasznalonev, :jelszo)",
    [
        ":teljes_nev"       => $teljes_nev,
        ":felhasznalonev"   => $felhasznalonev,
        ":jelszo"           => password_hash($jelszo, PASSWORD_DEFAULT),
    ]
  );
  return $db === 1;
}

function validate($post, &$data, &$hibak) {
    if (trim($post['nev']) === '') {
        $hibak[] = 'Teljes név kötelező!';
    }
    else {
        $data['nev'] = $post['nev'];
    }

    if (trim($post['email']) === '') {
        $hibak[] = 'E-mail cím kötelező!';
    } else if (!filter_var($post['email'], FILTER_VALIDATE_EMAIL)) {
        $hibak[] = "Hibás e-mail cím!"; 
    } else if (letezik($kapcsolat, $post['email'])) {  <========This is line 39, but $kapcsolat was defined in database.php
        $hibak[] = "Ehhez az e-mail címhez már tartozik felhasználói fiók!";
    }
    else {
        $data['email'] = $post['email'];
    }

    $data['jelszo'] = $post['jelszo'];

    return count($hibak) === 0;
}

$hibak = [];
if ($_POST) {
    if (validate($_POST, $data, $hibak)) {
        regisztral($kapcsolat, $data['nev'], $data['email'], $data['jelszo']);
        header("Location: index.php");
        exit();
    }
}
?>

The error only occurs when that part of the if statement runs. I just can't figure out what the problem is.What am I doing wrong?

AnthonyB

As I said in a comment, your variable $kapcsolat is defined in your file database.php, but is used in the function validate.
When you create a function, all variables used in it are supposed to be local variables.
I suggest to take this variable as parameter instead of using the global variable, it's more flexible. But if you really want to use this variable, you have to declare $kapcsolat as global.

In register.php you should use:

<?php
function validate($post, &$data, &$hibak) {
    global $kapcsolat; //IMPORTANT IS HERE
    //...Some code
}

See the global keyword

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Checking if email already exists in the database

From Dev

Checking if user & email exists on MYSQL with PHP

From Dev

Checking existence of email address

From Dev

Ruby checking email address

From Dev

sending an email in php to an address stored in database

From Dev

Checking if an email exists in another table

From Dev

Checking that an email address is google based or not

From Dev

Checking that an email address is google based or not

From Dev

Checking Email Address for Optional Number

From Dev

PHP Send email to multiple address from SQL

From Dev

Checking if a value exists in drupal database

From Dev

Checking MYSQL database if row exists

From Dev

checking user name or user email already exists

From Dev

Checking if searched item exists in database - PHP won't output anything but will still add item to the database

From Dev

PHP Checking if a file exists in a directory

From Dev

Checking if a record exists with PHP and sqlite

From Dev

php mysql checking if a record exists

From Dev

PHP Checking if email is already in use

From Dev

Checking IP address using php

From Dev

How to check email address exists or not in codeigniter

From Dev

Checking if database row exists using jQuery

From Dev

Trying to checking if some user exists in my database

From Dev

Checking if a particular value exists in the Firebase database

From Dev

Trying to checking if some user exists in my database

From Dev

CakePHP Getting form value and checking into database if exists

From Dev

Java checking whether the value exists in database

From Dev

Checking if database row exists using jQuery

From Dev

Data Structures: Checking if a record exists in a database?

From Dev

Checking if user exists in MySQL database fails

Related Related

  1. 1

    Checking if email already exists in the database

  2. 2

    Checking if user & email exists on MYSQL with PHP

  3. 3

    Checking existence of email address

  4. 4

    Ruby checking email address

  5. 5

    sending an email in php to an address stored in database

  6. 6

    Checking if an email exists in another table

  7. 7

    Checking that an email address is google based or not

  8. 8

    Checking that an email address is google based or not

  9. 9

    Checking Email Address for Optional Number

  10. 10

    PHP Send email to multiple address from SQL

  11. 11

    Checking if a value exists in drupal database

  12. 12

    Checking MYSQL database if row exists

  13. 13

    checking user name or user email already exists

  14. 14

    Checking if searched item exists in database - PHP won't output anything but will still add item to the database

  15. 15

    PHP Checking if a file exists in a directory

  16. 16

    Checking if a record exists with PHP and sqlite

  17. 17

    php mysql checking if a record exists

  18. 18

    PHP Checking if email is already in use

  19. 19

    Checking IP address using php

  20. 20

    How to check email address exists or not in codeigniter

  21. 21

    Checking if database row exists using jQuery

  22. 22

    Trying to checking if some user exists in my database

  23. 23

    Checking if a particular value exists in the Firebase database

  24. 24

    Trying to checking if some user exists in my database

  25. 25

    CakePHP Getting form value and checking into database if exists

  26. 26

    Java checking whether the value exists in database

  27. 27

    Checking if database row exists using jQuery

  28. 28

    Data Structures: Checking if a record exists in a database?

  29. 29

    Checking if user exists in MySQL database fails

HotTag

Archive