Jump to content


Loh2

Member Since 22/05/2016
Offline Last Active 22/05/2016, 18:20
-----

Topics I've Started

Erro ao salvar FK

22/05/2016, 18:19

Pessoal,

 

Tenho o código abaixo, mas quando clico em salvar, dá erro na gravação. O que pode ser?

 

TABELA NO BANCO

CREATE TABLE tab_entradaestoque(

      id int auto_increment primary key not null,

      id_produto int not null,

id_fornecedor int not null,

valorunitario float(10) not null,

quantidade int not null,

nfiscal int not null,
      data_entrada date not null

FOREIGN KEY (id_fornecedor) references tab_fornecedor(id_fornecedor),
      FOREIGN KEY (id_produto) references tab_produto(id_produto)

);

ENTRADAPRODUTOESTOQUE.PHP

<?php
include 'conexao.php';
$data = date('d-m-Y');

// Recebe o id do produto via GET
$id_cliente = (isset($_GET['id'])) ? $_GET['id'] : '';

// Valida se existe um id e se ele é numérico
if (!empty($id_cliente) && is_numeric($id_cliente)):

    // Captura os dados do produto solicitado
    $conexao = conexao::getInstance();
    $sql = "SELECT * FROM tab_produto WHERE id_produto = :id";
    $stm = $conexao->prepare($sql);
    $stm->bindValue(':id', $id_cliente);
    $stm->execute();
    $cliente = $stm->fetch(PDO::FETCH_OBJ);

endif;

?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="css/custom.css">
</head>
<body>
    <div class='container'>
        <fieldset>
            <legend><h1>Formulário - Registrar Entrada - Estoque</h1></legend>
            
            <?php if(empty($cliente)):?>
                <h3 class="text-center text-danger">Cliente não encontrado!</h3>
            <?php else: ?>
                <form action="action_entradaestoque.php" method="post" id='form-contato' enctype='multipart/form-data'>
                    
                <div class="form-group">
                  <label for="nome">Nome</label>
                  <input type="text" class="form-control" id="nome" name="nome" value="<?=$cliente->nome_produto?>" placeholder="Infome o Nome">
                  <span class='msg-erro msg-nome'></span>
                </div>
                
                <div class="form-group">
                  <label for="valor">Valor Unitário</label>
                  <input type="valor" class="form-control" id="valor" name="valor" placeholder="Informe o Valor Unitário">
                  <span class='msg-erro msg-valor'></span>
                </div>
                
                <div class="form-group">
                  <label for="quantidade">Quantidade </label>
                  <input type="quantidade" class="form-control" id="quantidade" name="quantidade" placeholder="Informe a Quantidade Adquirida"disable>
                  <span class='msg-erro msg-quantidade'></span>
                </div>
                
                <div class="form-group">
                  <label for="nfiscal">Nº Nota Fiscal </label>
                  <input type="nfiscal" class="form-control" id="nfiscal" maxlength="15" name="nfiscal" placeholder="Informe o Nº da Nota Fiscal de Compra">
                  <span class='msg-erro msg-nfiscal'></span>
                </div>

                <div class="form-group">
                    <label for="categoria">Fornecedor</label>
                    <select class="form-control" name="categoria" id="categoria">
                        <option value="">Selecione o Fornecedor</option>
                        
                    <?
                    
                    $sql2= mysql_query("SELECT id_fornecedor, nome_fornecedor FROM tab_fornecedor order by nome_fornecedor") or die("Erro");
                    while ( $query2 = mysql_fetch_array($sql2))
                            {
                                    $id_fornecedor = $query2['id_fornecedor'];
                                    echo "<option value='".$query2['id_fornecedor']."'>".$query2['nome_fornecedor']."</option>";                                
                            }

                    ?>            
                        
                    </select>
                </div>
                    
                    <input type="hidden" name="acao" value="editar">
                    <input type="hidden" name="ids" value="<?=$query2->id_fornecedor?>">
                    <input type="hidden" name="id" value="<?=$cliente->id?>">
                    <button type="submit" class="btn btn-primary" id='botao'> 
                      Gravar
                    </button>
                    <a href='indexprodutoestoque.php' class="btn btn-danger">Cancelar</a>
                </form>
            <?php endif; ?>
        </fieldset>

    </div>
    <script type="text/javascript" src="js/custons.js"></script>
</body>
</html>

 

 

 

 

ACTION_ENTRADAESTOQUE.PHP

 

<?

    include "conexao.php";

        // Recebe os dados enviados 
        $acao  = $_POST['acao'];
        $nome = $_POST['nome'];
        $id  = $_POST['id'];
        $valor  = $_POST['valor'];
        $idf  = $_POST['ids'];
        $quantidade = $_POST['quantidade'];
        $nfiscal = $_POST['nfiscal'];
        $categoria = $_POST['categoria'];

        // Valida os dados recebidos
        
        
        
        if ($nome == '' || strlen($nome) < 3){
                $mensagem .= '<li>Favor preencher o Nome.</li>';
        
        }

        elseif($categoria == ''){
        $mensagem .= '<li>Favor preencher a Categoria.</li>';
        
    }
        elseif($nfiscal == ''){
        $mensagem .= '<li>Favor preencher o Número da Nota Fiscal de Compra.</li>';
        
    }
        elseif($quantidade == ''){
        $mensagem .= '<li>Favor preencher a Quantidade.</li>';
        
    }
        elseif($valor == ''){
        $mensagem .= '<li>Favor preencher o Valor Unitário.</li>';
        
    }
    
        else{

            $sql = "insert into tab_entradaestoque(id_produto, valorunitario, quantidade, nfiscal, id_fornecedor, dataentrada) 
                values ('$id','$valor','$quantidade','$nfiscal', '$idf', 'now()')";    
            mysql_query($sql) or die ("Erro na tentativa de gravação");    

 

 

 

 


CRIAR ARRAY E ADICIONAR EM UMA TABLE

22/05/2016, 16:41

Pessoal, sou iniciante em php e estou precisando de ajuda.
Como faço para pegar o input de um usuário, adicioná-lo em um array e exibi-lo em uma table?

Tenho esse código:

 

 

    <form  method = "post">
        <div class="form-group">
            <label style="margin-left: 35px;">• Produto </label>
            
                
                    <select id="cboProduto" name= "produto" style="margin-left: 45px; width: 210px; height: 30px;padding-left:8px;">
                    <?php 
                        $query = 'SELECT * from tab_produto where flag_produto LIKE "venda" order by nome_produto';
                        $result = mysql_query($query) or die("Falha na execução da consulta");
                        while($row1 = mysql_fetch_assoc($result))
                        {                
                            $produto = $row1['id_produto'];
                            $nomeproduto = $row1['nome_produto'];
                    ?>                        
                            <option value="<?php echo $produto;?>"><?php echo $nomeproduto;?></option>
                            <?php 
                        }     ?>    
                    </select>
                    
                    
                    <?php
                        $sql = "SELECT * FROM tab_produto WHERE id_produto = '$produto'";
                        $rs = mysql_query($sql);        
                        while($reg = mysql_fetch_array($rs))
                        {    
                            $produtos[] = array('nome_produto' => $reg['nome_produto'], 'valor_produto' => $reg['valor_produto']);
                        }
                    ?>

            <label style="margin-left: 35px;">• Quantidade</label>
            <td><input style="margin-left: 15px; width: 100px; height: 30px;padding-left:10px;" name="quantidade" type="text" id="txtQuant" >
            <button class="btn btn-primary" style="margin-left: 15px" type="submit" id="btnAdicionar"> + </button>
            <a href="EfetuarVenda.php" class="btn btn-danger" role="button">Nova Venda</a>
            
        </div>                
    </form>
            
    
    <hr>
        <div class="container"style="margin-left:30px; width:1000px;">
            <div class="row">
                <div class="col-xs-12">
                    <div class="table-responsive">
                        <table class="table table-bordered">
                            <thead>
                                <tr class="active">
                                    <th style="width:115px; padding-left:40px;">Data</th>
                                    <th style="width:180px; padding-left:60px;">Produto</th>
                                    <th style="width:115px; padding-left:18px;">Quantidade</th>
                                    <th style="width:125px; padding-left:15px;">Preço Unitário</th>
                                    <th style="width:135px; padding-left:10px;">Total do Produto</th>
                                    <th style="width:135px; padding-left:10px;">Total Compra</th>
                                    <?php 
                                    $data = date("Y-m-d");
                                    $soma = 0;
                                    $dati = explode("-", $dato[$j]);
                                    $dataconvertida[$j]=$dati[2]."/".$dati[1]."/".$dati[0];
                                    $j=1;
                                    
                                    ?>
                                </tr>
                            </thead>
                            
                            <tbody>
                                <tr class="success">

                                    <td style="padding-left:15px;"><?php echo $dataconvertida[$i];?></td>
                                    <td style="padding-left:30px;"><?php echo $produtos[$cont][nome_produto]?></td></td>
                                    <td style="padding-left:30px;"><?php echo $quantidade[$i];?></td></td>
                                    <td style="padding-left:35px;"> R$<?php echo $produtos[$cont][valor_produto];?></td></td>
                                    <td style="padding-left:35px;"> R$<?php echo $produtos[$cont][valor_produto]*$quantidade[$i];?></td></td>
                                    <td style="padding-left:30px;"> R$ <?php $preço = (float)$preço; $total = (float)$total; $soma=(float)$soma; $total = $quantidade[$i]*$preço;  $soma = $soma + $total; echo $soma;?></td></td>
                                    <td><a href="excluirVenda.php?produto=<?php echo $codigoprodutoinserido[$i];?><?php echo $produto[$i];?>&cpf=<?php echo $cpfselecionado;?>&data=<?php echo $dato[$i];?>&quantidade=<?php echo $quantidade[$i];?>" class="btn btn-danger" role="button">Excluir Produto</a></td></td>
                                </tr>
                            </tbody>
                            
                        </table>
                    </div>
                </div>
            </div>
        </div>


IPB Skin By Virteq