6
I’m having trouble inserting incoming data from a dynamic input. My Procedure only inserts once, follows my code where I receive the data:
$ddd = $_POST['ddd'];
$userid = $_POST['userid'];
$idusuario = $_POST['idusuario'];
$telefone = $_POST['telefone'];
$obs = $_POST['obs'];
$quant_linhas = count($ddd);
for ($i=0; $i<$quant_linhas; $i++) {
$sql = "Call telefone_i(
'".$userid[$i]."',
'".$ddd[$i]."',
'".$telefone[$i]."',
'".$obs[$i]."',
'".$idusuario[$i]."'
)";
}
In this section is the jQuery that creates dynamic inputs:
<script type="text/javascript">
$(function () {
function removeCampo() {
$(".removerCampo").unbind("click");
$(".removerCampo").bind("click", function () {
if($("tr.linhas").length > 1){
$(this).parent().parent().remove();
}
});
}
$(".adicionarCampo").click(function () {
novoCampo = $("tr.linhas:first").clone();
novoCampo.find("input").val("");
novoCampo.insertAfter("tr.linhas:last");
removeCampo();
});
});
</script>
Follow the excerpt for inputs:
<div class="content" style="min-height: 150px;">
<!-- CONTEUDO INICIO -->
<center>
<h2 style="background-color: #DDEBFF">Cadastrar Telefone</h2>
</center>
<div style=" margin-left:5px; padding: 5px; width: 600px; overflow:auto; border:#036 thin; border-style:dotted;">
<form action="telefone_inserir.php?id=<?php echo $usuario_id; ?>" method="post" enctype="multipart/form-data">
<table class="list">
<thead>
<tr class="linhas">
<td width="25%" class="right">DDD</td>
<td class="left">
<input type="text" name="ddd[]" value="" maxlength="2" style="width: 30px" />
<input name="userid[]" type="hidden" value="<?php echo $id; ?>" />
<input name="idusuario[]" type="hidden" value="<?php echo $usuario_id; ?>" />
</td>
<td width="25%" class="right">Telefone</td>
<td class="left"><input type="text" name="telefone[]" value="" maxlength="9" style="width: 80px" /></td>
<td width="25%" class="right">OBS:</td>
<td class="left"><input type="text" name="obs[]" value="" maxlength="45" style="width: 200px" /></td>
<td class="left"><a href="#" class="removerCampo" title="Remover linha"><img src="imagens/exc_btn.png" border="0" /></a></td>
</tr>
<tr><td colspan="7">
<a href="#" class="adicionarCampo" title="Adicionar item"><img src="imagens/tel_btn.png" border="0" /></a>
</td></tr>
<tr>
<td colspan="7" align="center" height="50">
<input name="botao" type="image" value="Alterar" id="btn_salvar" src="imagens/cadastrar_btn.png">
</td>
</tr>
</thead>
</table>
</form>
</div>
<!-- CONTEUDO FIM -->
</div>
My previous:
CREATE DEFINER=`root`@`localhost` PROCEDURE `telefone_i`(
p_usuario_id int(11) ,
p_ddd int(3) ,
p_telefone int(9) ,
p_obs varchar(45) ,
p_usuario_lancamento int(11)
)
BEGIN
insert acad.telefone
set
usuario_id = p_usuario_id,
ddd = p_ddd,
telefone = p_telefone,
obs = p_obs,
data_lancamento = current_timestamp,
usuario_lancamento = p_usuario_lancamento;
END
I tried to do it this way but it didn’t work:
for ($i=0; $i<$quant_linhas; $i++) {
$sql = mysqli_query($con, "Call telefone_i(
'".$userid[$i]."',
'".$ddd[$i]."',
'".$telefone[$i]."',
'".$obs[$i]."',
'".$idusuario[$i]."'
)");
}
But my Procedure inserts only 1 record and returns the error "Query was Empty". I tried otherwise, but the same effect happens, just inserts a line and returns error. Follows the code snippet:
$msg = mysql_query("Call telefone_i(
'".$userid[$i]."',
'".$ddd[$i]."',
'".$telefone[$i]."',
'".$obs[$i]."',
'".$idusuario[$i]."'
)");
From now on, I appreciate any help.
Its $id variable is an array?
– Marcelo Bonus
not inputs are dynamically created with jquery the $id is to count the input numbers that are sent
– opeta
This I understood... But what kind of data comes in it? An array, a string separated by comma?
– Marcelo Bonus
@Marcelobonus opa was wrong in place of $id was $ddd and comes in an array yes
– opeta
Like someone with 27 points offers a 50% reward ?
– Diego Souza
@opeta, I have already put an answer to your question, if she answers your question, mark it as accepted. Thank you.
– Ivan Ferrer
@Taopaipai simple I was with 77 when you offer the reward it automatically comes out of your points
– opeta