Ir para conteúdo
Fórum Script Brasil
  • 0

(Resolvido)  formulario


Von_Reibnitz

Pergunta

olá, criei um formulário, e a parte que insere os dados no banco esta assim:

$nome             = $_POST[nome];
$sobrenome        = $_POST["sobrenome"];
$sexo             = $_POST["sexo"];
$data_nasc        = "$_POST[ano]-$_POST[mes]-$_POST[dia]";
$cidade_nasc      = $_POST["cidade_nasc"];
$estado_nasc      = $_POST["estado_nasc"];
$data_falec       = "$_POST[ano2]-$_POST[mes2]-$_POST[dia2]";
$cidade_falec     = $_POST["cidade_falec"];
$estado_falec     = $_POST["estado_falec"];
$nota             = $_POST["nota"];

$sql = mysql_query("INSERT INTO obitos VALUES ('','$nome','$sobrenome','$sexo','$data_nasc','$cidade_nasc','$estado_nasc','$data_falec','$cidade_falec','$estado_falec','$nota','$idade')");

já havia deixado um campo idade no banco, como fazer para cadastrar a idade correta lá atravéz destes dados?

ou melhor ainda como mostrar a idade automaticamente no próprio formulário depois de inserir as datas? não seria mais interessante?

agradeço o auxílio.

Editado por Von_Reibnitz
Link para o comentário
Compartilhar em outros sites

5 respostass a esta questão

Posts Recomendados

  • 0

Olá, grato pelo pronto atendimento..

olha como esta funcionando até o momento:

cadastro.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Cadastro</title>
<style>
<!--
.textBox { border:1px solid gray; width:200px;} 
-->
</style>
<link href="estilo.css" rel="stylesheet" type="text/css" />
</head>

<body>
<form id="form1" name="form1" method="post" action="salvar.php">
  <table width="400" align="center">
    <tr>
      <td width="145">Nome</td>
      <td width="245"><input name="sobre" type="text" id="sobre" maxlength="100" class="textBox" /></td>
    </tr>
    <tr>
      <td>Sexo</td>
      <td><input name="sexo" type="radio" value="M" checked="checked" />
          <label>Masculino</label>
          <input name="sexo" type="radio" value="F" />
      <label>Feminino</label></td>
    </tr>
    <tr>
      <td><div align="left">Data Nascimento</div></td>
      <td><input name="dia" class="input" size="2" maxlength="2" />
        /
        <input name="mes" class="input" size="2" maxlength="2" />
        /
        <input name="ano" class="input" size="5" maxlength="4" /> 
         dd/mm/aaaa</td>
    </tr>
    <tr>
      <td><div align="left">Data Falecimento</div></td>
      <td><input name="dia2" class="input" size="2" maxlength="2" id="dia2" />
        /
        <input name="mes2" class="input" size="2" maxlength="2" id="mes2" />
        /
        <input name="ano2" class="input" size="5" maxlength="4" id="ano2" />
dd/mm/aaaa </td>
    </tr>
    <tr>
      <td>Cidade</td>
      <td><input name="cidade" type="text" id="cidade" maxlength="45" class="textBox" /></td>
    </tr>
    <tr>
      <td>Estado</td>
      <td><input name="estado" type="text" id="estado" maxlength="45" class="textBox" /></td>
    </tr>
    <tr>
      <td> </td>
      <td><input type="submit" name="Submit" value="Salvar" style="cursor:pointer;" /></td>
    </tr>
  </table>
</form>
</body>
</html>
salvar.php
<?php
if($_SERVER["REQUEST_METHOD"] == "POST") {
$nome         = $_POST["nome"];
$sobre        = $_POST["sobre"];
$sexo         = $_POST["sexo"];
$data_nasc    = "$_POST[ano]-$_POST[mes]-$_POST[dia]";
$data_falec   = "$_POST[ano2]-$_POST[mes2]-$_POST[dia2]";
$cidade       = $_POST["cidade"];
$estado       = $_POST["estado"];

include "config.php";


if(@mysql_query("INSERT INTO obitos VALUES ('$nome', '$sobre', '$sexo' , '$data_nasc', '$data_falec', '$cidade', '$estado' )")) {

    if(mysql_affected_rows() == 1){
        echo "Registro efetuado com sucesso<br />";
    }    

} else {
    if(mysql_errno() == 1062) {
        echo $erros[mysql_errno()];
        exit;
    } else {    
        echo "Erro não foi possivel efetuar o cadastro";
        exit;
    }    
    @mysql_close();
}

}
?>
<a href="index.html">Voltar</a>
listar.php
<?php require_once('Connections/obituario.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $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;
}
}

$maxRows_Recordset1 = 10;
$pageNum_Recordset1 = 0;
if (isset($_GET['pageNum_Recordset1'])) {
  $pageNum_Recordset1 = $_GET['pageNum_Recordset1'];
}
$startRow_Recordset1 = $pageNum_Recordset1 * $maxRows_Recordset1;

mysql_select_db($database_obituario, $obituario);
$query_Recordset1 = "SELECT nome, sexo, data_nasc, data_falec, cidade, estado FROM obitos ORDER BY data_falec DESC";
$query_limit_Recordset1 = sprintf("%s LIMIT %d, %d", $query_Recordset1, $startRow_Recordset1, $maxRows_Recordset1);
$Recordset1 = mysql_query($query_limit_Recordset1, $obituario) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);

if (isset($_GET['totalRows_Recordset1'])) {
  $totalRows_Recordset1 = $_GET['totalRows_Recordset1'];
} else {
  $all_Recordset1 = mysql_query($query_Recordset1);
  $totalRows_Recordset1 = mysql_num_rows($all_Recordset1);
}
$totalPages_Recordset1 = ceil($totalRows_Recordset1/$maxRows_Recordset1)-1;
?>
<a href="index.html">Voltar</a>
    
</table>
<p> </p>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td bgcolor="#CCCCCC"><div align="center"><strong>Ações</strong></div></td>
    <td bgcolor="#CCCCCC"><div align="center"><strong>Nome</strong></div></td>
    <td bgcolor="#CCCCCC"><div align="center"><strong>Sexo</strong></div></td>
    <td bgcolor="#CCCCCC"><div align="center"><strong>Data de Nascimento</strong></div></td>
    <td bgcolor="#CCCCCC"><div align="center"><strong>Data de Falecimento</strong></div></td>
    <td bgcolor="#CCCCCC"><div align="center"><strong>Cidade</strong></div></td>
    <td bgcolor="#CCCCCC"><div align="center"><strong>Estado</strong></div></td>
  </tr>
  <?php do { ?>
    <tr>
      <td><div align="center">
        <table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
          <tr>
            <td><div align="center"><img src="imagens/admin/botao_drop.png" alt="Excluir" width="16" height="16" /></div></td>
            <td><div align="center"><img src="imagens/admin/botao_edit.png" alt="Editar" width="16" height="16" /></div></td>
          </tr>
        </table>
        <?php
        $nascimento= date("d/m/Y", strtotime($row_Recordset1['data_nasc']));
        $falecimento = date("d/m/Y", strtotime($row_Recordset1['data_falec']));
        ?>
      </div></td>
      <td><div align="center"><?php echo $row_Recordset1['nome']; ?></div></td>
      <td><div align="center"><?php echo $row_Recordset1['sexo']; ?></div></td>
      <td><div align="center"><?php echo $nascimento; ?></div></td>
      <td><div align="center"><?php echo $falecimento; ?></div></td>
      <td><div align="center"><?php echo $row_Recordset1['cidade']; ?></div></td>
      <td><div align="center"><?php echo $row_Recordset1['estado']; ?></div></td>
    </tr>
    <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</table>
<p align="center"> </p>
<?php
mysql_free_result($Recordset1);
?>
falecimentos-do-mes.php
<?php require_once('Connections/obituario.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $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;
}
}

$maxRows_falecimentos = 5;
$pageNum_falecimentos = 0;
if (isset($_GET['pageNum_falecimentos'])) {
  $pageNum_falecimentos = $_GET['pageNum_falecimentos'];
}
$startRow_falecimentos = $pageNum_falecimentos * $maxRows_falecimentos;

mysql_select_db($database_obituario, $obituario);
$query_falecimentos = "SELECT * FROM obitos WHERE data_falec LIKE '" . date(Y) . "-" . date(m). "%' ";
$query_limit_falecimentos = sprintf("%s LIMIT %d, %d", $query_falecimentos, $startRow_falecimentos, $maxRows_falecimentos);
$falecimentos = mysql_query($query_limit_falecimentos, $obituario) or die(mysql_error());
$row_falecimentos = mysql_fetch_assoc($falecimentos);

if (isset($_GET['totalRows_falecimentos'])) {
  $totalRows_falecimentos = $_GET['totalRows_falecimentos'];
} else {
  $all_falecimentos = mysql_query($query_falecimentos);
  $totalRows_falecimentos = mysql_num_rows($all_falecimentos);
}
$totalPages_falecimentos = ceil($totalRows_falecimentos/$maxRows_falecimentos)-1;
?><div align="center">
  <p align="center"><font color="#0000CC"><strong>VIDAPAE</strong></font> </p>
  <p align="center"><strong><font color="#009966">informa o Obtuário do mês de</font></strong> <strong> <font color="#0000CC">
    <?
$mes = date("m");

if($mes == "01"){ echo "Janeiro";}
if($mes == "02"){ echo "Fevereiro";}
if($mes == "03"){ echo "Marco";}
if($mes == "04"){ echo "Abril";}
if($mes == "05"){ echo "Maio";}
if($mes == "06"){ echo "Junho";}
if($mes == "07"){ echo "Julho";}
if($mes == "08"){ echo "Agosto";}
if($mes == "09"){ echo "Setembro";}
if($mes == "10"){ echo "Outubro";}
if($mes == "11"){ echo "Novembro";}
if($mes == "12"){ echo "Dezembro";}
?>
  </font></strong>.</p>
</div>
<table width="90%" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#009966">
  <tr>
    <td bgcolor="#99ccff"><div align="left"><font color="#0000CC"><strong>Nome</strong></font></div></td>
    <td bgcolor="#99ccff"><div align="left"><font color="#0000CC"><strong>Data de Nascimento</strong></font></div></td>
    <td bgcolor="#99ccff"><div align="left"><font color="#0000CC"><strong>Data de Falecimento</strong></font></div></td>
    <td bgcolor="#99ccff"><div align="left"><font color="#0000CC"><strong>Sexo</strong></font></div></td>
    <td bgcolor="#99ccff"><div align="left"><font color="#0000CC"><strong>Idade</strong></font></div></td>
    <td bgcolor="#99ccff"><div align="left"><font color="#0000CC"><strong>Cidade</strong></font></div></td>
    <td bgcolor="#99ccff"><div align="left"><font color="#0000CC"><strong>Estado</strong></font></div></td>
  </tr>
  <?php do { ?>
    <tr>
      <td><strong><?php echo $row_falecimentos['nome']; ?></strong></td>
      <td><p align="center"><strong><img src="imagens/estrela.gif" width="20" height="18" /></strong></p>
<?php
$nascimento= date("d/m/Y", strtotime($row_falecimentos['data_nasc']));
$falecimento = date("d/m/Y", strtotime($row_falecimentos['data_falec']));
?>
        <p align="center"><strong><?php echo $nascimento; ?></strong></p></td>
      <td><p align="center"><strong><img src="imagens/cruz.gif" width="16" height="20" /></strong></p>
        <p align="center"><strong><?php echo $falecimento; ?></strong></p></td>
      <td><div align="center"><strong><?php echo $row_falecimentos['sexo']; ?></strong></div></td>
      <td><div align="center"><strong><?php echo $row_falecimentos['cidade']; ?></strong></div></td>
      <td><strong><?php echo $row_falecimentos['cidade']; ?></strong></td>
      <td><strong><?php echo $row_falecimentos['estado']; ?></strong></td>
    </tr>
    <?php } while ($row_falecimentos = mysql_fetch_assoc($falecimentos)); ?>
</table>
<p align="center"> </p>
<?php
mysql_free_result($falecimentos);
?>
até aeee eu fiz, mas como dá para perceber estou utilizando
$query_falecimentos = "SELECT * FROM obitos WHERE data_falec LIKE '" . date(Y) . "-" . date(m). "%' ";
em um teste.php usei isso
$Consulta = mysql_query("SELECT nome, data_nasc,
                        (YEAR(data_falec)-YEAR(data_nasc)) - (RIGHT(data_falec,5)<RIGHT(data_nasc,5))
                        AS idade
                        FROM obitos
                        ORDER BY data_falec") or die (mysql_error());

realmente mostra as idades mas como utilizar isso nos meus códigos eu não sei fazer.

agradeço o auxílio.

Link para o comentário
Compartilhar em outros sites

  • 0

estou utlizando:

$query_falecimentos = "SELECT * FROM obitos WHERE data_falec LIKE '" . date(Y) . "-" . date(m). "%' ";
isso mostra os eventos que ocorreram no mes, tah funcionando beleza. mas seu eu desejar mostrar a idade também? fiz uma página de teste utilizando apenas isso>
$Consulta = mysql_query("SELECT nome, data_nasc,
                       [b] (YEAR(data_falec)-YEAR(data_nasc)) - (RIGHT(data_falec,5)<RIGHT(data_nasc,5))
                        AS idade[/b]                        FROM obitos
                        ORDER BY data_falec") or die (mysql_error());

realmente mostrou o nome data e a idade, mas como mostrar a idade utilizando a menira atual?

creio que me expressei melhor agora.

Link para o comentário
Compartilhar em outros sites

  • 0

Grato, porque era impossível que eu postasse algo aquela hora, pois estava dando aula.

e realmente eu consegui solucionar isso tudo apenas inserindo no banco de dados a idade também, visto que as datas não serão alteradas.

grato pelo pronto atendimento.

resolvi a questão inserindo a idade juntamente com o cadastro no banco desta forma

salvar_bd.php

<?
$nome             = $_POST[nome];
$sobrenome        = $_POST["sobrenome"];
$sexo             = $_POST["sexo"];
$data_nasc        = "$_POST[ano]-$_POST[mes]-$_POST[dia]";
$cidade_nasc      = $_POST["cidade_nasc"];
$estado_nasc      = $_POST["estado_nasc"];
$data_falec       = "$_POST[ano2]-$_POST[mes2]-$_POST[dia2]";
$cidade_falec     = $_POST["cidade_falec"];
$estado_falec     = $_POST["estado_falec"];
$nota             = $_POST["nota"];
$idade = floor((strtotime($data_falec) - strtotime($data_nasc))/(60*60*24*365.2425));

$sql = mysql_query("INSERT INTO obitos VALUES ('','$nome','$sobrenome','$sexo','$data_nasc','$cidade_nasc','$estado_nasc','$data_falec','$cidade_falec','$estado_falec','$nota','$idade')");
?>
<h3>Cadastro de Óbito efetuado com sucesso!</h3>
<br>

<a href='?pg=../estrutura/obituario/listar.php'>Voltar</a>

Grato pelo auxílio encontrado.

Link para o comentário
Compartilhar em outros sites

Participe da discussão

Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.

Visitante
Responder esta pergunta...

×   Você colou conteúdo com formatação.   Remover formatação

  Apenas 75 emoticons são permitidos.

×   Seu link foi incorporado automaticamente.   Exibir como um link em vez disso

×   Seu conteúdo anterior foi restaurado.   Limpar Editor

×   Você não pode colar imagens diretamente. Carregar ou inserir imagens do URL.



  • Estatísticas dos Fóruns

    • Tópicos
      152,1k
    • Posts
      651,8k
×
×
  • Criar Novo...