Problem with conditional Insert

Asked

Viewed 40 times

0

Good I’m with the following problem I’m making a conditional Insert not to duplicate entries in the table but is not inserting me anything

Code

<?php
session_start();
if(isset($_SESSION['user_id'])){
    mysql_query("INSERT INTO visitas_estabelecimento (user_id, estabelecimento_id) VALUES ('".$_SESSION['user_id']."', '".$row_cat->id."')
WHERE NOT EXISTS 
    (SELECT * FROM visitas_estabelecimento 
    WHERE estabelecimento_id='".$row_cat->id."' and user_id='".$_SESSION['user_id']."'");
}
?>
   $row_user_id=mysql_fetch_object($result_verifica);
    $_SESSION['user_id'] = $row_user_id->id;
  • Have you checked if the output of the string you are running inside the mysql_query is returned as expected? Probably some variable is empty. If you have everything ok, run the command in some SGBD and see what happens

  • Yes I have checked and leave as expected

  • Could put the code where $_SESSION['user_id'] is initialized;

  • I already put not put everything because I would have to put the whole login

  • If help I printed the return of the query INSERT INTO visitas_establishment (user_id, estabelecimento_id) VALUES (38, 1) WHERE NOT EXISTS (SELECT * FROM visitas_establishment WHERE estabelecimento_id=1 and user_id=38)

No answers

Browser other questions tagged

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