Session in php giving error

Asked

Viewed 43 times

-1

Good afternoon guys, trying since yesterday and not getting, next I have these two tables in the bank, when I soon save this session $_SESSION['username'] = $username;

but they need to give a select on the registered pets and they return all the content of the bank and not the user has registered


CREATE TABLE `novo_pet` (
    `id_novopet_cod` INT(11) NOT NULL AUTO_INCREMENT,
    `nome_animal` VARCHAR(100) NOT NULL,
    `raca_animal` VARCHAR(100) NOT NULL,
    `idade_animal` INT(2) NULL DEFAULT NULL,
    `cor_animal` VARCHAR(100) NOT NULL,
    `especie_animal` VARCHAR(100) NOT NULL,
    `porte_animal` VARCHAR(100) NOT NULL,
    `sexo_animal` VARCHAR(5) NOT NULL,
    `proprietario_animal` VARCHAR(100) NOT NULL,
    `caracteristica_animal` VARCHAR(500) NOT NULL,
    `telefone_animal` VARCHAR(100) NOT NULL,
    `image` TEXT NOT NULL,
    `situacao_animal` TEXT NOT NULL,
    PRIMARY KEY (`id_novopet_cod`)
)
COLLATE='latin1_swedish_ci'
ENGINE=MyISAM
AUTO_INCREMENT=4
;

CREATE TABLE `usuario` (
  `id_usuario_cod` int NOT NULL PRIMARY KEY AUTO_INCREMENT ,
  `username` varchar(50) NOT NULL,
  `email` varchar(50) NOT NULL,
  `senha` varchar(50) NOT NULL,
  `telefone` varchar (12) not null,
   `sexo_usuario` varchar (10) not null,
  `trn_date` datetime NOT NULL
);

?php
$sql = "select * from novo_pet  "; 
$result = mysqli_query($conn, $sql);
if(mysqli_num_rows($result)){
while($row = mysqli_fetch_assoc($result)){

if anyone can help me in this matter, I thank you for your help. ?>

  • Well there are several points in your code that need to be improved, 1° how do you know who registered each pet ? you need to add a Foreign key to your pets table referencing the user. according to your select do not have Where, ie you are bringing all the data from the database

1 answer

0


First you need to reference who registered the pet, for this you need to add a Foreign key in your pets table

ALTER TABLE novo_pet ADD id_usuario_cod_fk int not null
ALTER TABLE novo_pet ADD FOREIGN KEY(id_usuario_cod_fk) references usuario(id_usuario_cod)

Algorithm in your Query change it as follows

SELECT * FROM novo_pet 
JOIN usuario ON usuario.id_usuario_cod = novo_pet.id_usuario_cod_fk
where usuario.username = 'nomeDaSuaSessão'
  • Insert is no longer running rsrrs, and I’m catching up to try to tidy up, thank you so much for your reply.

  • you will probably have to inform the id_usuario_cod on the Insert so save this value in a session as well

  • no Heidisql direct Insert worked, but on the page of the site, I gave an Ecko to see what appears and where I put 1 in sql it returns me ' test' which is the name of the user and not the user code I used so $Rn = $_SESSION['username']; but how do I get the username to log in is not entering

Browser other questions tagged

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