Help with PHP Edit PDO

0

I'm pulling data from a table in sqlsrv to a table html, because I need to frequently edit this data in that table. So I created a php function edit to edit the table row in another page only that is not working does not return me nor one value. Here's code I've done so far:

Page to edit:

<?php
require_once('../js/javascript.html');
require_once('../css/cssprog.html');
require_once('../foundation/linkreal.html');

include('../connection_open.php');

include_once ('../controller/progControle.php');
include_once ('../model/Prog.php');
include_once ('../DAO/progDAO.php');

$controller = new Comando($conn);

$id = "";
$NrPlaca = "";
$DsMotorista = "";

if(!empty($_GET['id'])){
    $id = $_GET['id'];

    $results = $controller->listar($id);

    $NrPlaca = $results->getplaca();
    $DsMotorista = $results->getmot();

}

?>
<html>
<body>
    <form action="../controller/progPrecontrole.php" method="POST" onsubmit="return valid();">
        <div class="row">
            <div class="large- 12 medium-12 small-12 columns">
                <br>
                    <input type="hidden" name="id" value="<?php echo $id; ?>"/>
                <br>
                <p>Placa:</p><input type="text" id="placa" name="placa" value="<?php echo $NrPlaca; ?>"/>
                <p>Motorista:</p><input type="text" id="mot" name="mot" value="<?php echo $DsMotorista; ?>"/>
            </div>
            <div class="small-12 columns">
                <input type="submit" class="alert button"/>
                <a class="secondary button" href="programacao.php"> Voltar </a>
            </div>
        </div>
    </form>
</body>

Table where you receive a select of the database data:

<?php

require_once('../js/javascript.html');
require_once('../css/cssprog.html');
require_once('../foundation/linkreal.html');

include('../connection_open.php');

include_once ('../controller/progControle.php');
include_once ('../model/Prog.php');
include_once ('../DAO/progDAO.php');


$controller = new Comando($conn);

?>

<html>
    <head>
        <title>Programação</title>
    </head>
    <body>
        <!--Top Bar-->
        <div class="fixed">
            <nav class="top-bar" data-topbar="">
                <ul class="title-area">
                    <li class="name">
                      <a href="Home.php"><img src="../Img/log.jpg" ></a>
                    </li>
                    <li class="toggle-topbar menu-icon"><a href="#"><span></span></a></li>
                </ul>
                <section class="top-bar-section">
                    <ul class="left">
                        <li class="has-dropdown">
                            <a>LOGÍSTICA</a>
                            <ul class="dropdown">
                                <li><a href="programacao.php">Programação</a></li>
                                <li ><a href="#">Status de viajem</a></li>
                            </ul>
                        </li>
                        <li ><a href="procedimentos.php">Procedimentos</a></li>
                        <li><a href="contatos.php">Contatos</a></li>
                    </ul>
                    <ul class="right">
                        <li class="active"><a href="../logout.php">Sair</a></li>
                    </ul>
                </section>
            </nav>
        </div>
        <!--Tabela-->
        <br><br><br>
        <form method="POST" action="../controller/progPrecontrole.php">
        <div class="large-12 columns">
            <table class="borda">
                <tr>
                    <th> </th>
                    <th class="t">BITRUCK</th>
                    <th class="t">Motorista</th>
                    <th class="t">Data Saída</th>
                    <th class="t">Origem</th>
                    <th class="t">Destino</th>
                    <th class="t">Prev. Cheg. Dest</th>
                    <th class="t">Carga/Manifesto</th>
                    <th class="t">Adiantamento Fincanceiro</th>
                    <th class="t">Agendas</th>
                    <th class="t">Malote</th>
                    <th>Obs</th>

                </tr>
                <?php
                    foreach ($controller->ListaPorTipoB() as $objProg) {
                ?>
                    <tr>
                        <td> <a href="edita.php?id=<?php echo $objProg->getid();?>"> <p> Editar </p> </a> </td>
                        <td ><p><?php echo $objProg->getplaca(); ?></p></td>
                        <td ><p><?php echo $objProg->getmot(); ?></p></td>
                        <td><p></p></td>
                        <td><p></p></td>
                        <td><p></p></td>
                        <td><p></p></td>
                        <td><p></p></td>
                        <td><p></p></td>
                        <td><p></p></td>
                        <td><p></p></td>
                        <td><div>Set in the year 0 F.E. ("Foundation Era"), The Psychohistorians opens on Trantor, the capital of the 12,000-year-old Galactic Empire. Though the empire appears stable and powerful, it is slowly decaying in ways that parallel the decline of the Western Roman Empire.</div></td>
                <?php
                    }
                ?>
                </table>
            </div>
?>

Function edit in DAO:

class ProgDAO{

private $conn;

public function __construct($connection) {
    $this->conn = $connection;
}



public function ListaPorTipoB($tipo){
    $results = array();
    $stmt = $this->conn->prepare('SELECT * FROM GTCLogist WHERE DsTpVeiculo = ?');
    $stmt->execute(array($tipo));
        if($stmt) {
            while($row = $stmt->fetch(PDO::FETCH_OBJ)) {
                $prog = new Prog();
                $prog->setid($row->id);
                $prog->setplaca($row->NrPlaca);
                $prog->setmot(stripslashes($row->DsMotorista));
                $results[] = $prog;
            }
        }
        return $results;
}

public function editar(Prog $prog){
    $this->conn->beginTransaction();
    try {
        $stmt = $this->conn->prepare(
            'UPDATE GTCLogist SET NrPlaca = :NrPlaca, DsMotorista = :DsMotorista WHERE id = :id'
        );
        $stmt->bindValue(':id', $prog->getid(), PDO::PARAM_INT);
        $stmt->bindValue(':NrPlaca', $prog->getplaca(), PDO::PARAM_STR);
        $stmt->bindValue(':DsMotorista', $prog->getmot(), PDO::PARAM_STR);
        $stmt->execute();
        $this->conn->commit();
    }
    catch(Exception $e) {
        $this->conn->rollback();
    }
}

My precontrol:

<?php

include_once ('../connection_open.php');

include_once ('../model/prog.php');
include_once ('progControle.php');
include_once ('../DAO/progDAO.php');


        $id = $_POST['id'];
        $NrPlaca = $_POST['placa'];
        $DsMotorista = $_POST['mot'];

        $objProg = new Prog();
        $objProg->setid($id);
        $objProg->setplaca($NrPlaca);
        $objProg->setmot($DsMotorista);


            $controller = new Comando($conn);


        if ($id == ""){
            $objProg->setid($id);
            $controller->editar($objProg);
        }

        header ("location: ../view/edita.php?id=".$id);



include_once ('../connection_close.php');

?>

My control:

<?php

class Comando{

    private $conn;

        public function __construct($connec) {
            $this->conn = $connec;
        }

        public function ListaPorTipoB(){
            $dao = new ProgDAO($this->conn);
            return $dao -> ListaPorTipoB('Bitruck');
        }

        public function listar($id){
            $dao = new ProgDAO($this->conn);
            return $dao -> listar($id);
        }

        public function editar(Prog $objProg){
            $dao = new ProgDAO($this->conn);
            return $dao -> editar($objProg);
        }
}

?>

The problem is that in the page inputs edit is not returning a value within the value, and in the function ListTypeB in line $prog->setid($row->id); is giving the following error Undefined property: stdClass :: $ id

    
asked by anonymous 11.01.2016 / 14:59

1 answer

1

So the error in the above code comes in two places.

In DAO inside the function ListBypeB, I was simply getting the wrong row name in the database. The right thing is like this:

$prog->setid($row->ID);

And the other error was in the if in precontrol, you were editing a blank id, you just had to change the if to edit when there was an id empty).

if (!empty($id)){
    $objProg->setid($id);
    $controller->editar($objProg);
}
    
12.01.2016 / 15:19