3

This should be a relatively easy procedure to code but for some reason I am just not able to get it working.

What Im trying to do

Members enter a competition, you are only allowed to enter once. To stop members from entering more than once I use the following code

//Check If user allready entered comp
$sql="SELECT member_nr, count(member_nr) as entered FROM competition WHERE member_nr ='$memNr' and competition = '$comp'";
$result = mysql_query( $sql ) or trigger_error( mysql_error() . " in " . $sql );

while($row = mysql_fetch_array($result)){
    $isEntered = $row['entered'];   
}//end while`

if($isEntered >1  ){
?>
    <h1>You have allready entered</h1>
<?
}//end check if entered allready

My Problem

Im getting the error Notice: Undefined offset: 1

I cant for the live of me figure out why am getting this, Im guessing my sql query is incorrect...any advice will be appreciated

Daniel
  • 350
  • 4
  • 12
Timothy Coetzee
  • 5,626
  • 9
  • 34
  • 97

2 Answers2

2

You are fetching one column and one field only, then why you use it in loop.

Simply use this instead,

$sql="SELECT member_nr, count(member_nr) as entered 
      FROM competition 
      WHERE member_nr ='$memNr' and competition = '$comp' LIMIT 1";
$result = mysql_query( $sql ) or trigger_error( mysql_error() . " in " . $sql );

$isEntered = mysql_result($result,0);
if($isEntered > 1) echo "<h1>You have allready entered</h1>";

Other point is that, mysql functions are deprecated. Use mysqli_ or PDO is best.

Read : Why shouldn't I use mysql_* functions in PHP?

Community
  • 1
  • 1
Bhavesh G
  • 3,000
  • 4
  • 39
  • 66
1

Your SQL query is correct. However, you need to initialise the variable $isEntered to 0 somewhere at the beginning of your script, since it will be undefined in case the member id was not found at all in your table.

And ...

... you should no longer be using mysql_.. functions. They are deprecated. Instead go for mysqli_... or PDO.

Carsten Massmann
  • 26,510
  • 2
  • 22
  • 43