the table does not receive registration

Asked

Viewed 51 times

0

Good afternoon friends, I’m doing trying to make a php code that involves database, and in the course of the code I came across an error that I can not solve. when trying to register a data on the web, it does not register in any way inserir a descrição da imagem aqui inserir a descrição da imagem aqui, already when I try for the promt of comados the registration of success

below follows the codes I am 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">&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>

php connection.

<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>

php event.

<?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>";
 ?>
.

could someone help me find my mistake, I thought to put in the event.php $sql = "insert into produto values(0,'".$atrProduto."','".$atrPreco."',1)"; this way however would be registering everything on user 1, and I wanted for when I changed session and put let’s say a user who is in id 2, the id at the time of registering appeared 2 and so on. Could someone help me at this point tbm? really have no idea what I want to do. I really need the help of my friends. I thank you already.

1 answer

0

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 is the type not null, therefore, you must fill in a 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 still cannot register the product, use the function mysql_error below the insert to visualize the problem.

die( mysql_error($link) );

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

  • I thank you very much for your help, but unfortunately you still persist in the same mistake. but thank you very much. I’ll keep trying to solve this problem.

  • Oh and about mysqli_ I already knew, but I use two different Pc when I’m programming( the same and the school) and the version of the school shaman is outdated then end up giving some errors when using mysqli_

  • Add the mysql_error and post the error you receive. Also check that $_SESSION["id"] is returning a numerical value.

  • After using mysql_error I received such errors

  • Notice: Undefined variable: _SESSION in C: xampp htdocs Work event.php on line 12 Notice: Undefined variable: link in C: xampp htdocs Work event.php on line 13 Warning: mysql_error() expects Parameter 1 to be Resource, null Given in C: xampp htdocs Work event.php on line 13

  • The first mistake you forgot to add the $, the correct is $_SESSION. The second error means that the value of $_SESSION["id"] is null, and it should be number (as I explained in the reply). I edited my code, added a check on id.

  • With the change you made, it worked and you’re already registering. but always register in Employee 1. and when I change login and put one with an Id for example 3. continue registering as employee 1. there is a way to always change user the Id also change without having to manually change the number 1 in the code ?

  • This happens because the login is not saving the employee ID on Session or the user and password data are incorrect. You can create another question with this question and we can check. I can not mix the answers because it is against the rules of the site.

  • All right, I understand. Thank you so much for your help. I learned a lot from you today. unfortunately I have less than 15 reputation and I can not put 1 point in your reply. but I am super grateful for your help

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.