Avatar of centem
centemFlag for United States of America

asked on 

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);
?>
PHP

Avatar of undefined
Last Comment
hielo
ASKER CERTIFIED SOLUTION
Avatar of dec0mpile
dec0mpile
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo