PHP and MySQL page not displaying correct conditional result.

I am testing a simple php page that performs an INSERT and should echo "Success". But, it print the error message "Database query failed" even though it is successfully inserting the data into the database. Why is it not displaying "Success". Below is the code. Thank you.

      // 1. Create a database connection
      $dbhost = "localhost";
      $dbuser = "widget_cms";
      $dbpass = "secretpassword";
      $dbname = "widget_corp";
      $connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
      // Test if connection occurred.
      if(mysqli_connect_errno()) {
            die("Database connection failed: " .
                  mysqli_connect_error() .
                   " (" . mysqli_connect_errno() . ")"
      // Often these are form values in $_POST
      $menu_name = "Edit me";
      $position = (int) 4;
      $visible = (int) 1;
      // 2. Perform database query
      $query  = "INSERT INTO subjects (";
      $query .= "  menu_name, position, visible";
      $query .= ") VALUES (";
      $query .= "  '{$menu_name}', {$position}, {$visible}";
      $query .= ")";
      $result = mysqli_query($connection, $query);

      if(!$result) {
            // Success
            // redirect_to("somepage.php");
            echo "Success!";
      } else {
            // Failure
            // $message = "Subject creation failed"
            die("Database query failed. " . mysqli_error($connection));

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

<html lang="en">


      // 5. Close database connection
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Why are you negating the $result variable in the if statement?
if(!$result) {

Shouldn't it be:
if($result) {

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
>>if(!$result) {...}
mysqli_query() "Returns FALSE on failure."  So your code should be:
if(false!==$result) {...}
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.