Всем привет! Помогите!
Не добавляются данные в таблицу в phpmyadmin. Хотя всё проходит.
вот код
function send(obj){
var $btn = $(obj);
var name = $.trim($("input[name=name]").val());
var tel = $.trim($("input[name=phone]").val());
var time = $("input[name=clock]:checked").val();
var aErrors = [];
if(name === ""){
aErrors.push('укажите имя');
}
if(tel === ""){
aErrors.push('укажите номер телефона');
}
if(time === undefined || $("input[name=clock]:checked").attr('disabled') === 'disabled'){
aErrors.push('выберите время');
}
if(aErrors.length){
alert(aErrors.join("\n"));
return;
}
var date = window.date.getFullYear();
var tmp = window.date.getMonth()+1;
date += "-"+((tmp < 10)? "0"+tmp: tmp);
date += "-"+((window.date.getDate() < 10)? "0"+window.date.getDate(): window.date.getDate());
date += " "+((window.date.getHours() < 10)? "0"+window.date.getHours(): window.date.getHours());
date += ":"+((window.date.getMinutes() < 10)? "0"+window.date.getMinutes(): window.date.getMinutes());
date += ":"+((window.date.getSeconds() < 10)? "0"+window.date.getSeconds(): window.date.getSeconds());
var request = {
name: name,
tel: tel,
date: date,
key: $('input[name=key]').val()
};
$btn.attr("disabled", true);
$.post("", request)
.done(function(response) {
//response.output
alert("ok");
})
.fail(function(response){
var otvet = JSON.parse(response.responseText);
alert("Ошибка(и):\n"+otvet.output);
})
.always(function() {
$btn.attr("disabled", false);
});
}
<?php
session_start();
define("DB_HOST", "localhost");
define("DB_NAME", "tankov");
define("DB_USER", "root");
define("DB_PASS", "");
define("DB_CHARSET", "UTF8");
try{
$db = new PDO(
"mysql:host=".DB_HOST.";dbname=".DB_NAME.";charset=".DB_CHARSET, DB_USER, DB_PASS,
[PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC]
);
$db->exec("SET NAMES ".DB_CHARSET);
} catch(PDOException $e) {
die("Err: not connect to database.");
}
if(isset($_SESSION['secret_key']) && isset($_POST['key']) && $_SESSION['secret_key'] === $_POST['key']){
$result = false;
$msg = "";
$aErrors = [];
$name = !empty($_POST['name'])? trim(strip_tags($_POST['name'])): "";
$tel = !empty($_POST['tel'])? trim(strip_tags($_POST['tel'])): "";
$date = !empty($_POST['date'])? trim(strip_tags($_POST['date'])): "";
if($name === ""){
$aErrors[] = "укажите имя";
}
if($tel === ""){
$aErrors[] = "укажите номер телефона";
}
if($date === ""){
$aErrors[] = "выберите время";
}
if(sizeof($aErrors) === 0){
$count = (int)$db->query('SELECT COUNT(id) AS res FROM records WHERE vremya="'.$date.'"')->fetchColumn();
if($count === 0){
$db->exec('INSERT INTO records (name, tel, vremya) VALUE("'.$name.'", "'.$tel.'", "'.$date.'");');
$result = true;
$msg = "ok";
} else {
$msg = "данне время уже назначено кем-то другим";
}
} else {
$msg = implode("\n", $aErrors);
}
if($result === false){
header("HTTP/1.0 404 Not Found");
}
die(json_encode(["output" => $msg]));
}
$_SESSION['secret_key'] = uniqid();
?>