我需要php Homework的帮助

I tried using this code to input data from a form into a database, but the code will not work and it keeps giving me access denied and error messages.I will submit my php code and a screen shot of the error messages in the hopes that you can find out why this dose not work.

Code & Screen Shot Below:

<?php
// Create connection
$con=mysqli_connect("<host>","<username>","<password>","<database>");
// Check connection
if (mysqli_connect_errno())
  {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="INSERT INTO usertb (username, password)
VALUES
('$_POST[username]','$_POST[password]')";

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

mysqli_close($con);
?>

Access denied would point to a DB connection error. Without seeing the error message I would say its one of two possibilities, db user being denied a connection to the DB or the user not having enough rights on the DB to be able to make changes.

Confirm that you can log into the DB with the user information you have put in you code.

mysql -D 'db name' -u 'user' -p

If you can get in OK, see if your INSERT query works at the mysql command line. If it does then you know your user can enter data into that DB and you know your query is properly formed.

If the above works all OK, then you know there is something wrong with your PHP code.

Lastly I would put a positive response on your connect and query code.

    $con=mysqli_connect("<host>","<username>","<password>","<database>");
// Check connection
if (!$con)
  {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
} else {
echo "Connected to DB successfully!<br>!";
}

Hopefully that gets you a little closer.

S