Answer the question
In order to leave comments, you need to log in
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
$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;
Answer the question
In order to leave comments, you need to log in
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question