Jump to content

[SOLVED] Checking for pre-existing record not working


Maggan

Recommended Posts

Trying to halt a program if a record already exists.

 

This is a simple vote/poll script. The part I'm having problems with is the actual adding of the vote. I tried adding various clauses to check for, error trap if someone has already voted, if someone is logged in etc, etc. I've tried mysql_num_rows, tried tweaking the query itself, tried using fetch record (as in the code below) but for some reason it still updates database table even if that member has already voted.

 

 

code:

 

if ($_GET[uRI_PAGE] =='addvote') {
$user->check_auth('u_member_list');



	$sql =	"SELECT pid FROM poll WHERE pid='$pname'";
	$result = mysql_query($sql)
		or trigger_error(mysql_error()."<PRE>".$query."</PRE>", E_USER_ERROR);
	$row = mysql_fetch_row($result); 


		if ( empty($pname) )
    	{
        $messagevote = "You are not logged in";
	unset($addvote);
	$s_addpolldo = true;
    	}

	 if ( $row > 0) 
	{
	$messagevote = "You have already voted";
	unset($addvote);
	$s_addpolldo = true;
	} 

		if(($sea == $sky) or ($sea == $limbus) or ($sea == $salvage) or ($sea == $einherjar) or ($sea == $wyrm) or ($sky == $limbus) or ($sky == $salvage) or ($sky == $einherjar) or ($sky == $wyrm) or ($limbus == $salvage) or ($limbus == $einherjar) or ($limbus == $wyrm) or ($salvage == $einherjar) or ($salvage == $wyrm) or ($einherjar == $wyrm)) {
		 $messagevote = "You can only give one give 5 points to one option, 4 to another, 3 to a third etc. This is to rank your priorities. Giving same value to more than one event is not allowed.";
	unset($addvote);
	$s_addpolldo = true;
		}

		else {


		$updac = "INSERT INTO poll VALUES ('','$pname','$sea','$sky','$limbus','$salvage','$einherjar','$wyrm','$hnmpriority','$tuesday') ";
		mysql_query($updac);
		$s_addpolldo = true;
		$messagevote = "Successfully added your vote";
		}
		}

 

Can anyone spot the reason why it passes the check even if $pname already has voted?

Link to comment
Share on other sites

Then lose the quotes:  $sql = "SELECT pid FROM poll WHERE pid='$pname'";

 

I don't know that caused problems; it was just the first inconsistancy I saw.

 

More substantially, you're not checking for results correctly.  You've set $row to a row of data, but check that it's greater than zero.  You should check mysql_num_rows() instead.

 

<?php

if ($result && mysql_num_rows($result) > 0) {
$messagevote = "You have already voted";
unset($addvote);
$s_addpolldo = true;
}
?>

Link to comment
Share on other sites

Changed the following two bits:

 


	$sql =	"SELECT pid FROM poll WHERE pid=$pname";
	$result = mysql_query($sql)
		or trigger_error(mysql_error()."<PRE>".$query."</PRE>", E_USER_ERROR);
	$num = mysql_num_rows($result); 

 


                         if ( $num > 0) 
	{
	$messagevote = "You have already voted, $pname!";
	unset($addvote);
	$s_addpolldo = true;
	}

 

Still no go.

Link to comment
Share on other sites

It's a member id. numeric.

 

I'll post code in full with some tweaks, I've done to help.. recognise variables that had misleading names

 

<?php
/******************************
* EQdkp
* Copyright 2002-2003
* Licensed under the GNU GPL.  See COPYING for full terms.
* ------------------
* rules.php
* Began: Wed October 12 2005
*
* $Id: poll.php,v 1.3 2005/10/12 21:09:08 Dyfrin Exp $
*
******************************/

define('EQDKP_INC', true);
$eqdkp_root_path = './';
include_once($eqdkp_root_path . 'common.php');


if ($_GET[uRI_PAGE] =='viewpoll') {
$user->check_auth('u_member_list');
$s_viewpoll = true;
$curmli= $user->data['user_id'];
	$curmemberloggedin= mysql_fetch_assoc(mysql_query("SELECT * FROM " . MEMBER_USER_TABLE . " WHERE user_id='$curmli'"));
	$curnamememli= $curmemberloggedin['member_id'];
	$findcurmemlogin= mysql_fetch_assoc(mysql_query("SELECT * FROM " . MEMBERS_TABLE . " WHERE member_id='$curnamememli'"));
	$curnamememli2= $findcurmemlogin['member_name'];
	$findcurmemlogin2= mysql_fetch_assoc(mysql_query("SELECT * FROM " . W_PLAYER_TABLE . " WHERE pname='$curnamememli2'"));
	$pid= $findcurmemlogin2['pid'];
	$pname= $findcurmemlogin2['pname'];


}

if ($_GET[uRI_PAGE] =='addvote') {
$user->check_auth('u_member_list');



	$sql =	"SELECT pid FROM poll WHERE pid=". $addpid;
	$result = mysql_query($sql)
		or trigger_error(mysql_error()."<PRE>".$query."</PRE>", E_USER_ERROR);
	$num = mysql_num_rows($result); 


	if ( empty($pname) )
	{
	$messagevote = "You are not logged in";
	unset($addvote);
	$s_addpolldo = true;
	}

	 if ( $num > 0) 
	{
	$messagevote = "You have already voted!";
	unset($addvote);
	$s_addpolldo = true;
	} 

		if(($sea == $sky) or ($sea == $limbus) or ($sea == $salvage) or ($sea == $einherjar) or ($sea == $wyrm) or ($sky == $limbus) or ($sky == $salvage) or ($sky == $einherjar) or ($sky == $wyrm) or ($limbus == $salvage) or ($limbus == $einherjar) or ($limbus == $wyrm) or ($salvage == $einherjar) or ($salvage == $wyrm) or ($einherjar == $wyrm)) {
		 $messagevote = "You can only give one give 5 points to one option, 4 to another, 3 to a third etc. This is to rank your priorities. Giving same value to more than one event is not allowed.";
	unset($addvote);
	$s_addpolldo = true;
		}

		else {


		$updac = "INSERT INTO poll VALUES ('','$addpid','$sea','$sky','$limbus','$salvage','$einherjar','$wyrm','$hnmpriority','$tuesday') ";
		mysql_query($updac);
		$s_addpolldo = true;
		$messagevote = "Successfully added your vote";
		}
		}	


if ($_GET[uRI_PAGE] =='viewresult') {
$user->check_auth('u_member_list');
$s_viewresult = true;


	 $sqla = 'SELECT SUM(sea) AS seatotal, SUM(sky) AS skytotal, SUM(limbus) AS limbustotal, SUM(salvage) AS salvagetotal, SUM(einherjar) as sumeinherjar, SUM(wyrm) AS wyrmtotal	 
		FROM poll';

	if ( !($sum_result = $db->query($sqla)) )
	{
	message_die('Could not obtain item information', '', __FILE__, __LINE__, $sql);
	}
	while ( $sum = $db->fetch_record($sum_result) )
	{
	$tpl->assign_block_vars('sum_row', array(
	'ROW_CLASS' => $eqdkp->switch_row_class(),
	'SEA_TOTAL' => $sum['seatotal'],
	'SKY_TOTAL' => $sum['skytotal'],
	'LIMBUS_TOTAL' => $sum['limbustotal'],
	'SALVAGE_TOTAL' => $sum['salvagetotal'],
	'EINHERJAR_TOTAL' => $sum['sumeinherjar'],
	'WYRM_TOTAL' => $sum['wyrmtotal'])
	);
	}



$sqlb = 'SELECT * 	 
		FROM poll';
		if ( !($row_result = $db->query($sqlb)) )
		{
		message_die('Could not obtain item information', '', __FILE__, __LINE__, $sql);
		}
		while ( $row = $db->fetch_record($row_result) )
		{

$tpl->assign_block_vars('data_row', array(
'ROW_CLASS' => $eqdkp->switch_row_class(),
'SEA' => $row['sea'],
'SKY' => $row['sky'],
'LIMBUS' => $row['limbus'],
'SALVAGE' => $row['salvage'],
'EINHERJAR' => $row['einherjar'],
'WYRM' => $row['wyrm'],
'HNM_PRIORITY' => $row['hnmprio'],
'TUESDAY' => $row['tuesday'])
);
}
$hnmprioy = mysql_query("SELECT * FROM poll WHERE hnmprio='yes'");
$hnmprioynum = mysql_num_rows($hnmprioy);
$hnmprion = mysql_query("SELECT * FROM poll WHERE hnmprio='no'");
$hnmprionnum = mysql_num_rows($hnmprion);
$tuesdayy = mysql_query("SELECT * FROM poll WHERE tuesday='yes'");
$tuesdayynum = mysql_num_rows($tuesdayy);
$tuesdayn = mysql_query("SELECT * FROM poll WHERE tuesday='no'");
$tuesdaynnum = mysql_num_rows($tuesdayn);

   


}

$tpl->assign_vars(array(
        
	//Chamille vars start


	'PID_LOGGED_IN' => $pid,
	'P_NAME' => $pname,
	'HNM_PRIORITY_YES' => $hnmprioynum,
	'HNM_PRIORITY_NO' => $hnmprionnum,
	'TUESDAY_PRIORITY_YES' => $tuesdayynum,
	'TUESDAY_PRIORITY_NO' => $tuesdaynnum,
	'F_ADD_VOTE' => 'poll.php' . $SID . '&' . URI_PAGE . '=addvote',
	'S_VIEW_POLL' => $s_viewpoll,
	'S_ADD_POL_DO' => $s_addpolldo,
	'S_VIEW_RESULT' => $s_viewresult,
	'ADD_VOTE_MESS' => $messagevote,
	'L_SUBMIT' => $user->lang['submit'],
	'S_REQUESTITEM' => $s_requestitem,
	'S_REQUESTITEMFOR' => $s_requestitemfor)

);

$pm->do_hooks('/poll.php');




$eqdkp->set_vars(array(
	'page_title'    => sprintf($user->lang['title_prefix'], $eqdkp->config['guildtag'], $eqdkp->config['dkp_name']),
	'template_file' => 'poll.html',
	'display'       => true)
	);


?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.