Jump to content
Fórum Script Brasil
  • 0
Sign in to follow this  
rxhxtx

Atualizando dados da pagina ok, mas DB dando pau Ajax-PHP

Question

Boa tarde amigos, 

Estava tentando fazer uma atualização via Ajax no sistema, atualiza os dados da pagina, porem não atualiza no banco de dados,

muito provavel que o erro esteja em alguma coisa na parte de php, mas eu não estou cnseguindo encontrar solução:

INDEX

 <?php  include 'listaUSUcon.php'; ?>
<head></head>
<body>

<div class="container">   
  <table class="table">
    <thead>
      <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>Email</th>
        <th>Action</th>
      </tr>
    </thead>
    <tbody>
      <?php
          $table  = mysqli_query($connection ,'SELECT * FROM users_full');
          while($row  = mysqli_fetch_array($table)){ ?>
              <tr id="<?php echo $row['user_id']; ?>">
                <td data-target="user_fname"><?php echo $row['user_fname']; ?></td>
                <td data-target="user_lname"><?php echo $row['user_lname']; ?></td>
                <td data-target="user_email"><?php echo $row['user_email']; ?></td>
                <td><a href="#" data-role="update" data-id="<?php echo $row['user_id'] ;?>">Update</a></td>
              </tr>
         <?php }
       ?>
       
    </tbody>
  </table>

  
</div>

    <!-- Modal -->
    <div id="myModal" class="modal fade" role="dialog">
      <div class="modal-dialog">

        <!-- Modal content-->
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">&times;</button>
                <input type="text" id="user_id" class="form-control" readonly>
          </div>
          <div class="modal-body">
              <div class="form-group">
                <label>First Name</label>
                <input type="text" id="user_fname" class="form-control">
              </div>
              <div class="form-group">
                <label>Last Name</label>
                <input type="text" id="user_lname" class="form-control">
              </div>

               <div class="form-group">
                <label>Email</label>
                <input type="text" id="user_email" class="form-control">
              </div>
                <input type="hidden" id="user_id" class="form-control">


          </div>
          <div class="modal-footer">
            <a href="#" id="save" class="btn btn-primary pull-right save">Update</a>
            <button type="button" class="btn btn-default pull-left" data-dismiss="modal">Close</button>
          </div>
        </div>

      </div>
    </div>

</body>

<script>
  $(document).ready(function(){

    //  append values in input fields
      $(document).on('click','a[data-role=update]',function(){
            var user_id  = $(this).data('id');
            var user_fname  = $('#'+user_id).children('td[data-target=user_fname]').text();
            var user_lname  = $('#'+user_id).children('td[data-target=user_lname]').text();
            var user_email  = $('#'+user_id).children('td[data-target=user_email]').text();

            $('#user_fname').val(user_fname);
            $('#user_lname').val(user_lname);
            $('#user_email').val(user_email);
            $('#user_id').val(user_id);
            $('#myModal').modal('toggle');
      });

      // now create event to get data from fields and update in database 

       $('#save').click(function(){
          var user_id  = $('#user_id').val(); 
         var user_fname =  $('#user_fname').val();
          var user_lname =  $('#user_lname').val();
          var user_email =   $('#user_email').val();

          $.ajax({
              url      : 'listaUSUcon.php',
              method   : 'post', 
              data     : {user_fname : user_fname , user_lname: user_lname , user_email : user_email , user_id: user_id},
              success  : function(response){
                            // now update user record in table 
                             $('#'+user_id).children('td[data-target=user_fname]').text(user_fname);
                             $('#'+user_id).children('td[data-target=user_lname]').text(user_lname);
                             $('#'+user_id).children('td[data-target=user_email]').text(user_email);
                             $('#myModal').modal('toggle'); 

                         }
          });
       });
  });
</script>

nesta parte esta pegando os dados do banco de dados e exibindo na pagina,
está puxando os dados corretos para o Modal.
Quando clica em editar, esta editando na página corretamente, mas não atualiza no banco de dados,

se atualizar a pagina voltam os dados originais.

 

listaUSUcon.php

<?php
$connection =	mysqli_connect('localhost' , 'root' ,'' ,'testes');




if(isset($_POST['user_id'])){
	
	$user_fname = $_POST['user_fname'];
	$user_lname = $_POST['user_lname'];
	$user_email = $_POST['user_email'];
	$user_id = $_POST['user_id'];

	//  query to update data 
	 
	$result  = mysqli_query($connection , "UPDATE `users_full` SET `user_fname`=`$user_fname` , `user_lname`=`$user_lname` , `user_email`= `$user_email` WHERE `user_id`=`$user_id`");
	if($result){
		echo 'data updated';
	}

}
?>

fiz uns teste retirando o if POST e alterando os dados para "valor" e dai atualiza,
mas usando esse AJAX não funciona de maneira alguma...

 

 

alguém consegue me dar uma luz?

 

 

Obrigado!!

 

Share this post


Link to post
Share on other sites

1 answer to this question

Recommended Posts

  • 0

No arquivo listaUSUcon.php você está suando crase em volta das variáveis

UPDATE `users_full` SET `user_fname`=`$user_fname` , `user_lname`=`$user_lname` , `user_email`= `$user_email` WHERE `user_id`=`$user_id`

Use aspas

UPDATE `users_full` SET `user_fname`='$user_fname' , `user_lname`='$user_lname' , `user_email`= '$user_email' WHERE `user_id`='$user_id'

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

Cloud Computing


  • Forum Statistics

    • Total Topics
      148417
    • Total Posts
      643832
×
×
  • Create New...