Help with Editing PHP PDO

Asked

Viewed 261 times

0

I’m pulling data from a table in sqlsrv to an html table because I often need to edit this data in that table. So I created a php edit function to edit the table row on another page only that is not working does not return me value nor one. Follow the code of what 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 from 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 edits 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 inputs of the edits page is not returning nor a value within the value, and in the Listaportipob Function in the line $prog->setid($row->id); is making the following mistake Undefined Property: stdClass::$id

  • What exactly is the problem? I’m a bot I can’t read source code from an image. Use the button { } to format the source code.

  • The problem is that the edit is not working it does not pull or return table values. And I switched to code, no more images.

  • Your code seems right, the only detail $results is an array you do not access it directly, for test try to access so, put this code in the value of the input, <?php echo $results[0]->getplaca(); ?> in doubt see the generated html code, use Ctrl+u to view.

  • I used the U control and an error appears: <input type="text" id="placa" name="placa" value="<br /> <b>Notice</b>: Undefined variable: results in <b>C:\xampp\htdocs\Rasador2\view\edita.php</b> on line <b>37</b><br />&#xA;<br />&#xA;<b>Fatal error</b>: Call to a member function getplaca() on null in <b>C:\xampp\htdocs\Rasador2\view\edita.php</b> on line <b>37</b><br />

  • Right now with the line $prog->setid($row->id); in Function Listaportipob on DAO is giving the following error: Notice: Undefined Property: stdClass::$id in C: xampp htdocs Rasador2 DAO progDAO.php on line 19 So far I haven’t figured out what it might be.

  • Of the one print_r($results); and another in $row

  • I tried already, from Undefined variable

  • It seems that the name of the field is not id, $Row print_r returned what? it must be within the method ListaPorTipoB()

  • My mistake, the $Results print_r did return the array’s. And $Row also.

  • Put the right keys now?

  • 1

    I did, now you did. Character error, I put the minuscule id being the capital ID in the bank rs, I noticed when returned in the print_r of $Row. Only that there is still a problem when I modify in the edit and save it not saved with the data I edited.

  • 1

    Comment on the header('...'); you must edit when there is a $Id and not when he’s white me. Look => if ($id == ""){ $objProg->setid($id); $controller->editar($objProg);}

  • OK I didn’t realize, now I tidied up and stayed like this if (!empty($id)){&#xA; &#xA; $objProg->setid($id);&#xA; $controller->editar($objProg);&#xA; }

  • Is there any other problem?

  • Not only that, you can mark as your answer that helped. Thank you.

  • Can create an answer, you have more details than solved the problem :)

Show 11 more comments

1 answer

1


So, the error in the above code posted appears in two places.

On the DAO inside the Function Listaportipob, I was simply taking the wrong name of Row in the database. The right one was like this:

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

And the other mistake was in if no precontrol, was editing a blank id, just change the if for editing when there is an id ( when it is not empty).

if (!empty($id)){
    $objProg->setid($id);
    $controller->editar($objProg);
}

Browser other questions tagged

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