0

When I try to insert anything into my database it won't show any errors, but the data isn't in the database. I checked table name and structure and this is how its ment to be...

The SELECT was a tryout and that one is working properly, except for the INSERT INTO above, but why?

<?php
    error_reporting(0);
    require "../database/MySQL.php";

    $sql = "INSERT INTO zb_employees (name, option, image_path, email) VALUES ('1', '2', '3', '4')";
    $db->query($sql);
    echo $db->affected_rows;

    echo '<br>';

    $sql = "SELECT * FROM zb_employees";
    $result = $db->query($sql);

    if ( $result->num_rows > 0 ) {
      while($row = $result->fetch_assoc()) {
        $row["name"];
      }
    } else {
      echo "0 Employees found!";
    }

    $db->close();

1 Answers1

0

option is a reserved word in MYSQL. Hence you will need to put backticks to make it acceptable

Read more on reserved words in MYSQL

<?php
    //turn this on when in development.
            error_reporting(-1);



     $sql = "INSERT INTO zb_employees (`name`, `option`, `image_path`, `email`) VALUES ('1', '2', '3', '4')";
            $db->query($sql);
            echo $db->affected_rows;

Also consider using prepared statements. Helps against SQL injection

Rotimi
  • 4,783
  • 4
  • 18
  • 27
  • While the solution is correct, a good answer should describe the solution with text too, not just dump some code. And it's a duplicate, so it should be flagged as such, and preferably not answer it. – Qirel Mar 28 '17 at 14:22
  • omg... litterly didn't think about this, thank you it solved the problem! – Tjeu Foolen Mar 28 '17 at 14:23