Error while deleting with PHP using PDO class and object orientation

Asked

Viewed 301 times

0

I’m making a delete on my form manut_usuario, below is my method to delete which is in class usuario.class.php;

public function DelUsu($id){
        try{
        $delUsu = "DELETE FROM usuario WHERE idu_usujport = :id ";
        $delUsu = $this->con->Connect()->prepare($delUsu);
        $delUsu->bindValue(':id', $id, PDO::PARAM_INT );
        $delUsu->execute();
        $delUsu->fetch(PDO::FETCH_OBJ);
        return $delUsu;
        echo 'deletado';
        }
       catch(PDOException $erro_5){
        echo 'erro'.$erro_5->getMessage();
        }
    }//Metodo Delete dados.

In my form below is the way I delete data passing to the method DelUsu

<?php       //crio um array para recuperar os dados e mostrar no formulario.

             $objeto = new Usuario();
             $usuario = $objeto->LerDadosUsu();
             foreach($usuario as $usuario):
             ?>
             <tr>
             <td><?php print $usuario['nom_usujauport'] ?></td>
             <td><?php print $usuario['ema_usujauport'] ?></td>
             <td><?php print $usuario['sen_usujauport'] ?></td>
             <!--FINAL DO ARRAY(RECUPERANDO SEM ERROS)-->             


              <!--DELETAR-->
              <?php
              $usuario =  new Usuario();   
              $id="";    //zero a variável ID para pegar sempre a selecionada pelo administrador   
              if(isset($_POST['excl_usu'])){
              $usuario->DelUsu($id);             
              echo "Excluido";
              }else{
              echo "Não Excluido!!";
              }

              ?>

While running the program and delete by clicking the button <"excl_usu"> gives error below

erroSQLSTATE[HY000]: General errorExcluded

Below follows my form where I finish as the foreach.

    <?php
         $objeto = new Usuario();
         $usuario = $objeto->LerDadosUsu();
         foreach($usuario as $usuario):
         ?>
         <tr>
         <td><?php print $usuario['nom_usujauport'] ?></td>
         <td><?php print $usuario['ema_usujauport'] ?></td>
         <td><?php print $usuario['sen_usujauport'] ?></td>
         <!--FUNÇÕES PARA CARREGAR DADOS NAS ALTERAÇÕES-->             


          <!--DELETAR-->
          <?php
          $usuario =  new Usuario();   
          $id="";       
          if(isset($_POST['excl_usu'])){
          $usuario->DelUsu($id);             
          echo "Excluido";
          }else{
          echo "Não Excluido!!";
          }

          ?>
          <!--FINAL DELETAR-->
          <!--ALTERAR-->
          <?php


          ?> 
         <!--FINAL ALTERAR-->


<td><p data-toggle="tooltip" title="Edit">


         <button type="submit" class="btn btn-primary" data-title="Editar"  data-toggle="modal" data-target="#edit"  nome="alterar" id="alterar"><i class="fa fa-edit">&nbsp;&nbsp;Alterar</i></button></p></td>

    <td><data-toggle="tooltip" title="Delete">
         <button type="submit" class="btn btn-primary " nome="excl_usu" id="excl_usu"><i class="fa fa-trash-o">&nbsp;&nbsp;Excluir</i></button></p></td>
    </tr>

    <tr>
    </tbody>

</table>
             <?php
             endforeach;
             ?>
             </form>
<div class="clearfix" align="center"></div>
<ul class="pagination pull-right">
  <li class="disabled"><a href="#"><span class="glyphicon glyphicon-chevron-left"></span></a></li>
  <li class="active"><a href="#">1</a></li>
  <li><a href="#">2</a></li>
  <li><a href="#">3</a></li>
  <li><a href="#">4</a></li>
  <li><a href="#">5</a></li>
  <li><a href="#"><span class="glyphicon glyphicon-chevron-right"></span></a></li>
</ul>

            </div>

        </div>
    </div>
</div>
<div class="modal fade" id="edit" tabindex="-1"  role="dialog" aria-labelledby="edit" aria-hidden="true">
      <div class="modal-dialog">
    <div class="modal-content">
          <div class="modal-header">
          <form action="" id="frm_atu_usu" name="frm_atu_usu" method="POST" >
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></button>
        <h4 class="modal-title custom_align" id="Heading">Editar Usuario</h4>
        </div>
        <div class="modal-body">
        <div class="form-group">
        <input class="form-control " type="text" placeholder="Nome:">
        </div>
        <div class="form-group">

        <input class="form-control " type="text" placeholder="Email:">
        </div>
        <div class="form-group">
        <textarea rows="2" class="form-control" placeholder="Senha:"></textarea>
        </div>
        </div>
          <div class="modal-footer ">
          <button type="button" class="btn btn-sucessful btn-lg"><span class="glyphicon glyphicon-ok-sign"></span> Atualizar</button>
         </div></form>
        </div>

       </div>
      </div>
  • Where does your foreach? Are you doing the deletion inside the loop? This would not delete the same record multiple times?

  • Anderson Carlos Woss, follow the code to show where the end of the foreach is... my end of the foreach ends after a modal window from which I update...

  • 1

    Your HTML table is completely unstructured. Please review your code. It makes no sense to have </tbody> and </table> inside the loop and there’s a <tr> remaining in the line before the </tbody>. Further study the answer given in another question from you and understand how it’s done.

1 answer

2


You are right to call the object connection to the database ?

$delUsu = $this->con->Connect()->prepare($delUsu);

Removes these lines from their function is not used for DELETE fetch method

$delUsu->fetch(PDO::FETCH_OBJ); 
return $delUsu;

Add that:

echo 'deletado'.$delUsu->rowCount(); //vai retornar a quantidade de registro deletado
  • Harry good morning! , that’s right. The line where I make the right call but where the Return was right was where the problem was after removing it, it was right.... as I needed. Thank you

  • @Rodrigozanetti for nothing :).

Browser other questions tagged

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