the table does not receive registration
-
Good Late friends, I'm trying to make a php code that involves Database, and in the course of the code I came across an error that I can't solve. when trying to register a data by the web, it does not register in any way
, ja when I try for the promt of comados the record of success
below follows the code I'm using.
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">© 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">© 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>conexa.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>event.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 mistake, I thought about putting in the event. php
$sql = "insert into produto values(0,'".$atrProduto."','".$atrPreco."',1)";
that way they would stay registering everything in user 1, and I wanted for when I changed session and put it we will tell a user who ta in ID 2, the id at the time of registering appears 2 and so on. could anyone help me at this tbm point? I really have no idea what you want to do. I really need the help of my friends. I appreciate it right away.
-
The error is on the line
mysql_query($sql,$con);
Taking a look at the structure of your table, we can see that the field
id_funcionario
It's the kindnot null
, therefore you must fill a number value.You can do:
$userId = isset($_SESSION["id"]) ? $_SESSION["id"] : 1;
$sql = sprintf("INSERT INTO
produto
SETnome
= "%s",preco
= "%s",id_funcionario
= "%d";",
$atrProduto,
$atrPreco,
$userId);
If you are not able to register the product, use the function
mysql_error
belowinsert
to view the problem.die( mysql_error($link) );
Ps.: Just remembering that
mysql_
is no longer used. If possible change tomysqli_