Insert in two different tables

0

I have two tables ( cliente and telefone ), I need to insert the name in the cliente table and the client phone in the telefone table, at the same time, by the same form.

In the HTML form I was able to put all the fields (name, ddd, phone) but I can not send them to both tables.

if(isset($_POST['nome'])){
    $nome = filter_input(INPUT_POST,'nome',FILTER_SANITIZE_STRING);
    $ddd = filter_input(INPUT_POST,'ddd',FILTER_SANITIZE_STRING);
    $telefone = filter_input(INPUT_POST,'telefone',FILTER_SANITIZE_STRING);    

    $solicita = "INSERT INTO 'cliente' ('nome') VALUES ($nome),
                 INSERT INTO 'telefone' ('ddd','telefone') VALUES ($ddd,$telefone)";
    $query = mysqli_query($conn,$solicita);

    if($linha_usuario = mysqli_insert_id($conn)){  
    echo "Cadastrado com sucesso";
    }else{
        echo "Erro ao cadastrar";
    }
}
?>
<!doctype html>
<html>

<head>
    <meta charset="UTF-8">
    <title>Desafio Estágio</title>
    <link rel="stylesheet" href="estilo.css">
</head>

<body>
    <h1>Cadastro de clientes</h1>
    <form action="index.php" method="post" id="form">
        <label for="nome">Nome</label>
        <input type="text" id="nome" name="nome">
        <label for="ddd">D.D.D</label>
        <input type="text" id="ddd" name="ddd" size="2">
        <label for="telefone">Telefone</label>
        <input type="text" id="telefone" name="telefone">
        <input type="submit" value="Cadastrar" id="cadastrar">
    </form>

</body>

</html>

    
asked by anonymous 15.11.2018 / 08:16

1 answer

0

I believe the error happens because the client's FK field in the telephone table is not in your second insert, it's missing fields. Divide the 2 inserts and before inserting the phone try doing what I did below.

$sql = select * from cliente where like '%$nome%';
$consulta = mysqli_query($conn,$sql);
while($linha = mysqli_fetch_array($consulta)){

$id = $linha['id'];
}

if($id == TRUE) {

$sqlTelefone = 'insert into telefone (cliente_id, ddd, telefone) values ($id,$ddd,$telefone)';
$resultado = mysqli_query($conn,$sqlTelefone);

}

Dude, I did head, it may have syntax error, but anything leaves a comment. Hugs!

    
17.11.2018 / 14:13