PHP and MySQL page not displaying correct conditional result.

Posted on 2013-09-12
Medium Priority
Last Modified: 2013-09-12
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
Question by:centem

Accepted Solution

dec0mpile earned 1000 total points
ID: 39486835
Why are you negating the $result variable in the if statement?
if(!$result) {

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

Assisted Solution

hielo earned 1000 total points
ID: 39486837
>>if(!$result) {...}
mysqli_query() "Returns FALSE on failure."  So your code should be:
if(false!==$result) {...}

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question