-1
I have a small problem trying to get a new record to be included in another table, ie:
I have a table-A in the bank with the columns:
id | nome | email | tel | campanha
in table-B I have the columns:
id | id-tabela-A | valores
in short, I don’t know where to start, but basically it’s taking the id of the A-table and including it in the B-table inside the id-table-A column, it’s possible to do this with PHP?
<?php
include_once("conexao.php");
$nome_usuario = $_POST['nome'];
$email_usuario = $_POST['email'];
$tel_usuario = $_POST['telefone'];
$fonte_usuario = $_POST['fonte'];
$status_usuario = "2"; //Definir ( 1 - Cliente, 2 - Prospect, 3 - Contato Inicial, 4 - Sem contato, 5 - Enviada a proposta, 6 - Sem contato )
// CAMPANHA
$campanha_valor = "CAMPANHA EBOOK"; // NOME DA CAMPANHA
$campanha_id = "1"; //ID CAMPO CAMPANHA
// tereriano ?
$tereriano_usuario = $_POST['checagem'];
$tereriano_id = "2"; //ID CAMPO VOCENIADO
$nome_usuario = mysql_escape_string($nome_usuario);
$email_usuario = mysql_escape_string($email_usuario);
$tel_usuario = mysql_escape_string($tel_usuario);
//$result_usuario = "INSERT INTO usuarios(nome, email) VALUES ('$nome_usuario','$email_usuario')";
$result_usuario = "INSERT INTO tblleads('name',assigned, 'status', source, email, phonenumber, campanha, tereriano) VALUES ('$nome_usuario',RecuperaProximoAssigned(),'$status_usuario','$fonte_usuario','$email_usuario','$tel_usuario','$campanha_id','$tereriano_usuario')";
$resultado_usuario = mysqli_query($conn, $result_usuario); //EXECUTA
//$lastID = mysqli_insert_id($conn); // ID DO REGISTRO GERADO
//obtem o último ID duma instrução INSERT
$lastID = mysqli_query($conn, "SELECT LAST_INSERT_ID();");
$result_usuario_2 = "INSERT INTO tblcustomfieldsvalues(relid,fieldid,fieldto,value) VALUES ('$lastID','$tereriano_id',leads, '$tereriano_usuario')"; //INSERE O tereriano
mysqli_query($conn, $result_usuario_2); //EXECUTA
$result_usuario_3 = "INSERT INTO tblcustomfieldsvalues(relid,fieldid,fieldto,'value') VALUES ('$lastID','$campanha_id',leads, '$campanha_valor')"; //INSERE A CAMPANHA
mysqli_query($conn, $result_usuario_3); //EXECUTA
if(mysqli_affected_rows($conn) != 0){
echo "
<META HTTP-EQUIV=REFRESH CONTENT = '0;URL=#'>
<script type='text/javascript'>
alert('E-mail cadastrado com Sucesso.');
</script>
";
}else{
echo "
<META HTTP-EQUIV=REFRESH CONTENT = '0;URL=#'>
<script type='text/javascript'>
alert('Houve algum erro!.');
</script>
";
}
//CREATE FUNCTION RecuperaProximoAssigned ()
//RETURNS INT RETURN (
// CASE
// WHEN (SELECT COUNT(1) FROM tblleads) = 0 THEN 1
// WHEN (SELECT assigned FROM tblleads ORDER BY id DESC LIMIT 1) = 5 THEN 1
// ELSE (SELECT assigned FROM tblleads ORDER BY id DESC LIMIT 1) + 1
// END
// );
?>
when you insert into
tabela-A
need to go in the comics.. why not take advantage of the access to the base and already create the Insert intabela-B
?– rLinhares
I made a change, made the code available for better understanding.
– Junior Rocha
Possible duplicate of Copy record from one table to another Mysql
– Sorack
You want to return the last ID inserted in table A and insert it in table B ?
– Wagner Fillio