1
Good afternoon,
I’m having trouble doing UPDATE with date but the date always stays 0000-00-00.
I’m using angular.js and mysql PDO. I have researched here on the forum some similar problems but so far unsuccessful. As a beginner, I wish someone could help me.
HTML
<div class="form-group">
<label>Data de Emissão do Token</label>
<input type="text" id="dt_token" ng-model="dt_token" autocomplete="off" class="form-control" />
</div>
JS
$("#dt_token").datepicker({
dateFormat: 'yy-mm-dd',
dayNames: ['Domingo','Segunda','Terça','Quarta','Quinta','Sexta','Sábado'],
dayNamesMin: ['D','S','T','Q','Q','S','S','D'],
dayNamesShort: ['Dom','Seg','Ter','Qua','Qui','Sex','Sáb','Dom'],
monthNames: ['Janeiro','Fevereiro','Março','Abril','Maio','Junho','Julho','Agosto','Setembro','Outubro','Novembro','Dezembro'],
monthNamesShort: ['Jan','Fev','Mar','Abr','Mai','Jun','Jul','Ago','Set','Out','Nov','Dez'],
nextText: 'Próximo',
prevText: 'Anterior',
beforeShow: function () {
setTimeout(function () {
$('.clsDatePicker').css('z-index', 100000);
}, 0);
}
});
var app = angular.module('crudApp', ['datatables']);
app.controller('crudController', function($scope, $http){
$scope.fetchData = function(){
$http.get('fetch_data.php').success(function(data){
$scope.namesData = data;
});
};
$scope.openModal = function(){
var modal_popup = angular.element('#crudmodal');
modal_popup.modal('show');
};
$scope.closeModal = function(){
var modal_popup = angular.element('#crudmodal');
modal_popup.modal('hide');
};
$scope.addData = function(){
$scope.modalTitle = 'Add Data';
$scope.submit_button = 'Insert';
$scope.openModal();
};
$scope.submitForm = function(){
$http({
method:"POST",
url:"insert.php",
data:{'nome':$scope.nome, 'token':$scope.token, 'dt_token':$scope.dt_token, 'action':$scope.submit_button, 'id':$scope.hidden_id}
}).success(function(data){
if(data.error != '')
{
$scope.success = false;
$scope.error = true;
$scope.errorMessage = data.error;
}
else
{
$scope.success = true;
$scope.error = false;
$scope.successMessage = data.message;
$scope.form_data = {};
console.log($scope.dt_token);
console.log($scope.token);
$scope.closeModal();
$scope.fetchData();
}
});
};
$scope.fetchSingleData = function(id){
$http({
method:"POST",
url:"insert.php",
data:{'id':id, 'action':'fetch_single_data'}
}).success(function(data){
$scope.nome = data.nome;
$scope.token = data.token;
$scope.dt_token = data.dt_token; //data.dt_token;
$scope.hidden_id = id;
$scope.modalTitle = 'Edit Data';
$scope.submit_button = 'Edit';
$scope.openModal();
});
};
$scope.deleteData = function(id){
if(confirm("Are you sure you want to remove it?"))
{
$http({
method:"POST",
url:"insert.php",
data:{'id':id, 'action':'Delete'}
}).success(function(data){
$scope.success = true;
$scope.error = false;
$scope.successMessage = data.message;
$scope.fetchData();
});
}
};
});
SQL
<?php
//insert.php
include('database_connection.php');
$form_data = json_decode(file_get_contents("php://input"));
$error = '';
$message = '';
$validation_error = '';
$nome = '';
$token = '';
$dt_token = '';
function debug_to_console( $data ) {
$output = $data;
if ( is_array( $output ) )
$output = implode( ',', $output);
echo "<script>console.log( 'Debug Objects: " . $output . "' );
</script>";
}
if($form_data->action == 'fetch_single_data')
{
$query = "SELECT id, nome, token, dt_token FROM funcionario WHERE
id='".$form_data->id."'";
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
foreach($result as $row)
{
$output['nome'] = $row['nome'];
$output['token'] = $row['token'];
$output['dt_token'] = $row['dt_token'];
}
}
elseif($form_data->action == "Delete")
{
$query = "
DELETE FROM funcionario WHERE id='".$form_data->id."'
";
$statement = $connect->prepare($query);
if($statement->execute())
{
$output['message'] = 'Data Deleted';
}
}
else
{
if(empty($form_data->nome))
{
$error[] = 'Nome do Funcionario e necessario';
}
else
{
$nome = $form_data->nome;
}
if(empty($form_data->token))
{
$error[] = 'Informacao do Token e necessario';
}
else
{
$token = $form_data->token;
}
if(empty($error))
{
if($form_data->action == 'Insert')
{
$data = array(
':nome' => $nome,
':token' => $token,
':dt_token' => $dt_token
);
$query = "
INSERT INTO funcionario
(nome, token, dt_token) VALUES
(:nome, :token, :dt_token)
";
$statement = $connect->prepare($query);
if($statement->execute($data))
{
$message = 'Data Inserted';
}
}
if($form_data->action == 'Edit')
{
$data = array(
':nome' => $nome,
':token' => $token,
':dt_token' => $dt_token,
':id' => $form_data->id
);
$query = "
UPDATE funcionario
SET nome = :nome, token = :token, dt_token = :dt_token WHERE id
= :id
";
$statement = $connect->prepare($query);
if($statement->execute($data))
{
$message = 'Data Edited';
}
}
}
else
{
$validation_error = implode(", ", $error);
}
$output = array(
'error' => $validation_error,
'message' => $message
);
}
echo json_encode($output);
?>
I appreciate any help.