PHP代码中的错误

I am doing PHP coding for first time. I got the following errors:

Errors :

Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, boolean given in C:\xampp\htdocs\331002.php on line 9

Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, boolean given in C:\xampp\htdocs\331002.php on line 10

Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, boolean given in C:\xampp\htdocs\331002.php on line 11

Notice: Undefined index: empID in C:\xampp\htdocs\331002.php on line 12

Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, boolean given in C:\xampp\htdocs\331002.php on line 12

Warning: mysqli_query() expects parameter 1 to be mysqli, boolean given in C:\xampp\htdocs\331002.php on line 17

Warning: mysqli_error() expects parameter 1 to be mysqli, boolean given in C:\xampp\htdocs\331002.php on line 18

Here's my code:

<?php
$con=mysqli_connect("localhost","root","root","student");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// escape variables for security
$FName = mysqli_real_escape_string($con, $_POST['firstName']);
$LName = mysqli_real_escape_string($con, $_POST['lastName']);
$Salary = mysqli_real_escape_string($con, $_POST['salary']);
$ID = mysqli_real_escape_string($con, $_POST['empID']);

$sql="INSERT INTO PersonInfo (FName, LName, empID, Salary)
VALUES ('$FName', '$LName','$ID','$Salary')";

if (!mysqli_query($con,$sql)) {
  die('Error: ' . mysqli_error($con));
}
echo "1 record added";

mysqli_close($con);
?>

The usual "check connection" pattern is the following:

<?php
$con=mysqli_connect("localhost","root","root","student");
// Check connection
if (false === $con) {
  // die will "finish" the script
  die("Failed to connect to MySQL: " . mysqli_connect_error());
}

mysqli_real_escape_string needs a valid connection, which you don't seem to have.

The message clearly tells you that the first argument of mysqli_real_escape_string is a data type of boolean. If you don't know the differences between data types I recommend you to look them up , knowing them is essential for programming in any language. If you check the documentation you'll see it expects "A link identifier returned by mysqli_connect() or mysqli_init()".

Your call of mysqli_connect() returns false, you want to figure out why and handle the case when it returns false. Guess there is a connection error.

There is even an example on the page of the php manual showing this:

$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s
", mysqli_connect_error());
    exit();
}

Your problem lies at the top of your script:

$con=mysqli_connect("localhost","root","root","student");
// Check connection
if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// escape variables for security
$FName = mysqli_real_escape_string($con, $_POST['firstName']);

You have checked for the mysqli connection, but if there is a connection error, your script just echoes it and continues regardless. If there is a connection error, the script should die, or at least skip the DB interactions, because it can't continue with the database transaction:

$con = mysqli_connect("example.com", "user", "password", "database");
if (mysqli_connect_errno($con)) {
   die("Failed to connect to MySQL: " . mysqli_connect_error());
}