1

I am attempting to insert data into my users table in my database whenever a user signs up, however, I am having trouble doing so..

I attempted to do it on my index.php file (which worked perfectly) as a test:

if(empty($_POST['post'])) {
    mysqli_query($conn,"INSERT INTO users (fname,lname,group) VALUES ('John','Doe',1)");
}

I then attempted to do it in my register form, which didn't work so well, and I am trying to figure out why...

Here is the register function in the "users.php" file:

function register_user($register_data) {
    array_walk($register_data, 'array_sanitize');
    $register_data['password'] = md5($register_data['password']);

    $fields = '`' . implode('`, `', array_keys($register_data)) . '`';
    $data = '\'' . implode('\', \'', $register_data) . '\'';

    mysqli_query($conn, "INSERT INTO users ($fields) VALUES ($data)");
}

And the code I used on the "register.php" page:

require_once 'core/init.php';

$conn = new mysqli($host, $user, $pass, $_db);

if(empty($_POST) === false) {
    $register_data = array(
        'username'   => $_POST['username'],
        'password'   => $_POST['password'],
        'fname'      => $_POST['fname'],
        'lname'      => $_POST['lname'],
        'email'      => $_POST['email']
    );
    register_user($register_data);
    header('location: register.php?success');
    exit();
}

And finally, my Initialization file "init.php":

session_start();
    //error_reporting(0);

    require '_db/connect.php';
    require '_users/general.php';
    require '_users/users.php'; 

Note that inside of my "connect.php" file is the database connection script:

$host = "127.0.0.1";
$user = "root";
$pass = "";
$_db = "test";

//connect script
$conn = new mysqli($host, $user, $pass, $_db);

Any and all help is appreciated!

Thanks!

GROVER.
  • 4,071
  • 2
  • 19
  • 66
  • are you getting any errors? – CodeGodie Jan 30 '16 at 04:20
  • @CodeGodie none, I have made sure that It output's all errors too by commenting out `error_reporting(0)` – GROVER. Jan 30 '16 at 04:23
  • You have a variable scope issue - http://php.net/manual/en/language.variables.scope.php. `$conn` is not in scope when you call it inside `register_user()`. – Sean Jan 30 '16 at 04:24
  • Possible duplicate of [Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?](http://stackoverflow.com/questions/16959576/reference-what-is-variable-scope-which-variables-are-accessible-from-where-and) – Sean Jan 30 '16 at 04:25
  • What do you mean `no it's not`? **Your** [selected answer](http://stackoverflow.com/a/35097946/689579) is telling you that you have a variable scope issues - http://stackoverflow.com/a/35097946/689579. – Sean Jan 30 '16 at 04:32
  • @Sean I am aware of that... But, I'm not asking what a variable scope is... I am asking what I have done wrong, I did not know I messed something up in my variable scope, it could've been anything else for all I knew – GROVER. Jan 30 '16 at 04:35

1 Answers1

2

the $conn object is not being passed to your register_user() function, therefore the query fails.

pass it like this: register_user($register_data, $conn);

and receive it like this: function register_user($register_data, $conn){...}

CodeGodie
  • 12,116
  • 6
  • 37
  • 66