-2

I have a problem with my code. I think it's good but the console says its not:

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /home/u772833821/public_html/DB_Functions.php on line 66

Here is the code :

$result = mysqli_query($con, "INSERT INTO users SET (email, username, encrypted_password, salt, created_at) VALUES('$email', '$uname', '$encrypted_password', '$salt', NOW())");
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
  • What is the code at line 66? – AddWeb Solution Pvt Ltd Jan 19 '16 at 13:12
  • Please share the code of DB_Function.php – Parth Chavda Jan 19 '16 at 13:13
  • place below code after `mysqli_connect` line. `if (mysqli_connect_errno()) { throw new Exception(mysqli_connect_error(), mysqli_connect_errno()); } ` and check error message – AddWeb Solution Pvt Ltd Jan 19 '16 at 13:14
  • Remove `SET` from `INSERT` – AddWeb Solution Pvt Ltd Jan 19 '16 at 13:15
  • Where is `$con` set? – chris85 Jan 19 '16 at 13:17
  • I added the "if" lane and removed SET from INSERT and nothing changed. – TheLietuvis Jan 19 '16 at 13:20
  • 1
    RTM http://dev.mysql.com/doc/en/insert.html – Funk Forty Niner Jan 19 '16 at 13:21
  • 2
    Possible duplicate of [Warning: mysqli\_query() expects parameter 1 to be mysqli, null given in](http://stackoverflow.com/questions/18862743/warning-mysqli-query-expects-parameter-1-to-be-mysqli-null-given-in) – Saty Jan 19 '16 at 13:22
  • 2
    You really shouldn't use your own salts on password hashes and you really should use PHP's [built-in functions](http://jayblanchard.net/proper_password_hashing_with_PHP.html) to handle password security. If you're using a PHP version less than 5.5 you can use the `password_hash()` [compatibility pack](https://github.com/ircmaxell/password_compat). – Jay Blanchard Jan 19 '16 at 13:22
  • Plus, that error is not from your query, but from your connection. – Funk Forty Niner Jan 19 '16 at 13:23
  • 1
    The `set` made it an invalid query, separate issue from connection failing (your query also could still be invalid depending on what variables contain, look into prepared statements). Is this code in a function, did you include your db connection file, a lot of guessing still here... – chris85 Jan 19 '16 at 13:23
  • 1
    @chris85 heh, yep. I'm thinking this question's leaning towards mixing MySQL APIs, or their connection codes are off. – Funk Forty Niner Jan 19 '16 at 13:25
  • I would sugess you to bind your parameters to prevent sql injection and to make sur that your query works, because it will handle simple and double quote See this link for more information : http://www.w3schools.com/php/php_mysql_prepared_statements.asp – Pierre-Luc Bolduc Jan 19 '16 at 13:25
  • 1
    can you keep the guesswork out of things and edit your question to contain what you're using to connect with?? You're one vote away from your question being closed but that may change soon and will take more time to reopen it, if/when it does. – Funk Forty Niner Jan 19 '16 at 13:37

2 Answers2

2

Your query is incorrect. Try as below :

$result = mysqli_query($con, "INSERT INTO users (email, username, encrypted_password, salt, created_at) VALUES('$email', '$uname', '$encrypted_password', '$salt', NOW())");

Try to check $con is valid or not:

$host = "your_host"; // replace these settings for your own
$username = "your_username";
$password = "your_password_if_any";
$db = "your_db_name";
$con = mysqli_connect($host, $username, $password, $db);

if (!$con) {
    echo "Error: Unable to connect to MySQL." . PHP_EOL;
    echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
    echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
    exit;
}

References:

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
AnkiiG
  • 3,468
  • 1
  • 17
  • 28
0

Please follow steps, there are more then one correction:

1. Remove SET from INSERT query

$result = mysqli_query($con, "INSERT INTO users (email, username, encrypted_password, salt, created_at) VALUES('$email', '$uname', '$encrypted_password', '$salt', NOW())");

2. Check connection error

$con = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);

if (!$con) {
  echo "Error: Unable to connect to MySQL." . PHP_EOL;
  echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
  echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
  exit;
}

Perhaps issue with connection. Let me know what is the output from point 2.

AddWeb Solution Pvt Ltd
  • 21,025
  • 5
  • 26
  • 57