the table does not receive registration

0
Good afternoon, I'm trying to make a PHP Code that involves Database, and in the course of the code I came across an error which I can not solve. when he tries to register a data through the web, he does not register in any way , alreadywhenItrybythepromtofcomadostheregisterofsuccess

BelowisthecodeI'musing.

index.php

<?php
 session_start();
 if(isset($_SESSION['id'])==0 and isset($_SESSION['nome'])==0){
  echo "<script> window.location='login.php'</script>";
}else{
  $id=$_SESSION['id'];
  $nome=$_SESSION['nome'];
  echo "<h3>usuario: $nome <a href='login.php'>sair</a></h3>";
}
?>
<HTML>
  <HEAD>
    <meta charset="utf-8">
     <TITLE>Cadastros</TITLE>
 <style>
 h1{
    color:#8080FF;
    background-color:#FF6A6A;
    margin-top:-7;
    text-shadow:1px 2px 3px #800080;
    padding:25px;
    font-size:60px;
    margin-left:-8;
    margin-right:-1;
    }
    input[type=submit],input[type=text]{
    background-color:#FF6A6A ;
    border:2px solid #800080;
    border-radius:9px;
    color:#fff;
    }
    fieldset{
    margin-top:-40;
    background-color:#FFFFC0;
     margin-left:-8;
    margin-right:-1;
    }body{
    background-color:#8080FF;
    }#p1{
    color:green;
    }#p2{
    color:red;
    }th{
    color:#800080;
    }td{
    color:#000080;
    }#p3{
    color:#FF00FF;
    }#p4{
    color:#800000;
    }  h3{
    font-size:30px;
    color:#FFFFFF;
    margin-top:90;
    }
 </style>
</HEAD>
<BODY>
<center>
 <h1>Cadastro de Produtos</h1>
 <fieldset>
  <BR>
  <table >
  <form action ="evento.php?cadastro=true" method = "post" >
<tr>
  <td><label>Produto:</label></td>
  <td><input type="text" value='' name='produto' placeholder='Digite o nome do produto' required ></td>
</tr>
<tr>
  <td><label>Preço:</label></td>
  <td><input type="text" value ='' name='preco' placeholder='Digite o preço do produto' required ></td>
</tr>
</table>
 <BR>
 <input type="submit" value="cadastrar">
 </form>
 <BR>
 <BR>
 <table border="5">
 <thead>
<tr>
  <th>id</th>
  <th>Produto</th>
  <th>Preço</th>
  <th>Funcionário</th>
  <th>Alterar</th>
  <th>Excluir</th>
</tr>
</thead>

<center>
 <?php
            include_once "conexao.php";
            $sql = "select * from produto";
            $result = mysql_query($sql,$con);
            if($result){
            while($linha = mysql_fetch_array($result)){
?>
<tbody>
       <tr>
           <td id='p3'> <?php  echo $linha['codigo'];?></td>
           <td> <?php echo $linha['nome'];?></td>
           <td id='p4'> <?php echo $linha['preco'];?></td>
           <td> <?php echo $linha['id_funcionario'];?></td>       
           <td> <?php echo "<a id='p1' href = editar.php?editar=".$linha['codigo']." >editar </a>";?></td>
           <td> <?php echo "<a id='p2' href = evento.php?deletar=".$linha['codigo'].">excluir</a>";?></td>
       <tr>
</tbody>
<?php
          }//fim do while
          }//fim do if
          mysql_close($con);
?>
</table> 
</fieldset>
<h3 align="center">&copy Amem!</h3>
<BR>

</BODY>
</HTML>

login.php

<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	 <style>
 h1{
    color:#8080FF;
    background-color:#FF6A6A;
    margin-top:-7;
    text-shadow:1px 2px 3px #800080;
    padding:25px;
    font-size:60px;
    margin-left:-8;
    margin-right:-1;
    }
    input[type=submit],input[type=text]{
    background-color:#FF6A6A;
    border:2px solid #800080;
    border-radius:9px;
    color:#fff;
    }
    input[type=submit],input[type=password]{
    background-color:#FF6A6A;
    border:2px solid #800080;
    border-radius:9px;
    color:#fff;
    }
    fieldset{
    margin-top:-40;
    background-color:#FFFFC0;
     margin-left:-8;
    margin-right:-1;
    }body{
    background-color:#8080FF;
    }#p1{
    color:green;
    }#p2{
    color:red;
    }th{
    color:#800080;
    }td{
    color:#000080;
    }#p3{
    color:#FF00FF;
    }#p4{
    color:#800000;
    }  h3{
    font-size:30px;
    color:#FFFFFF;
    margin-top:90;
    }
 </style>
</head>
<body>
	<center>
<h1>Sistema Controle de Produtos</h1>
<fieldset>
<form method="post" action="">
	Login:
	<input type="text" name="login" required><br><br>
	Senha:
	<input type="password" name="senha" required><br><br>
 	<input type="submit" value="Entrar">
 	</center>
</form>
</fieldset>
<h3 align="center">&copy Amem!</h3>
<?php
include_once "conexao.php";
if(isset($_POST['login']))$login = $_POST['login'];
if(isset($_POST['senha']))$senha = $_POST['senha'];
$sql="select * from funcionario where nome='$login' and senha='$senha'";
$result=mysql_query($sql,$con);
$res=mysql_num_rows($result);
if($res){
	$linha=mysql_fetch_array($result);
	session_start();
	$_SESSION['id']=$linha['id_funcionario'];
	$_SESSION['nome']=$linha['nome'];
	echo "<script> window.location=' index.php'</script>";		
}
?>
</body>
</html>

connection.php

<HTML>
<HEAD>
 <TITLE>Conexao</TITLE>
</HEAD>
<BODY>
<?php
  $host="localhost";
  $login="root";
  $senha="";
  $banco="controle_de_estoque";

  $con=mysql_connect($host,$login,$senha)or die("Erro ao conecta!".mysql_error());
  $sql="create database if not exists ".$banco;
  mysql_query($sql,$con);
  mysql_select_db($banco,$con);
  $sql = "create table if not exists funcionario(
  id_funcionario int(8) auto_increment,
  nome varchar(100) not null,
  senha varchar(50) not null,
  primary key(id_funcionario))";
  mysql_query($sql,$con);
  $sql="create table if not exists produto(codigo int(8) auto_increment,
  nome varchar(100) not null, preco double not null, id_funcionario int(8) not null,
  primary key(codigo), foreign key(id_funcionario) references funcionario(id_funcionario))";
  mysql_query($sql);
?>

</BODY> 
</HTML>

evento.php

<?php
 include_once'conexao.php';
 if(isset($_POST['produto']))$atrProduto = $_POST['produto'];
 if(isset($_POST['preco']))$atrPreco = $_POST['preco'];
 if(isset($_GET['cadastro']))
 {
 $sql = "insert into produto values(0,'".$atrProduto."','".$atrPreco."')";
 mysql_query($sql,$con);
 echo "<script>alert('Produto cadastrado com sucesso!')</script>";
 }
 if(isset($_GET['deletar'])){
 $sql = "delete from produto where codigo = ".$_GET['deletar'];
 mysql_query($sql,$con);
 echo "<script>alert('Produto deletado com sucesso!')</script>";
 }
 if(isset($_GET['editar'])){
 $sql = "update produto set nome= '".$atrProduto."',preco='".$atrPreco."' where codigo=".$_GET['editar'];
 mysql_query($sql,$con);
 echo"<script> alert('Produto atualizado com sucesso')</script>";
 }
 echo"<script>window.location='index.php'</script>";

edit.php

<html>
<head>
	<meta charset="utf-8">
</head>
<body>
	<?php
	include_once 'conexao.php';
	$sql='select * from produto where codigo='.$_GET['editar'];
	$result=mysql_query($sql,$con);
	$linha=mysql_fetch_array($result);
	?>
	<h3> Editar Contato </h3>
	<?php echo "<form action= 'evento.php?editar=".$linha['codigo']."'method='post'>";?>
		Produto: <input type="text" name="produto" value="<?php echo $linha['nome'];?>"><p></p>
		Preço: <input type="text" name="preco" value="<?php echo $linha['preco'];?>"><p></p>
		<input type="submit" value="Editar">
	</form>
</body>
</html>

logout.php

<?php
 session_star();
 unset($_SESSION['id']);
 unset($_SESSION['nome']);
 session_destroy();
 echo "<script> window.location='login.php'</script>";
 ?>
.

Someone could help me find my error, I thought I'd put it in the event.php $sql = "insert into produto values(0,'".$atrProduto."','".$atrPreco."',1)"; that way, but I was going to register everything in User 1, and I wanted to when I changed the session and put it, let's say a user who in id 2, the id at the time of registration appears 2 and so on. could anyone help me at this point too? I really do not have any idea what to do. I really need the help of my friends. Thank you in advance.

    
asked by anonymous 02.01.2018 / 21:06

1 answer

0

The error is in line mysql_query($sql,$con);

By looking at the structure of your table, we can see that the id_funcionario field is of type not null , so you should fill in a value number.

You can do:

$userId = isset($_SESSION["id"]) ? $_SESSION["id"] : 1;

$sql = sprintf("INSERT INTO 'produto' SET 'nome' = \"%s\", 'preco' = \"%s\", 'id_funcionario' = \"%d\";",
    $atrProduto,
    $atrPreco,
    $userId);

If you are still unable to register the product, use the mysql_error function below insert to view the problem.

die( mysql_error($link) );

Ps: Just remembering that mysql_ is no longer used. If possible, switch to mysqli_

    
02.01.2018 / 21:22