0

Hello stackoverflow people. I need help with my script. I've got registration file and index file, when I made case for index file, that when action is register, load registration.php header,(index.php?action=register) my registration script does not work! I get error that variable mysql not found(in register.inc.php file). But when URL is normal - www.website.com/register.php then everything is OK, do you guys know what kind of problem is this? How can I fix it?

Here is my register.in.php file start:

<?php
include_once 'db_connect.php';
include_once 'psl-config.php';

$error_msg = "";

if (isset($_POST['username'], $_POST['email'], $_POST['p'])) {
    // Sanitize and validate the data passed in
    $username = filter_input(INPUT_POST, 'username', FILTER_SANITIZE_STRING);
    $email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);
    $email = filter_var($email, FILTER_VALIDATE_EMAIL);
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        // Not a valid email
        $error_msg .= '<p class="error">The email address you entered is not valid</p>';
    }

    $password = filter_input(INPUT_POST, 'p', FILTER_SANITIZE_STRING);
    if (strlen($password) != 128) {
        // The hashed pwd should be 128 characters long.
        // If it's not, something really odd has happened
        $error_msg .= '<p class="error">Invalid password configuration.</p>';
    }

    $prep_stmt = "SELECT id FROM members WHERE email = ? LIMIT 1";
    $stmt = $mysqli->prepare($prep_stmt);

Error is in the last row where $mysqli. That $mysql is from file do_connect.php:

<?php
include_once 'psl-config.php';
$mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE);

Help please?

McLaren
  • 776
  • 1
  • 10
  • 23
  • Why are you including this psl-config.php when it is already included in db_connect.php' – Happy Coding Sep 01 '15 at 09:13
  • Write the complete error here!! – Harshit Sep 01 '15 at 09:15
  • Here is the error `Notice: Undefined variable: mysqli in /home/....../public_html/includes/register.inc.php on line 30 Fatal error: Call to a member function prepare() on a non-object in /home........../public_html/includes/register.inc.php on line 30` – McLaren Sep 01 '15 at 09:22
  • 1
    Code seems to be fine. Try to put `$mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE);` just above `$prep_stmt = "SELECT id FROM members WHERE email = ? LIMIT 1"; ` and run again. – Harshit Sep 01 '15 at 09:30
  • LOL that worked THANX!!! . But why it dont take it from file then? :/ – McLaren Sep 01 '15 at 09:36
  • May be your file doesn't get included or there may be some other problem. I don't have your full code, so cant tell you exactly the reason. – Harshit Sep 01 '15 at 09:40
  • Is there are any way I could find the problem by myself? Btw write in answers :) – McLaren Sep 01 '15 at 09:43

1 Answers1

1

For tracing the error, use the step by step way. Put

$prep_stmt = "SELECT id FROM members WHERE email = ? LIMIT 1";
$stmt = $mysqli->prepare($prep_stmt);

Just below

include_once 'db_connect.php';

Put any email ID that exists in the database to the query manually. If again not working, then it may be the problem with including file. If it works, the move a step ahead.

Put

$prep_stmt = "SELECT id FROM members WHERE email = ? LIMIT 1";
$stmt = $mysqli->prepare($prep_stmt);

Just below

include_once 'psl-config.php';

then if working then put it below

if (isset($_POST['username'], $_POST['email'], $_POST['p'])) {

and so on. You will finally get the cause of error.

Harshit
  • 5,147
  • 9
  • 46
  • 93
  • When i putted bellow `include_once 'db_connect.php'; ` same error! So something is wrong with including file. – McLaren Sep 01 '15 at 10:02
  • Yes. Seems to be the same. Try go to `'db_connect.php` & put your query code directly & see if it works. Or try `echo "Working";` & run your code. If it is not including the file, then it shouldn't print `Working`. – Harshit Sep 01 '15 at 10:05
  • It prints working. So I've changed include_once with require and everything is OK now. Is there are big difference between these two include once and require? – McLaren Sep 01 '15 at 10:07