The problem is that information from the database is displayed only when I add new data to the database, and then the last new record is not displayed, and when you go to another page of the site, all previous records disappear. Please tell me how can I fix this.
Model File:
public function getLink() {
$result = $this->_db->query("SELECT * FROM link WHERE user_id = '$this->user_id'");
return $result->fetchAll(PDO::FETCH_ASSOC);
}
Controller File:
...
} else {
if (isset($_POST['long_link'])) {
$link = $this->model('LinkShorteningModel');
$link->setData($_POST['long_link'], $_POST['short_name'], $link->getIdUser());
$getLink = $link->getLink();
$isValid = $link->validForm();
if ($isValid == "Верно")
$link->addLink();
else
$data['message'] = $isValid;
$data['link'] = $getLink;
}
}
$this->view('home/index', $data);
Views File:
<form action="/" method="post" class="form-control">
<input type="text" name="long_link" placeholder="Длинная ссылка" value="<?=$_POST['long_link'] ?? null?>"><br>
<input type="text" name="short_name" placeholder="Короткое название"><br>
<div class="error"><?=$data['message'] ?? null?></div>
<button type="submit" class="btn" id="send">Уменьшить</button>
</form>
<?php if(isset($data['link'])): ?>
<h2 class="link_ind">Сокращенные ссылки</h2><br>
<?php for ($i = 0; $i < 5; $i++): ?>
<div class="link_all">
<h3><b>Длинная:</b> <?=$data['link'][$i]['long_link'] ?? null?></h3>
<h3><b>Короткая:</b> </h3>
<button type="submit" class="btn">Удалить</button>
</div>
<?php endfor; ?>
<?php endif;?>