The account member number in the table cannot be more than one

It should be a relatively simple procedure to code, but for some reason I just can't get it to work.

What am i trying to do

Participants enter the competition, you are allowed to enter once . To prevent participants from entering multiple times, 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 error Note: Undefined offset: 1

I can't live to figure out why I am getting this, I am assuming my sql query is not correct ... any advice would be appreciated

+3


source to share


2 answers


You are only fetching one column and one field and then why are you using it in a loop.

Just 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>";

      



Another thing is that the functions mysql

are outdated. Use mysqli_

or PDO

.

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

+2


source


Your SQL query is correct. However, you need to initialize the variable $isEntered

to 0

somewhere at the beginning of your script, as it will be undefined if the element id is not found at all in your table.

AND...



... you shouldn't use functions anymore mysql_..

. They are outdated. Instead, go to mysqli_...

or PDO

.

+1


source







All Articles