Update with Ajax + Pdo

Asked

Viewed 239 times

0

I performed the UPDATE with AJAX + Pdo, however, it is affecting only one line, the others are not affected. I’ve tried a lot of things, but I can’t seem to figure it out, if anyone can help me.

				
							
function uptade_cat() {
    
    var valor_update=$("#valor_update").val();
    var id_cat_update=$("#id_cat_update").val();
    var nome_categoria_update=$("#nome_categoria_update").val();
 

        $.ajax({
            type: "POST",
            url: "caixa/categoria_uptade.php",
            data: {nome_categoria:nome_categoria_update, valor:valor_update, id_cat:id_cat_update},
            dataType: "JSON",
            success: function (data) {
				$('#valor_update').val(data.valor_uptade);
				$('#nome_categoria_update').val(data.nome_categoria);
				$('#id_cat_update').val(data.id_cat_update);
			 $('#alert_message3').html('<div class="alert alert-success alert-dismissible fade show" role="alert"><strong>Sucesso!</strong> Categoria Atualizado.<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button></div>');
			$('#categoria_uptade_form').reload(); 

				
            },
            error: function() {
				$('#erro').html('<div class="alert alert-danger alert-dismissible fade show" role="alert"><strong>Erro!</strong> Ocorreu algum problema ao atualizar categoria.<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button></div>');
							
           
            },
        });
  }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>

<div class="col-lg-12">

 	<div class="card-header">
                                <strong class="card-title">Editar Categoria</strong>
                            </div>
			<div class="card-body">
            <div class="table">
              <table class="table table-striped" style="width:100%">
                <thead>
                  <tr>
                    <th class="td-actions">Registro</th>
                    <th class="td-actions">Categoria</th>
                    <th class="td-actions">Valor</th>
                    <th class="td-actions">Editar</th>
                  </tr>
                </thead>
                <tbody>
<?PHP
$select = "SELECT * from caixa_categoria ORDER BY id_cat DESC ";

		
		try{
			$result = $conexao->prepare($select);			
			$result->execute();
			$contar = $result->rowCount();
			if($contar>0){
				while($mostra = $result->FETCH(PDO::FETCH_OBJ)){
?>           
                  <tr>
					<td> <?php echo $mostra->nome_categoria;?></td>
                    <td> <?php echo $mostra->categoria;?></td>
                    <td> <?php echo $mostra->valor;?> </td>
                    <td class="td-actions text-center"> <a href="" class="btn btn-outline-warning" data-toggle="modal" data-target="#cotegoria_<?php echo $mostra->id_cat;?>"><i class="far fa-edit"></i> Editar</td>
				</tr>
					
<?php
}		
			
			}else{
				echo '<div class"alert alert-danger">
                      <button type="button" class="close" data-dismiss="alert">×</button>
                      <strong>Aviso!</strong> Não há post cadastrado em nosso banco de dados.
                </div>';
			}
			
		}catch(PDOException $e){
			echo $e;
		}
					
		?>			
					           
                  
                
                </tbody>
              </table>
					</div>
				</div>
			</div>


<!-- Modal -->
<?php 

			$result = $conexao->prepare('SELECT * from caixa_categoria');		
			$result->execute();
				while($mostra = $result->FETCH(PDO::FETCH_ASSOC)):
		
?>
<div class="modal fade" id="cotegoria_<?php echo $mostra['id_cat'] ?>" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Categoria: <?php echo $mostra['nome_categoria'] ?> (<?php echo $mostra['categoria'] ?>)</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
		  <form id="categoria_uptade_form" name="categoria_uptade_form">
			  <div class="form-row">
				  <input type="hidden" name="id_cat_update" id="id_cat_update" value="<?php echo $mostra['id_cat'] ?>" > 
				<div class="form-group col-md-6">
				  <label for="inputEmail4">Nome Categoria</label>
				  <input type="text" class="form-control" name="nome_categoria_update" id="nome_categoria_update" value="<?php echo $mostra['nome_categoria'] ?>">
				</div>
				<div class="form-group col-md-6">
				  <label for="inputEmail4">Valor</label>
				  <input type="text" class="form-control" name="valor_update" id="valor_update" value="<?php echo $mostra['valor'] ?>">
				</div>

		</div>
</form>
		 
        <div id="alert_message3"></div>
        <div id="erro"></div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
		<button type="button" class="btn btn-success" onclick="uptade_cat()">Atualizar</button>

      </div>
    </div>
  </div>
</div>
<?php endwhile; ?>

categoria_uptade

 <?php 
    include ('../../conexao/conecta.php');

    $id_cat= $_POST['id_cat'];
    $nome_categoria= $_POST['nome_categoria'];
    $valor= $_POST['valor'];

    $stmt = $conexao->prepare("UPDATE caixa_categoria SET nome_categoria=:nome_categoria, valor=:valor WHERE id_cat=:id_cat");

    $stmt->bindparam(':id_cat', $id_cat);
    $stmt->bindparam(':nome_categoria', $nome_categoria);
    $stmt->bindparam(':valor', $valor);


    if($stmt->execute())
    {
      $res="Data Inserted Successfully:";
      echo json_encode($res);
    }
    else {
      $error="Not Inserted,Some Probelm occur.";
      echo json_encode($error);
    }

    ?>
  • what is the doubt? explains better

  • this must be because there must be some comma that is not closed or this in the wrong place looks at each field and checks to find the error faster uses the browser’s network tab and see if all data is being passed

  • @Weessmith in update only one ID I can update, the rest I can’t update.

  • @diogoDsa I’ll do it ! Thank you

  • you want to update several ids at once?

  • @Weessmith don’t want to update just one!

Show 1 more comment
No answers

Browser other questions tagged

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