I'm having trouble inserting input data from a dynamic input. My procedure only inserts once, it follows my code where I get 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 the 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 related to the 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 procedure:
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 this anyway but it did not 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 another way, but the same effect happens, just inserts a line and returns error. Here is the code snippet:
$msg = mysql_query("Call telefone_i(
'".$userid[$i]."',
'".$ddd[$i]."',
'".$telefone[$i]."',
'".$obs[$i]."',
'".$idusuario[$i]."'
)");
Thanks in advance for your help.