How to limit one entry (purchase) per user?

0

This code is the action of a purchase form, I want only one user to register a purchase. Here in the code if you have not bought anything, your ID will not be in the table tickets so you can add the information, however if your ID was already added it is supposed to return to the index, however I find that the same user can continue adding purchases and have multiple purchases with the same ID and I do not understand what is happening, I do not get an error or something.

 session_start (); 
  include 'dbh.inc.php';

$userid = $_SESSION['user_id'];

////Valores para el formulario
$sala = $_POST ['cbmboxsala1'];
$horario = $_POST ['cbmboxhorario1'];
$asiento = $_POST ['cbmboxasiento1'];

if ( isset($_POST['btncomprar']) ) 

{

$sql = "SELECT * FROM boleto WHERE user_id = '$userid'";  
$result = mysqli_query ($conn, $sql);
if (!$row = mysqli_fetch_assoc($result))  

{

$sql = "UPDATE  users SET sala = '$sala', horario = '$horario', asiento = '$asiento' WHERE user_id = '$userid'";
                mysqli_query ($conn, $sql);                     
                header("Location: ../index.php?signup=success");                            
                exit ();
}

else 
{
    header("Location: ../index.php?factura=yatienesuna");
}


 }

else

 {
  echo 'error';
 }
  ?>
    
asked by Hoozuki 20.06.2018 в 20:54
source

1 answer

3

It's a conceptual error. The query "SELECT * FROM ticket WHERE user_id = '$ userid'" does not return false if it is not found, returns an empty array. In order for you to return false, the query must be "erroneous". Review the function: mysqli_query

    
answered by 21.06.2018 / 01:04
source