A
A
Alexey Vesnin2018-02-22 04:48:19
PHP
Alexey Vesnin, 2018-02-22 04:48:19

What am I doing wrong when adding an entry via PDO?

Hello. I want to add a record to the table, but I get an error:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'group, utm, ref, cost, date_inserted ) VALUES (NULL, 'vkontakte', 'ta' at line 10

I don't understand what I'm doing wrong. Here is the code:
$data = array();

$data['referer_url'] = NULL;
$data['utm_source'] = NULL;
$data['utm_medium'] = NULL;
$data['utm_campaign'] = NULL;
$data['utm_term'] = NULL;
$data['utm_content'] = NULL;
$data['type'] = NULL;
$data['source'] = NULL;
$data['group'] = NULL;
$data['utm'] = NULL;
$data['ref'] = NULL;
$data['cost'] = NULL;
$data['data_inserted'] = NULL;

if(isset($_SERVER['HTTP_REFERER'])) {
      $data['referer_url'] = $_SERVER['HTTP_REFERER'];
   }

//$data['referer_url'] = $referer_url;
foreach ($parameters as $param) {
    if (isset($_SESSION[$param]) && !empty($_SESSION[$param])) {
 		$data[$param] = $_SESSION[$param];
  }
}
// Создание соединения и исключения
try {
  $db = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
   // Установить режим ошибки PDO в исключение
  $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  $db->exec("set names utf8");
   // Установка данных в таблицу
 
// Получаем текущую дату
$data['data_inserted'] = date("Y-m-d H:i:s");

// Запрос на импорт данных в базу данных

$stmt = $db->prepare("INSERT INTO utm_params (
  referer_url,
  utm_source,
  utm_medium,
  utm_campaign,
  utm_term,
  utm_content,
  type,
  source,
  group,
  utm,
  ref,
  cost,
  date_inserted
   ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");

$stmt->bindParam(1, $data['referer_url']);
$stmt->bindParam(2, $data['utm_source']);
$stmt->bindParam(3, $data['utm_medium']);
$stmt->bindParam(4, $data['utm_campaign']);
$stmt->bindParam(5, $data['utm_term']);
$stmt->bindParam(6, $data['utm_content']);
$stmt->bindParam(7, $data['type']);
$stmt->bindParam(8, $data['source']);
$stmt->bindParam(9, $data['group']);
$stmt->bindParam(10, $data['utm']);
$stmt->bindParam(11, $data['ref']);
$stmt->bindParam(12, $data['cost']);
$stmt->bindParam(13, $data['data_inserted']);

 $stmt->execute();
  echo "Успешно создана новая запись";
 }
  catch(PDOException $e) {
    echo $e->getMessage();
 }

// Закрыть подключение
$db = null;

Tell me what's wrong here??? And how else to do so that if the connection fails or some kind of error, then nothing should be displayed on the screen, since this is a redirect, that is, the program should continue to run as if nothing had happened

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mikhail Bobkov, 2018-02-22
@alexvdv

Wrap group in `` (other fields are allowed).
Group is a service word, so an error is thrown.
In order not to output anything on error, remove the output from catch

F
FanatPHP, 2018-02-22
@FanatPHP

how else to do so that if the connection fails or some kind of error, then nothing should be displayed on the screen

Probably remove the code that outputs?
Just remove what you call "exceptions": try and catch

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question