Edit does not edit PHP

Asked

Viewed 120 times

1

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');

?>

Notcontrol

<?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 on input "DsMsg" on the page editNot is not changing in the bank and keeps returning me the same thing on echo on the page home within the foreach. I have not identified the error yet. It seems that the edit function in DAO is not working, but the others work perfectly. Some help ?

  • Debug, print the query that was executed to save the value. The query will show where the problem is, if the correct value is coming.

  • I gave one print_r($objNot); in Notprecontrole and returns to me what I edited in input DsMsg, Until there arrives because the bank does not edit and so does not appear in echo $objNot->getmsg(); in the home already edited.

  • Update condition is being met ?

  • It doesn’t work, the input data reaches even in the precontrol after then nothing happens.

  • Solve the notices and do new tests.

  • Now I returned these errors in the precontrol: Notice: Undefined index: InAtivo in C:\xampp\htdocs\controller\NotPrecontrole.php on line 9 Notice: Undefined index: DtInicio in C:\xampp\htdocs\controller\NotPrecontrole.php on line 10 Notice: Undefined index: DtFim in C:\xampp\htdocs\controller\NotPrecontrole.php on line 11

  • Okay, I got it, but it’s still the same.

  • Print the query and show it to us as an example here.

  • Which query do you need ? I don’t understand. What kind of information do you want to help me ?

  • I would like to see the sql query you used to save the edited records.

  • Only sql query that use is that ai of the edit function is this you need ?

Show 6 more comments

2 answers

1


I solved the problem, in my database the column Inactive is a Primary key Not Null.

So I wasn’t worth one to her at UPDATE.

I created another input in the editNot I passed the value by input and edited everything.

<input type="text" name="InAtivo" value="<?php echo $InAtivo; ?>"/>

0

Where Hidden field is, try not to use it, I’ve always found it a little problematic. When I have to pass this type of 'id' value, or I pass a button:

<button type="submit" name="meu_botao" value="minha_id_variavel">editar</button>

or via url in the form call:

<form action="pagina.php?id='sua_id_variavel'" method="post">

ai in the second case, in php you receive so:

$_GET['id'];

Particularly I prefer the second case, via url, because you have a url for each page php generate, it is easier for some search engine to make references to your pages separately ;)

I hope to have helped the//

Browser other questions tagged

You are not signed in. Login or sign up in order to post.