Всем доброго дня!
Не могу понять почему не хочет удалять из базы строку.
Подскажите, пожалуйста в чём проблема.
Есть кнопка в модальном окне, при нажатии на которую должна удалиться строка и закрыться окно. Окно закрывается, но не удаляется строка.
Как в шаблоне, так и в контроллере большой код - писать весь не буду - много. Весь код напишу только в Модели.
Спасибо!
Шаблон:
<div class="modal fade" id="Lep_Modal_Delete" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="myModalLabel">Подтверждение удаления</h4>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>Вы действительно хотите удалить данный договор?</p>
<table class="table table-striped table-bordered">
<tbody>
<tr>
<td>
<strong>ID номер:</strong>
</td>
<td>
<span id="Lep_Delete_LepID"></span>
</td>
</tr>
<tr>
<td>
<strong>Договор №:</strong>
</td>
<td>
<span id="Lep_Delete_LepNr"></span>
</td>
</tr>
<tr>
<td>
<strong>Имя клиента:</strong>
</td>
<td>
<span id="Lep_Delete_KlientNimi"></span>
</td>
</tr>
<tr>
<td>
<strong>Фамилия клиента:</strong>
</td>
<td>
<span id="Lep_Delete_KlientPNimi"></span>
</td>
</tr>
</tbody>
</table>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-danger" id="Lep_Delete_Confirm">Delete</button>
</div>
</div>
</div>
</div>
<script>
$('.Lep_LepID_delete_confirm').click(function () {
var lepid = $(this).data('lepid');
var lepnr = $(this).data('lepnr');
var klientnimi = $(this).data('klientnimi');
var klientpnimi = $(this).data('klientpnimi');
$('#Lep_Delete_LepID').html(lepid);
$('#Lep_Delete_LepNr').html(lepnr);
$('#Lep_Delete_KlientNimi').html(klientnimi);
$('#Lep_Delete_KlientPNimi').html(klientpnimi);
$('#Lep_Delete_Confirm').off('click').on('click', function () {
$.ajax({
url: 'index.php?route=common/lepingud/lepingud',
type: 'post',
data: 'lepid=' + lepid + '&token= {{ csrf_token }}',
dataType: 'json',
beforeSend: function () {
$('#Lep_Modal_Delete').modal('hide');
$('.loading-overlay').show();
},
success: function (json) {
if (json['success']) {
location.reload();
} else {
alert(json['error']);
}
},
complete: function () {
$('.loading-overlay').hide();
}
});
});
});
</script>
Контроллер:
public function delete()
{
$this->load->language('common/lepingud/lepingud');
$json = array();
if ($this->request->server['REQUEST_METHOD'] == 'POST') {
if (isset($this->request->post['lepid'])) {
$this->load->model('common/lepingud/lepingud');
$this->model_common_lepingud_lepingud->deleteLeping($this->request->post['lepid']);
$json['success'] = true;
} else {
$json['error'] = $this->language->get('error_lepid');
}
} else {
$json['error'] = $this->language->get('error_method');
}
$this->response->addHeader('Content-Type: application/json');
$this->response->setOutput(json_encode($json));
}
Модель(Верхняя часть кода работает и я могу добавить данные в таблицу)
<?php
class Modelcommonlepingudlepingud extends Model
{
public function addDataToDatabase($data)
{
$db = new PDO('mysql:host=' . DB_HOSTNAME . ';dbname=' . DB_DATABASE, DB_USERNAME, DB_PASSWORD);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->query("SET NAMES 'utf8'");
// Connect to the database
try {
$db = new PDO('mysql:host=' . DB_HOSTNAME . ';dbname=' . DB_DATABASE, DB_USERNAME, DB_PASSWORD);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->query("SET NAMES 'utf8'");
} catch (Exception $e) {
// Handle any errors that occur during the connect process
}
// Insert or update the data in the database
$sql = "INSERT INTO " . DB_PREFIX . "leping_kuld (LepNr, Esindus, Paevid, KlientNimi, KlientPNimi, Summa, Pakett, Seisukord, KTahtaeg, Intress, Hoiutasu, Lepingutasu, Nimetus, Seeria, Parool, Kirjeldus, Makseviis, Proov, Kaal, Kategooria, TNimi, TPNimi, Isikukood)
VALUES (:lepnr, :esindus, :paevid, :klientnimi, :klientpnimi, :summa, :pakett, :seisukord, :ktahtaeg, :intress, :hoiutasu, :lepingutasu, :nimetus, :seeria, :parool, :kirjeldus, :makseviis, :proov, :kaal, :kategooria, :tnimi, :tpnimi, :isikukood)
ON DUPLICATE KEY UPDATE Esindus = VALUES(Esindus), Paevid = VALUES(Paevid), KlientNimi = VALUES(KlientNimi), KlientPNimi = VALUES(KlientPNimi), Summa = VALUES(Summa), Pakett = VALUES(Pakett), Seisukord = VALUES(Seisukord), KTahtaeg = VALUES(KTahtaeg), Intress = VALUES(Intress), Hoiutasu = VALUES(Hoiutasu), Lepingutasu = VALUES(Lepingutasu), Nimetus = VALUES(Nimetus), Seeria = VALUES(Seeria), Parool = VALUES(Parool), Kirjeldus = VALUES(Kirjeldus), Makseviis = VALUES(Makseviis), Proov = VALUES(Proov), Kaal = VALUES(Kaal), Kategooria = VALUES(Kategooria), TNimi = VALUES(TNimi), TPNimi = VALUES(TPNimi), Isikukood = VALUES(Isikukood)";
$stmt = $db->prepare($sql);
$stmt->bindParam(':lepnr', $data['LepNr']);
$stmt->bindParam(':esindus', $data['Esindus']);
$stmt->bindParam(':paevid', $data['Paevid']);
$stmt->bindParam(':klientnimi', $data['KlientNimi']);
$stmt->bindParam(':klientpnimi', $data['KlientPNimi']);
$stmt->bindParam(':summa', $data['Summa']);
$stmt->bindParam(':pakett', $data['Pakett']);
$stmt->bindParam(':seisukord', $data['Seisukord']);
$stmt->bindParam(':ktahtaeg', $data['KTahtaeg']);
//$date = DateTime::createFromFormat('d.m.Y', $data['KTahtaeg']);Delete if not use
//$newDate = $date->format('Y-m-d');Delete if not use
//$stmt->bindParam(':ktahtaeg', $newDate);Delete if not use
$stmt->bindParam(':intress', $data['Intress']);
$stmt->bindParam(':hoiutasu', $data['Hoiutasu']);
$stmt->bindParam(':lepingutasu', $data['Lepingutasu']);
$stmt->bindParam(':nimetus', $data['Nimetus']);
$stmt->bindParam(':seeria', $data['Seeria']);
$stmt->bindParam(':parool', $data['Parool']);
$stmt->bindParam(':kirjeldus', $data['Kirjeldus']);
$stmt->bindParam(':makseviis', $data['Makseviis']);
$stmt->bindParam(':proov', $data['Proov']);
$stmt->bindParam(':kaal', $data['Kaal']);
$stmt->bindParam(':kategooria', $data['Kategooria']);
$stmt->bindParam(':tnimi', $data['TNimi']);
$stmt->bindParam(':tpnimi', $data['TPNimi']);
$stmt->bindParam(':isikukood', $data['Isikukood']);
$stmt->execute();
header('Refresh: 0.1');
}
public function deleteLeping($lepid)
{
if (is_numeric($lepid) && isset($this->db)) {
$lepid = (int)$lepid;
$query = $this->db->prepare("DELETE FROM " . DB_PREFIX . "leping_kuld WHERE LepID = ?");
$query->bind_param("i", $lepid);
$query->execute();
}
}
}