0

For some reason I made a query and the the query isn't executing, I tested it in the code to see if the query would execute and if it can't, to display a message and it displays the message.

I will post all the information below!

$_POST: enter image description here

Code:

    ini_set('display_errors',1); 
    error_reporting(E_ALL);
    echo "<pre>";
    var_dump($_POST);
    echo "</pre>";
    $mysqli = new mysqli("localhost", "lunar_casino", "******", "lunar_casino");
    if(isset($_POST['submit'])){
      $error = array();
      if(empty($error)){
        $bonus = $_POST['bonus'];
        $deposit = $_POST['deposit'];
        $offers = $_POST['offers'];
        $link = $_POST['link'];
        $name = $_POST['logo'];
        $q = $mysqli->query("INSERT INTO `lunar_casino`.`casino` VALUES(NULL, '$bonus', '$deposit', '$offers', '$link', '$logo', '$name', NULL)");
        if(!$q){
          echo "<font color='red'><b>There has been an error with our database! Please contact the website administrator!</b></font><br /><br />";
        } else {
          echo "<font color='green'><b>You have successfully added the casino!</b></font><br /><br />";
        }
      } else {
        echo "<font color='red'><b>There were ".count($error)." errors in your form:</b></font><br />";
        foreach($error as $err){
          echo "<b>".$err."</b><br />";
        }
        echo "<br />";
      }
    }

Structure of Database: enter image description here

If you need more information just let me know!

By the way, the way I know the error is in the query is because I checked if(!$q) and made it display an error message if the query can't be done, and it displays the error message on the page.

Any ideas why its not working? Also I left out date from the query because I don't know how to add the current date:time into the query.

If anyone could help with either of these issues please let me know! :)

7
  • Before people spam saying its vulnerable to sql injection, I do realize that, thats not the issue. Commented Jan 27, 2014 at 10:00
  • 1
    You are assuming that SQL errors will generate PHP errors. No, they won't. PHP and SQL are different languages. You still need to do error checking with the appropriate mysqli functions. Commented Jan 27, 2014 at 10:01
  • 1
    You have NOT NULL column and you INSERT NULL value. Commented Jan 27, 2014 at 10:02
  • 1
    Also, if you really think that being immediately aware of SQL injection makes us "spam" your question, how hard it is to write your queries THE SAFE WAY with prepared statement to simply do not give us even a chance to "spam"? Commented Jan 27, 2014 at 10:02
  • 2
    Try with in2.php.net/mysqli_error for any MySQL operation error. Commented Jan 27, 2014 at 10:04

1 Answer 1

1

Start by checking for errors on connection

e.g

 $db = new mysqli('localhost', 'user', 'pass', 'lunnar_casino');

 if($db->connect_errno > 0){
   die('Unable to connect to database [' . $db->connect_error . ']');
 }

Then execute your query and make sure you're using mysqli errors function to return occurred errors

e.g

$sql = <<<SQL
SELECT *
FROM `students`
WHERE `marks` < 10
SQL;

if(!$result = $db->query($sql)){
  die('An error occured [' . $db->error . ']');
}

Let me know if this worked for you, I will add more information if needed.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.