Only save ID 0

Asked

Viewed 79 times

0

I’m having a problem that I’ve tried many ways, but is only recording Zero id.

Part of the Button to register the street in the neighborhood

Listing of neighborhoods

bairro_lista.php

Part of the Button to register the street in the neighborhood Listing of neighborhoods

<?php
while ($linha = mysql_fetch_array($resultado)) { ?>
<tr>
	<td><?php echo $linha['nome']; ?></td>
	<td class="actions " style="width: 38.66666667%;">
		<button type="button" name="addRua" id="<?php $linha['id']; ?>" data-toggle="modal" data-target="#addRua_data_Modal_rua" class="btn btn-warning btn-xs id_cod_bairro" >Cadastrar Rua</button>
	</td>
	
<?php } ?>  	

Part of Modal where arrow the values to be recorded

<div id="addRua_data_Modal_rua" class="modal fade" data-backdrop="static">  
  <div class="modal-dialog">  
	   <div class="modal-content">  
			<div class="modal-header">  
				 <!--<button name="fecharRua" id="fecharRua" type="submit" class="close" data-dismiss="modal">&times;</button>  -->
				 <input type="submit" name="fecharRua" id="fecharRua" value="&times;" class="close" data-dismiss="modal" />
				 <!--<h4 class="modal-title" name="insert" id="insert" value="Adicionar" >Adicionar / Atualizar</h4>-->
				 <label type="text" name="insert_title" id="insert_title" value="Cadastrar Bairro" class="modal-title" />	 
			</div>  
			<div class="modal-body">  
				 <form method="post" id="insert_form_rua">  
					   <select name="tiporua" id="tiporua" class="form-control">  
						   <option value="Rua">Rua</option>  
						   <option value="Travessa">Travessa</option>  
						   <option value="Avenida">Avenida</option>
					  </select>  
				  
					  <br /> 
					  <label>Digite o nome da Rua</label>						  
					  <input type="text" name="nome_rua" id="nome_rua" class="form-control" />  					
					  <br />  
					  <input type="hidden" name="employee_id" id="employee_id" /> 
					  <input type="submit" name="insert" id="insert" value="Insert" class="btn btn-success" />
				 </form> 
				 
			</div>  
			<div class="modal-footer">  
				 <input type="submit" name="fecharRua02" id="fecharRua02" value="Sair" class="btn btn-default" data-dismiss="modal" />
			</div>  
	   </div>  
  </div>  
</div>  

Part of javascript where directs to write

$(document).ready(function(){  
 $('#addRua').click(function(){  
 var id_cod_bairro = $(this).attr("id");
	   $('#insert').val("Cadastrar");  
	   $('#insert_title').val("Cadastrar Rua");
	   

  }); 
  $('#insert_form_rua').on("submit", function(event){  
	   event.preventDefault();  
	   if($('#nome_rua').val() == "")  
	   {  
			alert("nome is required");  
	   }    
	   else  
	   {  
			$.ajax({  
				 url:"insert_bairro_rua.php?id_usuario=<?php echo $_GET["id_usuario"]; ?>&cod_bairro=id_cod_bairro",  
				 method:"POST",  
				 data:{employee_id:employee_id},  
				 data:$('#insert_form_rua').serialize(), 				 
				 beforeSend:function(){  
					  $('#insert').val("Inserindo Dados..."); 
					  $('#insert_form_rua')[0].reset();
					  
					  
				 },  
				 success:function(data){  
					  $('#addRua_data_Modal_rua').modal('hide');  
					  $('#employee_table').html(data);
					  $('#insert_form_rua')[0].reset();						  
				 }  
			}); 
			
	   }  
  });  

insert_bairro_rua.php

Where to save data to Mysql

 <?php  
 include("/lib/conexao.php");
 if(isset($_POST["employee_id"]))  
 {  
	  $id_usuario = $_GET["id_usuario"]; 
	  $cod_bairro = $_GET["cod_bairro"]; 
	  $nome_rua = filter_input(INPUT_POST, 'nome_rua', FILTER_SANITIZE_SPECIAL_CHARS);
	  $tiporua = filter_input(INPUT_POST, 'tiporua', FILTER_SANITIZE_SPECIAL_CHARS);
	  
	  
      if($_POST["employee_id"] != '')  
      {  
		$query = "  
			   UPDATE rua   
			   SET   
			   nome='$nome_rua',   
			   tiporua='$tiporua',
			   cod_bairro='$cod_bairro',
			   id_usuario='$id_usuario'
			   WHERE id='".$_POST["employee_id"]."'"
			   ;	
		mysql_query($query,$conexao);	
      }  
      else  
      {  
		$query = "  
			   INSERT INTO rua(nome, tiporua, cod_bairro, id_usuario)  
			   VALUES('$nome_rua', '$tiporua','$cod_bairro','$id_usuario');
			   ";	
		mysql_query($query,$conexao);		   
        $message = 'Data Inserted';  		
      }   	  
 }  
 ?>

I only have a few notions of these codes.

  • Probably in your database, you did not set your primary key as auto increment

  • I understand, but I have a button to see the streets of the neighborhood and in the information I could see the name and ID of the neighborhood and the streets registered in it. If you want to see how I did it, I put the code.

1 answer

0

Your id registration button is missing function echo

Wrong:

id="<?php $linha['id']; ?>"

correct:

id="<?php echo $linha['id']; ?>"
  • It didn’t work, recorded the last id, but I got it through another code.

Browser other questions tagged

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