PHP and MySQL page not displaying correct conditional result.

Greetings,
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.

<?php
      // 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() . ")"
            );
      }
?>
<?php
      // 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"
   "http://www.w3.org/TR/html4/loose.dtd">

<html lang="en">
      <head>
            <title>Databases</title>
      </head>
      <body>
      

      </body>
</html>

<?php
      // 5. Close database connection
      mysqli_close($connection);
?>
centemAsked:
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.

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

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

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
hieloCommented:
>>if(!$result) {...}
mysqli_query() "Returns FALSE on failure."  So your code should be:
if(false!==$result) {...}
0
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
PHP

From novice to tech pro — start learning today.