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

Problema para inserir dados web no banco


Raio da Noite

Pergunta

Bom dia a todos, venho pedir encarecidamente ajuda com erro na seguinte pagina:

<?php include "conexao.php"; 
extract($_REQUEST);

$acao = $_POST["acao"];
$id = $_POST["id"];

$txt_id_categoria        = $_POST["txt_id_categoria"];
$txt_id_subcategoria     = $_POST["txt_id_subcategoria"];
$txt_categoria            = $_POST["txt_categoria"];
$txt_subcategoria         = $_POST["txt_subcategoria"];
$txt_produto             = $_POST['txt_produto']; 
$txt_foto                 = $_POST["txt_foto"];
$txt_descricao             = $_POST["txt_descricao"];


if ( $acao =="Inserir" ){ 
$sql = "INSERT INTO produtos (id_categoria, id_subcategoria, produto, foto, descricao) VALUES ('$txt_id_categoria', '$txt_id_subcategoria', '$txt_produto', '$txt_foto, '$txt_descricao')";
mysql_query($sql) or die ("Não foi possível Inserir dados"); 
}

if($acao =="Alterar"){ 
$sql = "UPDATE produtos SET
id_categoria        = $'txt_id_categoria',
id_subcategoria        = $'txt_id_subcategoria',
produto                = $'txt_produto',
foto                = $'txt_foto',
descricao            = $'txt_descricao'
WHERE id_produto = '$id'";
mysql_query($sql) or die ("Não foi possível Inserir dados");
}

if($_GET['Excluir'] == "ok"){ 
$id = $_GET['id']; 
mysql_query ("DELETE FROM produtos WHERE id_produto ='$id'")or die (mysql_error()); 
}else{ 
}
?>

<!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>Untitled Document</title>
</head>

<body>
<table width="900" border="0" align="center">
  <tr>
    <td colspan="2"><?php include "cabecalho.php"; ?></td>
  </tr>
  <tr>
    <td width="197"><?php include "menu.php"; ?></td>
    <td width="693"><table width="100%" border="0">
      <tr>
        <td><div align="center">
          <p>Produtos</p>
          
          <?php
            $acao = $_GET["acao"];
            $id = $_GET["id"];
            
            if ($acao != "")
            {
          $sql = "select p.*, c.*, s.* from produtos p, categorias c, subcategorias s where p.id_categoria = c.id_categoria and p.id_subcategoria = s.id_subcategoria and p.id_produto = '$id'"; 
          $resultado = mysql_query ($sql);
          $linha = mysql_fetch_array($resultado);
          
          $produto             = $linha["produto"];
          $foto             = $linha["foto"];
          $descricao         = $linha["descricao"];
          $subcategoria     = $linha["subcategoria"];
          $categoria         = $linha["categoria"];
          $id_subcategoria     = $linha["id_subcategoria"];
          $id_categoria     = $linha["id_categoria"];
          
          ?>
          
          <form id="form1" name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
            <table width="475" border="1" align="center" cellpadding="2">
              <tr>
                <td width="93">Categoria:</td>
                <td width="310">
                  <select name = "txt_id_categoria" id="txt_id_categoria">
                    <?php
                        $sql_cat = "select * from categorias order by categoria";
                        $result = mysql_query($sql_cat);
                
                        while ($registro = mysql_fetch_array($result))
                        {
                            $valor = $registro["id_categoria"];
                            if ($id_categoria == $valor)
                                $selecionado = "selected";
                            else
                                $selecionado = "";
                                
                        print "<option value = \"$valor\"$selecionado> $registro[categoria] </option>";
                        }
                ?>
                  </select>                  </td>
              </tr>
              <tr>
                <td width="93">SubCategoria:</td>
                <td width="310">
                  <select name = "txt_id_subcategoria" id="txt_id_subcategoria">
                    <?php
                        $sql_subcat = "select * from subcategorias order by subcategoria";
                        $result = mysql_query($sql_subcat);
                
                        while ($registro = mysql_fetch_array($result))
                        {
                            $valor = $registro["id_subcategoria"];
                            if ($id_categoria == $valor)
                                $selecionado = "selected";
                            else
                                $selecionado = "";
                                
                        print "<option value = \"$valor\"$selecionado> $registro[subcategoria] </option>";
                        }
                ?>
                  </select>                  </td>
              </tr>
              <tr>
                <td width="93">Produto:</td>
                <td>
                  <input name="txt_produto" type="text" id="txt_produto" value= "<?php echo $produto; ?>" size="35" maxlength="100" /></td>
              </tr>
              <tr>
                <td width="93">Foto:</td>
                <td><input name="txt_foto" type="text" id="txt_foto" value= "<?php echo $foto; ?>"/></td>
              </tr>
              <tr>
                <td width="93">Descrição:</td>
                <td><textarea name="txt_descricao" cols="35" rows="5" id="txt_descricao"><?php echo $descricao; ?></textarea></td>
              </tr>
              
              <tr>
                <td colspan="2"><label>
                  <div align="right">
                    <input type="hidden" name="id" value= "<?php echo $id ?>" />
                    <input type="hidden" name=" <?php echo $acao ?>" value ="ok" />
                    <input type="submit" name="Submit" value="OK" />
                    </div>
                </label></td>
                </tr>
            </table>
              </form>
              <?php } else { ?>
          <table width="99%" border="1">
            <tr>
              <td width="26%"><div align="center">Categorias:</div></td>
              <td width="26%"><div align="center">SubCategorias:</div></td>
              <td width="36%"><div align="center">Produtos:</div></td>
              <td colspan="2"><div align="center">Ação:</div></td>
              </tr>
              <?php 
              $sql = "select p.*, c.*, s.* from produtos p, categorias c, subcategorias s where p.id_categoria = c.id_categoria and p.id_subcategoria = s.id_subcategoria order by p.produto";
              $resultado = mysql_query($sql);
              while ($coluna = mysql_fetch_array($resultado)) { ?>
              
            <tr>
              <td><?php echo $coluna["categoria"] ?></td>
              <td><?php echo $coluna["subcategoria"] ?></td>
              <td><?php echo $coluna["produto"] ?></td>
              <td width="6%"><div align="center"><a href="?acao=Alterar&id=<?php echo $coluna["id_produto"];?>" > <img src="img/editar.png" alt="Editar" width="30" height="30" border = "0" /></a></div></td>
              <td width="6%"><div align="center"><a href="?Excluir=ok&id=<?php echo $coluna["id_produto"];?>" > <img src="img/excluir.png" alt="Excluir" width="30" height="30" border = "0" /></a></div></td>
            </tr>
            <?php } ?>
            <tr>
              <td colspan="5"><div align="right"><a href="?acao=Inserir"> <img src="img/inserir.png" alt="Inserir" width="80" height="25" border = "0" /></a></div></td>
              </tr>
          </table>
          <?php } ?>
          <p> </p>
        </div></td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td colspan="2"><div align="center">
      <?php include "rodape.php"; ?>
    </div></td>
  </tr>
</table>

</body>
</html>

É a pagina da loja virtual que estou montando, consigo ver os dados na tabela pelo navegador do produto que cadastrei no banco via php myadmin, mas ao inserir dados ou alterar ele volta pra tabela sem ter inserido ou alterado e sem apresentar mensagem de erro somente ignora o que fiz, alguém pode me ajudar?

desde já muito obrigado!

Link para o comentário
Compartilhar em outros sites

2 respostass a esta questão

Posts Recomendados

  • 0

já verificou se esta passando o 'acao' ??

vi no codigo que tem

$acao = $_POST["acao"]; e $acao = $_GET["acao"];

precisa definir metodo esta usando para receber oconteudo que vai fazer e receber com o mesmo

se for <form method='post' action='pagina.php?acao=incluir'>

receba o acao com o GET no lugar do post

Link para o comentário
Compartilhar em outros sites

  • 0

problema resolvido com a sua dica muito obrigado, segue código correto para que o tópico fique completo:

<?php include "conexao.php"; 
extract($_REQUEST);

$id = $_POST["id"];

$txt_id_categoria        = $_POST["txt_id_categoria"];
$txt_id_subcategoria     = $_POST["txt_id_subcategoria"];
$txt_categoria            = $_POST["txt_categoria"];
$txt_subcategoria         = $_POST["txt_subcategoria"];
$txt_produto             = $_POST['txt_produto']; 
$txt_foto                 = $_POST["txt_foto"];
$txt_descricao             = $_POST["txt_descricao"];


$Inserir = $_POST['Inserir']; 
if($Inserir =="ok"){
$sql = "INSERT INTO produtos (id_categoria, id_subcategoria, produto, foto, descricao) VALUES ('$txt_id_categoria', '$txt_id_subcategoria', '$txt_produto', '$txt_foto', '$txt_descricao')";
mysql_query($sql) or die ("Não foi possível Inserir dados"); 
}

$Alterar= $_POST['Alterar'];
if ($Alterar == "ok"){
$sql = "UPDATE produtos SET
id_categoria        = '$txt_id_categoria',
id_subcategoria        = '$txt_id_subcategoria',
produto                = '$txt_produto',
foto                = '$txt_foto',
descricao            = '$txt_descricao'
WHERE id_produto = '$id'";
mysql_query($sql) or die ("Não foi possível Inserir dados");
}

if($_GET['Excluir'] == "ok"){ 
$id = $_GET['id']; 
mysql_query ("DELETE FROM produtos WHERE id_produto ='$id'")or die (mysql_error()); 
}else{ 
}
?>

<!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>Untitled Document</title>
</head>

<body>
<table width="900" border="0" align="center">
  <tr>
    <td colspan="2"><?php include "cabecalho.php"; ?></td>
  </tr>
  <tr>
    <td width="197"><?php include "menu.php"; ?></td>
    <td width="693"><table width="100%" border="0">
      <tr>
        <td><div align="center">
          <p>Produtos</p>
          
          <?php
            $acao = $_GET["acao"];
            $id = $_GET["id"];
            
            if ($acao != "")
            {
          $sql = "select p.*, c.*, s.* from produtos p, categorias c, subcategorias s where p.id_categoria = c.id_categoria and p.id_subcategoria = s.id_subcategoria and p.id_produto = '$id'"; 
          $resultado = mysql_query ($sql);
          $linha = mysql_fetch_array($resultado);
          
          $produto             = $linha["produto"];
          $foto             = $linha["foto"];
          $descricao         = $linha["descricao"];
          $subcategoria     = $linha["subcategoria"];
          $categoria         = $linha["categoria"];
          $id_subcategoria     = $linha["id_subcategoria"];
          $id_categoria     = $linha["id_categoria"];
          
          ?>
          
          <form id="form1" name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
            <table width="475" border="1" align="center" cellpadding="2">
              <tr>
                <td width="93">Categoria:</td>
                <td width="310">
                  <select name = "txt_id_categoria" id="txt_id_categoria">
                    <?php
                        $sql_cat = "select * from categorias order by categoria";
                        $result = mysql_query($sql_cat);
                
                        while ($registro = mysql_fetch_array($result))
                        {
                            $valor = $registro["id_categoria"];
                            if ($id_categoria == $valor)
                                $selecionado = "selected";
                            else
                                $selecionado = "";
                                
                        print "<option value = \"$valor\"$selecionado> $registro[categoria] </option>";
                        }
                ?>
                  </select>                  </td>
              </tr>
              <tr>
                <td width="93">SubCategoria:</td>
                <td width="310">
                  <select name = "txt_id_subcategoria" id="txt_id_subcategoria">
                    <?php
                        $sql_subcat = "select * from subcategorias order by subcategoria";
                        $result = mysql_query($sql_subcat);
                
                        while ($registro = mysql_fetch_array($result))
                        {
                            $valor = $registro["id_subcategoria"];
                            if ($id_categoria == $valor)
                                $selecionado = "selected";
                            else
                                $selecionado = "";
                                
                        print "<option value = \"$valor\"$selecionado> $registro[subcategoria] </option>";
                        }
                ?>
                  </select>                  </td>
              </tr>
              <tr>
                <td width="93">Produto:</td>
                <td>
                  <input name="txt_produto" type="text" id="txt_produto" value= "<?php echo $produto; ?>" size="35" maxlength="100" /></td>
              </tr>
              <tr>
                <td width="93">Foto:</td>
                <td><input name="txt_foto" type="text" id="txt_foto" value= "<?php echo $foto; ?>"/></td>
              </tr>
              <tr>
                <td width="93">Descri&ccedil;&atilde;o:</td>
                <td><textarea name="txt_descricao" cols="35" rows="5" id="txt_descricao"><?php echo $descricao; ?></textarea></td>
              </tr>
              
              <tr>
                <td colspan="2"><label>
                  <div align="right">
                    <input type="hidden" name="id" value= "<?php echo $id ?>" />
                    <input type="hidden" name=" <?php echo $acao ?>" value ="ok" />
                    <input type="submit" name="Submit" value="OK" />
                    </div>
                </label></td>
                </tr>
            </table>
              </form>
              <?php } else { ?>
          <table width="99%" border="1">
            <tr>
              <td width="26%"><div align="center">Categorias:</div></td>
              <td width="26%"><div align="center">SubCategorias:</div></td>
              <td width="36%"><div align="center">Produtos:</div></td>
              <td colspan="2"><div align="center">A&ccedil;&atilde;o:</div></td>
              </tr>
              <?php 
              $sql = "select p.*, c.*, s.* from produtos p, categorias c, subcategorias s where p.id_categoria = c.id_categoria and p.id_subcategoria = s.id_subcategoria order by p.produto";
              $resultado = mysql_query($sql);
              while ($coluna = mysql_fetch_array($resultado)) { ?>
              
            <tr>
              <td><?php echo $coluna["categoria"] ?></td>
              <td><?php echo $coluna["subcategoria"] ?></td>
              <td><?php echo $coluna["produto"] ?></td>
              <td width="6%"><div align="center"><a href="?acao=Alterar&id=<?php echo $coluna["id_produto"];?>" > <img src="img/editar.png" alt="Editar" width="30" height="30" border = "0" /></a></div></td>
              <td width="6%"><div align="center"><a href="?Excluir=ok&id=<?php echo $coluna["id_produto"];?>" > <img src="img/excluir.png" alt="Excluir" width="30" height="30" border = "0" /></a></div></td>
            </tr>
            <?php } ?>
            <tr>
              <td colspan="5"><div align="right"><a href="?acao=Inserir"> <img src="img/inserir.png" alt="Inserir" width="80" height="25" border = "0" /></a></div></td>
              </tr>
          </table>
          <?php } ?>
          <p>&nbsp;</p>
        </div></td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td colspan="2"><div align="center">
      <?php include "rodape.php"; ?>
    </div></td>
  </tr>
</table>

</body>
</html>

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,2k
    • Posts
      652k
×
×
  • Criar Novo...