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 , 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">© 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>
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.
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.
– user77891
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_
– user77891
Add the
mysql_error
and post the error you receive. Also check that$_SESSION["id"]
is returning a numerical value.– Valdeir Psr
After using mysql_error I received such errors
– user77891
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
– user77891
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 onid
.– Valdeir Psr
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 ?
– user77891
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.
– Valdeir Psr
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
– user77891