mysql数据库连接错误

whenever I give sign up data and press sign up button data isn't inserted into the table and it says "failed". I m not getting why is that so. HTML code is as follow.

<form action="connection.php" method="post">
<label for="fname" style="color: white; margin-top: 0%;">Full Name</label>
<input type="text" id="FulName" name="FullName" placeholder="Full Name">
<br>
<label for="email" style="color: white;">Email</label>
<input type="text1" id="Email" name="UserEmail" placeholder="User Email">
<br>
<label for="fname" style="color: white;">User Name</label>
<input type="text2" id="UserName" name="UserName" placeholder="User Name">
<br>
<label for="password" style="color: white;">Password</label>
<input type="Password" id="Pass" name="Pass" placeholder="User Password">
<br>                
<input type="submit" value="Sign Up">
</form> 

php Code is as follow.

define('DB_HOST', 'localhost');
define('DB_NAME', 'nfakonline');
define('DB_USER','root');
define('DB_PASSWORD','');
$con=mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect    MySQL: " . mysqli_error());
$db=mysqli_select_db(DB_NAME,$con) or die("Failed !" . mysqli_error());

Error: Failed

In mysqli you have to pass connection object as first argument

$db=mysqli_select_db($con,DB_NAME) or die("Failed !" . mysqli_error($con));

Change from

$con=mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect    MySQL: " . mysqli_error());

to

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

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

Simple Google search will tell you that mysqli_select_db() takes first parameter as mysql connection link and second argument as dbname.

In your case it would be:

mysqli_select_db($con, DB_NAME);

Also mysqli_error() takes connection link:

mysqli_error($con);

and here is the link for you, you can read more:

http://php.net/manual/en/mysqli.select-db.php

http://php.net/manual/en/mysqli.error.php

You did 2 mistakes:

  1. You used a wrong syntax for mysqli_select_db(DB_NAME,$con).

    Correct syntax is: mysqli_select_db($con,DB_NAME).

  2. You didn't pass the connection in mysqli_error() function.

    Correct Syntax is: mysqli_error($con)

Use this PHP code:

<?php
define('DB_HOST', 'localhost');
define('DB_NAME', 'mywebsite');
define('DB_USER','root');
define('DB_PASSWORD','');
$con=mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect    MySQL: " . mysqli_error());
$db=mysqli_select_db($con,DB_NAME) or die("Failed !" . mysqli_error($con));
?>

I hope, it will help you!