I have form
to insert into three different tables:
<form name="customer_details" method="POST" onsubmit="return form_validation()" >
<h5><strong>Utente</strong></h5> <input type="text" id="nome" name="nome" placeholder="Primeiro e último nome" style="width:300px" required />
<h5><strong>Nº Utente</strong></h5> <input type="text" id="codigoutente" name="codigoutente" style="width:80px" required />
<h5><strong>Valência</strong></h5>
<select name="codvalencia" required>
<option></option>
<?php
$sql = "SELECT * FROM centrodb.Valencia ORDER BY Valencia ASC";
$qr = mysqli_query($conn, $sql);
while($ln = mysqli_fetch_assoc($qr)){
echo '<option value="'.$ln['CodValencia'].'"> '.$ln['Valencia'].'</option>';
}
?>
</select>
<h5><strong>Responsável</strong></h5> <input type="text" id="Responsavel" name="Responsavel" style="width:350px" required />
<h5><strong>Contato</strong></h5>
<div class="input-group">
<span class="input-group-addon">351</span> <input type="text" class="input" id="Contato" name="Contato" style="width:150px" required />
</div>
<input type="submit" value="Registo"/>
</form>
Code to insert:
$nome = $_POST['nome'];
$codigoutente = $_POST['codigoutente'];
$codvalencia = $_POST['codvalencia'];
$Responsavel = $_POST['Responsavel'];
$Contato = $_POST['Contato'];
$stmt = $conn->prepare("INSERT INTO UtentesCons ('nome','codvalencia','codigoutente') VALUES ('$nome','$codvalencia','$codigoutente')");
mysqli_stmt_execute($stmt);
$stmt1 = $conn->prepare("INSERT INTO Responsaveis ('Responsavel','IdUtente') VALUES ('$Responsavel','$codigoutente')");
mysqli_stmt_execute($stmt1);
$last_id = $conn->insert_id;
$stmt2 = $conn->prepare("INSERT INTO ContatoRes ('IdUtente','IdResponsavel','Contato') VALUES ('$codigoutente','$last_id','351$Contato')");
mysqli_stmt_execute($stmt2);
So far, it's working properly.
Now I've added another form
before input
of submit
:
<input id="botao" style="float: right;" class="botao1" onclick="addresp()" value="Adicionar Responsável"/>
<div id="novoresp">
</div>
<div class="teste" id="respform" hidden>
<h5><strong>Responsável</strong></h5> <input type="text" id="Responsavel" name="Responsavel" style="width:350px"/>
<h5><strong>Contato</strong></h5>
<div class="input-group">
<span class="input-group-addon">351</span> <input type="text" class="input" id="Contato" name="Contato" style="width:150px"/>
</div>
</div>
If you have more than one new person in charge, form novoresp
would also insert the same tables in the first form, in these two:
$stmt1 = $conn->prepare("INSERT INTO Responsaveis ('Responsavel','IdUtente') VALUES ('$Responsavel','$codigoutente')");
mysqli_stmt_execute($stmt1);
$last_id = $conn->insert_id;
$stmt2 = $conn->prepare("INSERT INTO ContatoRes ('IdUtente','IdResponsavel','Contato') VALUES ('$codigoutente','$last_id','351$Contato')");
mysqli_stmt_execute($stmt2);