K
K
Kristina87872020-06-10 09:45:50
PHP
Kristina8787, 2020-06-10 09:45:50

Why does execute return false?

Hello. I took a Zinchenko course here, maybe someone knows, everything was fine and clear until the middle of the last lesson, the createProduct method returns false

public static function createProduct($options)
  {
    // Соединение с БД
    $db = Db::getConnection();

    // Текст запроса к БД
    $sql = 'INSERT INTO product '
        . '(name, code, price, category_id, brand, availability,'
        . 'description, is_new, is_recommended, status)'
        . 'VALUES '
        . '(:name, :code, :price, :category_id, :brand, :availability,'
        . ':description, :is_new, :is_recommended, :status)';

    // Получение и возврат результатов. Используется подготовленный запрос
    $result = $db->prepare($sql);
    $result->bindParam(':name', $options['name'], PDO::PARAM_STR);
    $result->bindParam(':code', $options['code'], PDO::PARAM_STR);
    $result->bindParam(':price', $options['price'], PDO::PARAM_STR);
    $result->bindParam(':category_id', $options['category_id'], PDO::PARAM_INT);
    $result->bindParam(':brand', $options['brand'], PDO::PARAM_STR);
    $result->bindParam(':availability', $options['availability'], PDO::PARAM_INT);
    $result->bindParam(':description', $options['description'], PDO::PARAM_STR);
    $result->bindParam(':is_new', $options['is_new'], PDO::PARAM_INT);
    $result->bindParam(':is_recommended', $options['is_recommended'], PDO::PARAM_INT);
    $result->bindParam(':status', $options['status'], PDO::PARAM_INT);

    if ($result->execute()) {
      // Если запрос выполенен успешно, возвращаем id добавленной записи
      return $db->lastInsertId();
    }
    // Иначе возвращаем 0
    return 0;
  }


the $options array itself is filled with data, the connection goes to the database, the sql query is correct, but execute returns false , help me figure it out(

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
Lazy @BojackHorseman PHP, 2020-06-10
@Kristina8787

PDO Error Handling

F
FanatPHP, 2020-06-10
@FanatPHP

Vasya, I already answered this question for you .
Is there nothing in your head at all?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question