Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

TiagoMaker

[Resolvido] Formulário com update e insert de tabelas diferentes

Recommended Posts

Dae pessoal, tenho um formulário que recebe dados da tabela ATENDIMENTOS, e ao cadastrar ele insere na tabela FILIAIS, só que ao cadastrar eu teria que fazer um update na tabela ATENDIMENTOS para alterar o campo conclusão, que antes era SUBSTITUIR e depois será RESOLVIDO, que estão em botão de rádio representados pelos números 1 e 3 respectivamente. O problema é que não tem nenhum campo que permite mudar o 1 para 3, ele deve fazer o update junto com o cadastro. Tentei representar a query da seguinte forma:

 

if ($_POST[bt] == 'CADASTRAR')
{
	$sql = "INSERT INTO filiais (data, loja, equipamento, problema, caminhao, contato, nf, cobrado, recebido) VALUES ('".$_POST["data"]."', '".$_POST["loja"]."', '".$_POST["equipamento"]."', '".$_POST["problema"]."', '".$_POST["caminhao"]."', '".$_POST["contato"]."', '".$_POST["nf"]."', '".$_POST["cobrado"]."', '".$_POST["recebido"]."')";
	query($sql);
	
        $sql2 = "UPDATE atendimento SET conclusao = '3' WHERE id_atendimento = $_POST[codigo] ";
	query($sql2);
	
	header("Location: cadastro.php?msg=sucesso");

}

Não sei se é assim que faz, tambem tentei por as 2 sql como "$sql", mas não deu certo. sempre retorna o erro:

 

Erro: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

Compartilhar este post


Link para o post
Compartilhar em outros sites

Se eu tiro aquele sql2 com o update, ele faz o insert em FILIAIS corretamente, ele só da o erro se tem esse sql2 com o update da tabela atendimentos.

 

a pagina de cadastro é assim:

 

<?php include("../includes/config.php"); ?>
<?php
if ($_GET[id] > 0)
{
	require_once("../includes/conect_mysql.php");
	conecta_BD();
	
	$sql = "SELECT * FROM filiais WHERE id_registro = $_GET[id]";
	$linha = query($sql);
	$dados = le_linha($linha);

}
if ($_GET[at] > 0)
{
	require_once("../includes/conect_mysql.php");
	conecta_BD();
	
	$sql = "SELECT * FROM atendimento WHERE id_atendimento = $_GET[at]";
	$linha = query($sql);
	$dados = le_linha($linha);

}

?>
<!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" />
<link href="../includes/style.css" rel="stylesheet" type="text/css">
<title><?php echo "$titulo"; ?></title>
<script src="../includes/SpryValidationTextField.js" type="text/javascript"></script>
<link href="../includes/SpryValidationTextField.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="../includes/funcoes.js"></script>
<script language="javascript">  
function abrePopUp(url){  
window.open('../infos/roteiro_popup.php','ROTEIRO DE CARREGAMENTO','width=550,height=650');  
}</script>
</head>

<body>
<?php include("../includes/menu.php"); ?>
<form id="form1" action="cadastro_grava.php" method="post" name="cadastro">
<table width="1300" border="0" align="center" style="margin-top:80px;">
  <tr>
    <td height="78" align="center" class="titulo">CADASTRO DE EQUIPAMENTOS PARA LOJAS<BR></td>
  </tr>
  <tr>
    <td height="44" align="center"><br />
      <table width="1200" height="131" border="0" style="border: solid 1px #000;">
        <tr style="font-weight:bold;">
          <td width="100" height="49" align="center" valign="bottom">DATA:</td>
          <td width="100" align="center" valign="bottom">LOJA:</td>
          <td width="150" align="center" valign="bottom">EQUIPAMENTO:</td>
          <td width="150" align="center" valign="bottom">PROBLEMA ALEGADO:</td>
          <td width="100" align="center" valign="bottom">CAMINHÃO:</td>
          <td width="50" align="center" valign="bottom">CONTATO:</td>
          <td width="80" align="center" valign="bottom">N°NF:</td>
          <td width="100" align="center" valign="bottom">COBRADO:</td>
          <td width="100" align="center" valign="bottom">REC.DPD.:</td>
        </tr>
        <tr>
          <td align="center" valign="top"><span id="sprytextfield1"><input name="data" type="text" size="10" maxlength="10" onkeypress="Data(event, this)" value="<?php echo $dados[data]?>"/></span></td>
          <td align="center" valign="top"><span id="sprytextfield2"><input name="loja" type="text" size="3" maxlength="3" value="<?php echo $dados[loja]?>" /></span></td>
          <td align="center" valign="top"><span id="sprytextfield3"><input name="equipamento" type="text" size="30" maxlength="50" value="<?php echo $dados[equipamento]?>" /></span></td>
          <td align="center" valign="top"><span id="sprytextfield4"><input name="problema" type="text" size="30" maxlength="50" value="<?php echo $dados[problema]?>" /></span></td>
          <td align="center" valign="top"><span id="sprytextfield5"><input name="caminhao" type="text" size="10" maxlength="10" onkeypress="Data(event, this)" value="<?php echo $dados[caminhao]?>" /></span></td>
          <td align="center" valign="top"><span id="sprytextfield10"><input name="contato" type="text" size="15" maxlength="15" value="<?php echo $dados[contato]?>" /></span></td>
          <td align="center" valign="top"><span id="sprytextfield7"><input name="nf" type="text" size="6" maxlength="6" value="<?php echo $dados[nf]?>" /></span></td>
          <td align="center" valign="top"><span id="sprytextfield8"><input name="cobrado" type="text" size="10" maxlength="10" onkeypress="Data(event, this)" value="<?php echo $dados[cobrado]?>" /></span></td>
          <td align="center" valign="top"><span id="sprytextfield9"><input name="recebido" type="text" size="10" maxlength="10" onkeypress="Data(event, this)" value="<?php echo $dados[recebido]?>" /></span></td>
        </tr>
        <tr>
          <td colspan="9" align="left" valign="middle"><a href="#" onclick="javascript:window.open('../infos/roteiro_popup.php','ROTEIRO','width=550,height=650');"> > VERIFICAR ROTEIRO DE CARREGAMENTO</a></td>
          </tr>
      </table>
      <p><br />
        <input type="hidden" name="codigo" value="<?php echo $dados[id_atendimento]?>">
        <input type="hidden" name="codigo" value="<?php echo $dados[id_registro]?>">
        <?php
if ($_GET[id] > 0)
{
	echo '<input id="bt" type="submit" name="bt" value="ALTERAR"> ';
	echo '<input type="submit" name="bt" value="NOVO">';
}
else
	echo '<input id="bt" type="submit" name="bt" value="CADASTRAR"> ';
?>
        <input type="reset" name="limpar" id="limpar" value="LIMPAR" />
        </p>
      <p>
        <?php
    if ($_GET["msg"] == "sucesso") {
                echo "<h3>* EQUIPAMENTO CADASTRADO COM SUCESSO!</h3>";
	}
     else if ($_GET["msg"] == "erro") {
                echo "<h3>* ERRO AO CADASTRAR EQUIPAMENTO!</h3>";
	}
	  else if ($_GET["msg"] == "alterado") {
                echo "<h3>* EQUIPAMENTO ALTERADO COM SUCESSO!</h3>";
	}
?>
        <br />
      </p></td>
  </tr>
</table>
</form>
<script type="text/javascript">
<!--
var sprytextfield1 = new Spry.Widget.ValidationTextField("sprytextfield1", "date", {format:"dd/mm/yyyy"});
var sprytextfield2 = new Spry.Widget.ValidationTextField("sprytextfield2");
var sprytextfield3 = new Spry.Widget.ValidationTextField("sprytextfield3");
var sprytextfield4 = new Spry.Widget.ValidationTextField("sprytextfield4");
var sprytextfield5 = new Spry.Widget.ValidationTextField("sprytextfield5", "date", {format:"dd/mm/yyyy"});
var sprytextfield6 = new Spry.Widget.ValidationTextField("sprytextfield6", "date", {format:"dd/mm/yyyy"});
var sprytextfield7 = new Spry.Widget.ValidationTextField("sprytextfield7");
var sprytextfield8 = new Spry.Widget.ValidationTextField("sprytextfield8", "date", {isRequired:false, format:"dd/mm/yyyy"});
var sprytextfield9 = new Spry.Widget.ValidationTextField("sprytextfield9", "date", {isRequired:false, format:"dd/mm/yyyy"});
var sprytextfield10 = new Spry.Widget.ValidationTextField("sprytextfield10", "none", {isRequired:false});
//-->
</script>
</body>
</html>

Compartilhar este post


Link para o post
Compartilhar em outros sites

Altere isso:

 $sql2 = "UPDATE atendimento SET conclusao = '3' WHERE id_atendimento = $_POST[codigo] ";
Para isso:

 $sql2 = "UPDATE atendimento SET conclusao = '3' WHERE id_atendimento = '{$_POST['codigo']}' ";

Abraços...

Compartilhar este post


Link para o post
Compartilhar em outros sites

Lucas, valeu pela dica, mas resolvi o problema de outra maneira:

 

Adicionei um campo hidden no cadastro de equipamento:

 

<input type="hidden" name="codigoat" value="<?php echo $dados[id_atendimento]?>">

e deixei o cadastro assim:

 

if ($_POST[bt] == 'CADASTRAR')
{
	$sql = "INSERT INTO filiais (data, loja, equipamento, problema, caminhao, contato, nf, cobrado, recebido) VALUES ('".$_POST["data"]."', '".$_POST["loja"]."', '".$_POST["equipamento"]."', '".$_POST["problema"]."', '".$_POST["caminhao"]."', '".$_POST["contato"]."', '".$_POST["nf"]."', '".$_POST["cobrado"]."', '".$_POST["recebido"]."')";
	query($sql);
	$sql = "UPDATE atendimento SET conclusao = '1' WHERE id_atendimento = $_POST[codigoat] ";
	query($sql);
	
	header("Location: cadastro.php?msg=sucesso");

}

Não sei se é a melhor maneira, mas funcionou. Valeu ae!

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.