Relacao de Tabelas Mysql dreamweaver e php
Bom dia.. alguém poderia me ajudar(Iniciante), estou usando o Dreamweaver php e mysql para fazer um sistema de cadastro porem o cliente quando for de menor precisa que seja cadastrado um responsável porem estou fazendo em tabelas diferentes e com uma tabela relacionando as duas. enfim
estou com dificuldade para poder salvar nas duas tabelas usando a FK. ou seja preciso salvar o cliente e se ele for menor de idade preciso relacionar no banco que ele tem um responsável.
como fazer?? segue abaixo meu codigo para entenderem melhor
esse e o PHP
Esse e o Formulario principal
esse e o formulario do responsavel fiz ele com um modal do bootstrap
estou com dificuldade para poder salvar nas duas tabelas usando a FK. ou seja preciso salvar o cliente e se ele for menor de idade preciso relacionar no banco que ele tem um responsável.
como fazer?? segue abaixo meu codigo para entenderem melhor
esse e o PHP
<?php if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { if (PHP_VERSION < 6) { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; } $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? doubleval($theValue) : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } } $editFormAction = $_SERVER['PHP_SELF']; if (isset($_SERVER['QUERY_STRING'])) { $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']); } $data = implode("-",array_reverse(explode("/",$data))); function converter_data($strData) { // Recebemos a data no formato: dd/mm/aaaa // Convertemos a data para o formato: aaaa-mm-dd if ( preg_match("#/#",$strData) == 1 ) { $strDataFinal = "'"; $strDataFinal .= implode('-', array_reverse(explode('/',$strData))); $strDataFinal .= "'"; } return $strDataFinal; } if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "cadresp")) { $insertSQL = sprintf("INSERT INTO responsavel (nomecompleto, parentesco, telefone, sexo, datanascimento, rg, cpf, cep, estado, cidade, bairro, endereco) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)", GetSQLValueString($_POST['nome'], "text"), GetSQLValueString($_POST['parente'], "text"), GetSQLValueString($_POST['tel'], "text"), GetSQLValueString($_POST['sexo'], "text"), converter_data($_POST['data'], "date"), GetSQLValueString($_POST['rg'], "text"), GetSQLValueString($_POST['cpf'], "text"), GetSQLValueString($_POST['cep'], "text"), GetSQLValueString($_POST['estado'], "text"), GetSQLValueString($_POST['cidade'], "text"), GetSQLValueString($_POST['bairro'], "text"), GetSQLValueString($_POST['endereco'], "text")); mysql_select_db($database_dbclientes, $dbclientes); $Result1 = mysql_query($insertSQL, $dbclientes) or die(mysql_error()); } $colname_rsbusca = "-1"; if (isset($_GET['cpf'])) { $colname_rsbusca = $_GET['cpf']; } mysql_select_db($database_dbclientes, $dbclientes); $query_rsbusca = sprintf("SELECT * FROM cadclientes WHERE cpf LIKE %s", GetSQLValueString("%" . $colname_rsbusca . "%", "text")); $rsbusca = mysql_query($query_rsbusca, $dbclientes) or die(mysql_error()); $row_rsbusca = mysql_fetch_assoc($rsbusca); $totalRows_rsbusca = mysql_num_rows($rsbusca); if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "cadastro")) { $insertSQL = sprintf("INSERT INTO cadclientes (nomecompleto, sexo, dtnascimento, rg, orgaoexpedidor, cpf, unidadeatendimento, telprincipal, celular, telrecado, email, cep, estado, cidade, bairro, endereco) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)", GetSQLValueString($_POST['nome'], "text"), GetSQLValueString($_POST['sexo'], "text"), converter_data($_POST['data'], "date"), GetSQLValueString($_POST['rg'], "int"), GetSQLValueString($_POST['ssp'], "text"), GetSQLValueString($_POST['cpf'], "text"), GetSQLValueString($_POST['unidade'], "text"), GetSQLValueString($_POST['tel'], "text"), GetSQLValueString($_POST['tel1'], "text"), GetSQLValueString($_POST['tel2'], "text"), GetSQLValueString($_POST['email'], "text"), GetSQLValueString($_POST['cep'], "text"), GetSQLValueString($_POST['estado'], "text"), GetSQLValueString($_POST['cidade'], "text"), GetSQLValueString($_POST['bairro'], "text"), GetSQLValueString($_POST['endereco'], "text")); mysql_select_db($database_dbclientes, $dbclientes); $Result1 = mysql_query($insertSQL, $dbclientes) or die(mysql_error()); $insertGoTo = "CadastroExame.php"; if (isset($_SERVER['QUERY_STRING'])) { $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?"; $insertGoTo .= $_SERVER['QUERY_STRING']; } header(sprintf("Location: %s", $insertGoTo)); }
Esse e o Formulario principal
<div class="row"> <fieldset class="bs-callout bs-callout-info col-md-12"> <legend>Pesquisa de Clientes</legend> <form action="CadastroCliente.php" name="pesquisa" method="GET"> <div class="form-group col-md-4"> <p> <label for="buscacpf">INFORME O CPF PARA PESQUISA</label> <input type="text" class="form-control" name="cpf" onBlur="ValidarCPF(pesquisa.cpf);" onKeyPress="MascaraCPF(pesquisa.cpf);" maxlength="14" required> <p> <input type="submit" class="btn btn-primary" value="Consultar"> </div> </form> </fieldset> <fieldset class="bs-callout bs-callout-info col-md-12"> <legend> <h3>DADOS PESSOAIS</h3> </legend> <form name="cadastro" method="POST"> <div class="form-group col-md-12"> <label for="nome">NOME COMPLETO:</label> <input type="text" class="form-control input-sm" name="nome" id="nome" required> </div> <div class="form-group col-xs-3"> <label for="unidade">SEXO:</label> <select class="form-control input-sm" name="sexo"> <option></option> <option>MASCULINO</option> <option>FEMININO</option> </select> </div> <div class="form-group col-md-3"> <label for="nascimento">DATA DE NASCIMENTO:</label> <input type="date" class="form-control input-sm" name="data" id="data" onKeyPress="MascaraData(cadastro.data);" maxlength="10" onBlur= "ValidaDatacadastro.data);" value="" required> </div> <div class="form-group col-md-3"> <label for="rg">RG:</label> <input type="text" class="form-control input-sm" name="rg" id="rg" required> </div> <div class="form-group col-md-3"> <label for="ssp">ORGÃO EXPEDIDOR:</label> <input type="text" class="form-control input-sm" name="ssp" id="ssp" required> </div> <div class="form-group col-md-3"> <label for="chkbox">PACIENTE MENOR DE IDADE?</label> <p> <input type="checkbox" name="habi" id="habi" value="sim" onClick="HabiDsabi()"> </div> <div class="has-error form-group col-md-3"> <label for="resp">INFORMAR O RESPONSÁVEL:</label> <button type="button" name="envia" id="envia" class="btn btn-primary btn-sm" data-toggle="modal" data-target="#responsavel" disabled> Responsável </button> </div> <div class="form-group col-sm-6"> <label for="cpf">CPF:</label> <input type="text" class="form-control input-sm" name="cpf" onBlur="ValidarCPF(cadastro.cpf);" onKeyPress="MascaraCPF(cadastro.cpf);" maxlength="14" required> </div> <div class="form-group col-md-6"> <label for="unidade">UNIDADE DE ATENDIMENTO:</label> <select class="form-control input-sm" name="unidade"> <option>Unidade</option> </select> </div> <p> </p> <p> </p> <p> </p> <legend> <h3>CONTATO</h3> </legend> <div class="form-group col-md-4"> <label for="telefone">TEL. PRINCIPAL:</label> <input type="text" class="form-control input-sm" name="tel" onKeyPress="MascaraTelefone(cadastro.tel);" maxlength="14" onBlur="ValidaTelefone(cadastronew.tel);" required> </div> <div class="form-group col-md-4"> <label for="telefone">CELULAR:</label> <input type="text" class="form-control input-sm" name="tel1" onKeyPress="MascaraTelefone1(cadastro.tel1);" maxlength="14" onBlur="ValidaTelefone1(cadastronew.tel1);"> </div> <div class="form-group col-md-4"> <label for="telefone">TEL. RECADO:</label> <input type="text" class="form-control input-sm" name="tel2" onKeyPress="MascaraTelefone2(cadastro.tel2);" maxlength="14" onBlur="ValidaTelefone2(cadastronew.tel2);"> </div> <div class="form-group col-md-4"> <label for="telefone">EMAIL:</label> <input type="text" class="form-control input-sm" name="email" required> </div> <p> </p> <legend> <h3>ENDEREÇO</h3> </legend> <div class="form-group col-md-3"> <label for="cep">CEP:</label> <input type="text" class="form-control input-sm" name="cep" id="cep"> </div> <div class="form-group col-md-3"> <label for="estado">ESTADO:</label> <input type="text" class="form-control input-sm" name="estado" id="estado" required> </div> <div class="form-group col-md-3"> <label for="cidade">CIDADE:</label> <input type="text" class="form-control input-sm" name="cidade" id="cidade" required> </div> <div class="form-group col-md-3"> <label for="bairro">BAIRRO:</label> <input type="text" class="form-control input-sm" name="bairro" id="bairro" required> </div> <div class="form-group col-md-12"> <label for="endereco">ENDEREÇO:</label> <input type="text" class="form-control input-sm" name="endereco" id="endereco" required> </div> <input type="reset" class="btn btn-danger" value="Cancelar"> <input type="submit" class="btn btn-primary" value="Gravar Registro"> <input type="hidden" name="MM_insert" value="cadastro"> </form> </fieldset> </div> </div>
esse e o formulario do responsavel fiz ele com um modal do bootstrap
<div class="modal fade" id="responsavel" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button> <h3 class="modal-title" id="myModalLabel">DADOS DO RESPONSÁVEL</h3> </div> <div class="modal-body"> <form action="<?php echo $editFormAction; ?>" name="cadresp" method="POST"> <div class="form-group col-md-12"> <label for="nome">NOME COMPLETO RESPONSÁVEL:</label> <input type="text" class="form-control input-sm" name="nome" id="nome" required> </div> <div class="form-group col-md-12"> <label for="parente">GRAU DE PARENTESCO:</label> <input type="text" class="form-control input-sm" name="parente" id="parente" required> </div> <p></p> <p></p> <div class="form-group col-md-12"> <label for="nome">TELEFONE PRINCIPAL:</label> <input type="text" class="form-control input-sm" name="tel" onKeyPress="MascaraTelefone(cadresp.tel);"maxlength="14" onBlur="ValidaTelefone(cadrespnew.tel);"> </div> <div class="form-group col-md-12"> <label for="sexo">SEXO:</label> <p> <label> <input type="radio" name="sexo" value="masculino" id="Sexo_0"> MASCULINO</label> <label> <input type="radio" name="sexo" value="feminino" id="Sexo_1"> FEMININO</label> </p> </div> <div class="form-group col-md-6"> <label for="nascimento">DATA DE NASCIMENTO:</label> <input type="text" class="form-control input-sm" name="data" onKeyPress="MascaraData(cadresp.data);" maxlength="10" onBlur= "ValidaDatacadastro.data);" required> </div> <div class="form-group col-md-6"> <label for="rg">RG:</label> <input type="text" class="form-control input-sm" name="rg" id="rg"> </div> <div class="form-group col-md-12"> <label for="cpf">CPF:</label> <input type="text" class="form-control input-sm" name="cpf" onBlur="ValidarCPF(cadresp.cpf);" onKeyPress="MascaraCPF(cadresp.cpf);" maxlength="14" required> </div> <div class="form-group col-md-3"> <label for="cep">CEP:</label> <input type="text" class="form-control input-sm" name="cep" id="cep4"> </div> <div class="form-group col-md-3"> <label for="estado">ESTADO:</label> <input type="text" class="form-control input-sm" name="estado" id="estado4" required> </div> <div class="form-group col-md-3"> <label for="cidade">CIDADE:</label> <input type="text" class="form-control input-sm" name="cidade" id="cidade4" required> </div> <div class="form-group col-md-3"> <label for="bairro">BAIRRO:</label> <input type="text" class="form-control input-sm" name="bairro" id="bairro4" required> </div> <div class="form-group col-md-12"> <label for="endereco">ENDEREÇO:</label> <input type="text" class="form-control input-sm" name="endereco" id="endereco4" required> </div> </div> <div class="footer"> <input type="reset" class="btn btn-danger" value="Cancelar"> <input type="submit" class="btn btn-primary" value="Gravar Registro"> <input type="hidden" name="MM_insert" value="cadresp"> </form> </div> </div> </div>
Daniel Martins
Curtidas 0