0

I'm creating a sign up sheet system for my website. Each sign up sheet is given an ID number. When a user signs up it sends the username and the ID number to a database. However I want to stop duplicate entries so that the same user can only sign up to an event once. I can't use a WHERE statement in the SQL query and I was just wondering what the best way to tackle this would be. I can't use primary or unique keys as then they can only sign up for one event, or only one event can be signed up for. Thanks in advance.

This is the code for the page

<?php
session_start();

$user = $_POST['username'];
$id = $_POST['id'];

if(empty($user) == FALSE )
{

$con=mysqli_connect("localhost","emuas","flgdls","EMUAS_signUp");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$sql="INSERT INTO SIGN_UP_TEST (EventID, User, Comments)
VALUES
('$_POST[id]','$_SESSION[username]','$_POST[comments]')";

if (!mysqli_query($con,$sql))
  {
  die('Error: ' . mysqli_error($con));
  }
  header("Location: {$_SERVER['HTTP_REFERER']}");
  }
  else
  {
  header("Location: {$_SERVER['HTTP_REFERER']}");
  }
?>
  • A user can only sign up for 1 event? Or is it that they can only sign up for each event once? – Anujan May 23 '13 at 16:58
  • Only sign up for each event once, sorry worded it badly – Byron Fitzgerald May 23 '13 at 17:00
  • 1
    If it is mySql, you can create a **composite unique key**. See: http://stackoverflow.com/questions/635937/how-do-i-specify-unique-constraint-for-multiple-columns-in-mysql, – PM 77-1 May 23 '13 at 17:06

2 Answers2

0

You can add this UNIQUE key. It'll make it so a user can only sign up for each event once.

alter table SIGN_UP_TEST add unique index(EventID, User);

Anujan
  • 928
  • 1
  • 9
  • 20
0

If you use a composite primary key (id and user), then a user can sign up for multiple events, but not the same event twice.

primary key (id, user)

Ryan
  • 120
  • 2
  • 11