Q
Q
Q1MdDiOHL2N2018-12-23 07:44:20
PHP
Q1MdDiOHL2N, 2018-12-23 07:44:20

How to add record to SQLITE database?

I'm trying to add an entry

$webroot = $_SERVER['DOCUMENT_ROOT'];
$db = new PDO("sqlite:$webroot/db/database.db");
    if($_GET['username'] == null)
      exit();
    $username = $_GET['username'];
    $st = $db->prepare("INSERT INTO reports_blacklist_table (user_nick) VALUES (?)");
        $st->execute(array($username);

There are no errors in the response, but the record does not appear in the database either. What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Nikolaev, 2018-12-23
@gromdron

1. And who will write the brackets correctly? You have a php error: (one parenthesis is missing): 2. Read about error handling in php - php.net/manual/ru/pdo.error-handling.php

$webroot = $_SERVER['DOCUMENT_ROOT'];

try
{
  $db = new PDO("sqlite:$webroot/db/database.db");

  $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  
    $username = $_GET['username'];
  
  if ( empty($username) )
  {
    throw new \Exception("User name is empty");
  }
  
    $st = $db->prepare("INSERT INTO reports_blacklist_table (user_nick) VALUES (?)");
  
  $st->execute([$username]);
}
catch( \Exception $e )
{
  var_dump($e->getMessage());
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question