Andrew Matthew
Andrew Matthew

Reputation: 432

query not sending to database

Embarrassing question here as it's obviously something so simple that's going wrong. (Also for anyone who claims 'duplicate', I've spent 3 hours on here looking up relevant questions and putting numerous answers into practice but to no avail, hence the need for the question.)

I'm in the process of making a user registration form using PHP and MySQL, everything submits fine, however, I don't see the values in my database? So obviously it isn't submitting fine.

THE QUESTION

Why isn't it submitting?

I've done a var_dump($_POST); and the result is :

array(4) { ["name"]=> string(14) "Foo Bar" ["email"]=> string(18) "[email protected]" ["password"]=> string(8) "foobar123" ["submit"]=> string(8) "Register" }

I've also done print_r($query); on the query to see what's being passed and the result is:

INSERT INTO user_info (name, email, password) VALUES ('Foo Bar', '[email protected]', '$2y$10$36UQIGc6OwFrNs/TBfc6letRlrrdRGGoj.lh65puJElDJER08ozQe')

The table user_info already exists and my connection to the database is fine. I've tried using backward commas for the column names, I've also tried not using commas for the values but nothing seems to be submitting.

Here's the code:

HTML -

<form method="post" name="register-form" action="database.php">
    Name:       <input type="text" name="name"><br>
    Email:      <input type="email" name="email"><br>
    Password:   <input type="password" name="password">
            <input type="submit" name="submit" value="Register" />
</form>

PHP -

<?php

require 'dbconnect.php';

function registerUser() {
    $username = $_POST['name'];
    $email = $_POST['email'];
    $password = $_POST['password'];
    $hash = password_hash($password, PASSWORD_BCRYPT);


    $mysqli = mysqli_init();
    $query = "INSERT INTO user_info (name, email, password) VALUES ('$username', '$email', '$hash')";
    $mysqli->query($con, $query); //$con referenced in dbconnect to connect to db

    var_dump($_POST);
    print_r($query);
}

if(isset($_POST['submit']) === TRUE){
    registerUser();
}

?>

dbconnect.php

<?php

$dbserver = 'localhost';
$dbusername = 'foo';
$dbpassword = 'bar';
$dbdatabase = 'usersdb';

$con = mysqli_connect($dbserver, $dbusername, $dbpassword, $dbdatabase);

// Check connection
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>

I'm starting to think this might be permission related but it's on my localhost using XAMPP so surely it can't be? Any help would be greatly appreciated! Thank you.

Upvotes: 0

Views: 117

Answers (3)

karim karimeri
karim karimeri

Reputation: 188

This is the solution :

in your dbconnect.php reaplace this line

$con = mysqli_connect($dbserver, $dbusername, $dbpassword, $dbdatabase);

with

$mysqli = new mysqli($dbserver, $dbusername, $dbpassword, $dbdatabase);

and reaplace the content of database.php with this :

require 'dbconnect.php';

$username = $_POST['name'];
$email = $_POST['email'];
$password = $_POST['password'];
$hash = password_hash($password, PASSWORD_BCRYPT);

if(isset($_POST['submit']) === TRUE){ // $mysqli = mysqli_init(); $query = "INSERT INTO user_info (name, email, password) VALUES ('$username', '$email', '$hash')"; $mysqli->query($query); //$con referenced in dbconnect to connect to db

var_dump($_POST);
print_r($query);

}

this should work

Upvotes: 0

undefined_variable
undefined_variable

Reputation: 6218

Inside your function registerUser there is no $con defined. create a connection and your data will get inserted.

function registerUser($con) {
    $username = $_POST['name'];
    $email = $_POST['email'];
    $password = $_POST['password'];
    $hash = password_hash($password, PASSWORD_BCRYPT);


    $mysqli = mysqli_init();
    $query = "INSERT INTO user_info (name, email, password) VALUES ('$username', '$email', '$hash')";
    $mysqli->query($con, $query); //$con referenced in dbconnect to connect to db

    var_dump($_POST);
    print_r($query);
}

Upvotes: 1

Ke Vin
Ke Vin

Reputation: 2024

The problem lies here:

$mysqli = mysqli_init();
$query = "INSERT INTO user_info (name, email, password) VALUES ('$username', '$email', '$hash')";
$mysqli->query($con, $query); //$con referenced in dbconnect to connect to db

You need to establish a connection using mysqli_real_connect after mysqli_init or use the constructor new mysqli() instead of those two. Also $mysqli->query uses the $mysqli object and takes only one parameter, your query. If you have specified the connection somewhere else, you need to use mysqli_query. See http://php.net/manual/de/mysqli.query.php

Upvotes: 0

Related Questions