Code:
EditNot
<?php
include('../connection_open.php');
include_once ('../controller/NotControle.php');
include_once ('../model/Not.php');
include_once ('../DAO/NotDAO.php');
$controller = new Notifica($conn);
$id = "";
$InAtivo = "";
$DtInicio = "";
$DtFim = "";
$DsMsg = "";
if(!empty($_GET['id'])){
$id = $_GET['id'];
$results = $controller->listar($id);
$InAtivo = $results->getativo();
$DtInicio = $results->getini();
$DtFim = $results->getfim();
$DsMsg = $results->getmsg();
}
?>
<html>
<body>
<form action="../controller/NotPrecontrole.php" method="POST" onsubmit="return valid();">
<div class="row">
<input type="hidden" name="id" value="<?php echo $id; ?>"/>
<input type="text" name="DsMsg" value="<?php echo $DsMsg; ?>"/>
<input type="submit" class="round success small button" value="Aplicar"/>
<a class="round secondary small button" href="Home.php">Voltar</a>
</div>
</form>
</body>
</html>
NotPrecontrole
<?php
include_once ('../connection_open.php');
include_once ('../model/Not.php');
include_once ('NotControle.php');
include_once ('../DAO/NotDAO.php');
$id = $_POST['id'];
$InAtivo = $_POST['InAtivo'];
$DtInicio = $_POST['DtInicio'];
$DtFim = $_POST['DtFim'];
$DsMsg = $_POST['DsMsg'];
$objNot = new Not();
$objNot->setid($id);
$objNot->setativo($InAtivo);
$objNot->setini($DtInicio);
$objNot->setfim($DtFim);
$objNot->setmsg($DsMsg);
$controller = new Notifica($conn);
if (!empty($id)){
$objNot->setid($id);
$controller->editar($objNot);
}
header ("location: ../view/home.php?id=".$id);
include_once ('../connection_close.php');
?>
NotControle
<?php
class Notifica{
private $conn;
public function __construct($connec){
$this->conn = $connec;
}
public function editar(Not $objNot){
$dao = new NotDAO($this->conn);
return $dao -> editar($objNot);
}
public function listar($id){
$dao = new NotDAO($this->conn);
return $dao -> listar($id);
}
public function ListaNot(){
$dao = new NotDAO($this->conn);
return $dao -> ListaNot();
}
}
?>
NotDAO
<?php
class NotDAO{
private $conn;
public function __construct($connection) {
$this->conn = $connection;
}
public function ListaNot(){
$results = array();
$stmt = $this->conn->prepare('SELECT * FROM esp35399');
$stmt->execute();
if($stmt) {
while($row = $stmt->fetch(PDO::FETCH_OBJ)) {
$not = new Not();
$not->setid($row->ID);
$not->setativo($row->InAtivo);
$not->setini($row->InAtivo);
$not->setfim($row->DtFim);
$not->setmsg($row->DsMsg);
$results[] = $not;
}
}
return $results;
}
public function editar(Not $not){
$this->conn->beginTransaction();
try {
$stmt = $this->conn->prepare(
'UPDATE esp35399 SET DsMsg = :DsMsg, InAtivo = :InAtivo, DtInicio = :DtInicio, DtFim = :DtFim WHERE ID = :ID'
);
$stmt->bindValue(':ID', $not->getid(), PDO::PARAM_INT);
$stmt->bindValue(':InAtivo', $not->getativo(), PDO::PARAM_INT);
$stmt->bindValue(':DtInicio', $not->getini(), PDO::PARAM_INT);
$stmt->bindValue(':DtFim', $not->getfim(), PDO::PARAM_INT);
$stmt->bindValue(':DsMsg', $not->getmsg(), PDO::PARAM_STR);
$stmt->execute();
$this->conn->commit();
}
catch(Exception $e) {
$this->conn->rollback();
}
}
public function listar($id){
$not = new Not();
$stmt = $this->conn->prepare(
'SELECT * FROM esp35399 WHERE ID = :ID'
);
$stmt->bindValue(':ID', $id, PDO::PARAM_INT);
$stmt->execute();
if($stmt) {
while ($row = $stmt->fetch(PDO::FETCH_OBJ)) {
$not->setid($row->ID);
$not->setativo($row->InAtivo);
$not->setini($row->DtInicio);
$not->setfim($row->DtFim);
$not->setmsg($row->DsMsg);
}
}
return $not;
}
}
?>
Not
class Not{
private $id;
private $InAtivo;
private $DtInicio;
private $DtFim;
private $DsMsg;
public function setid($id){
$this->id = $id;
}
public function getid(){
return $this->id;
}
public function setativo($InAtivo){
$this->InAtivo = $InAtivo;
}
public function getativo(){
return $this->InAtivo;
}
public function setini($DtInicio){
$this->DtInicio = $DtInicio;
}
public function getini(){
return $this->DtInicio;
}
public function setfim($DtFim){
$this->DtFim = $DtFim;
}
public function getfim(){
return $this->DtFim;
}
public function setmsg($DsMsg){
$this->DsMsg = $DsMsg;
}
public function getmsg(){
return $this->DsMsg;
}
Home
foreach ($controller->ListaNot() as $objNot){
<p><?php echo $objNot->getmsg(); ?></p>
echo '<a href="editNot.php?id=' . $objNot->getid() . '">Editar</a>';
}
The problem is that when editing something in input "DsMsg"
on the editNot page you are not changing in the database and keep returning the same thing to echo
on the home page within foreach
. I have not identified the error yet. It looks like the edit function in DAO is not working, but the rest works perfectly. Any help?