[SOLVED] Poll problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Czar
Forum Commoner
Posts: 58
Joined: Sun Dec 29, 2002 11:17 am

[SOLVED] Poll problem

Post by Czar »

It's my first poll, so...
It throws following error every time:

Warning: mysql_result(): supplied argument is not a valid MySQL result resource

here's the code:

Code: Select all

<?php

function polli() {
  require "../dbconn.php";
  $stringi = "SELECT id FROM polli-1";
  $stringi2 = "INSERT INTO polli-1(id,a,b) VALUES ('','$va','$vb')";
  if(isset($action) && ($action == "vote")) {
    mysql_query($stringi2);
  }
  $count1= mysql_result(mysql_query("SELECT COUNT(a) FROM polli-1 WHERE a=1"),0);
  $count2= mysql_result(mysql_query("SELECT COUNT(b) FROM polli-1 WHERE b=1"),0);
  if($count1!= 0) {
    if($count2 != 0) {
      echo "<table width=300><tr><td><form action=?show=poll&action=vote method=post>";
	  echo "<input name=va type=radio value=1> option 1<br>";
	  echo "<input name=vb type=radio value=1> option 2<br>";
	  echo "<input type=submit value=vote name=do_vote></form>";
	  echo "</td></tr>";
	  echo "<tr><td height=5 bgcolor=#cccccc width=".$count1."> ".$count1."<td><tr>";
      echo "<tr><td height=5>&nbsp;</td></tr>";
      echo "<tr><td height=5 bgcolor=#cccccc width=".$count2."> ".$count2."</td></tr></table>";
	} 
  }
}

?>
Any help appreciated...
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Post by microthick »

Are a and b INT fields or VARCHAR fields?
Czar
Forum Commoner
Posts: 58
Joined: Sun Dec 29, 2002 11:17 am

Post by Czar »

varchar
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

I had a look at the code and this is what I came up with:

Code: Select all

function polli() 
{
	require '../dbconn.php';
	
	// what do you do with this, it doesn't appear to be used?
	$stringi  = "SELECT id FROM polli-1";

	// if you want to insert something if the form is posted then you'll
	// need a reorganised version of the lines you had before
	if(isset($_GET['action']) && $_GET['action'] == 'vote') {
		// The values of va and vb can be gathered using a simplified
		// version of an if...else statement which uses something called
		// the ternary operator

		// see the comments above the form as to why $_POST['option'] is
		// being tested.
		$va = ($_POST['option'] == 'va') ? 1 : 0;
		$vb = ($_POST['option'] == 'vb') ? 1 : 0;

		// since $va and $vb are numbers they don't need quotes around
		// their values
		$stringi2 = "INSERT INTO polli-1(id, a, b) VALUES ('', $va, $vb)";

		// add some error handling to your mysql_query() call to catch
		// any problems
		mysql_query($stringi2) or die(mysql_error().'<p>'.$sql.'</p>');
	}

	// separate out these functions and SQL statement so that you can 
	// do some error handling.
	$sql1    = "SELECT COUNT(a) FROM polli-1 WHERE a=1";
	$result1 = mysql_query($sql1) or die(mysql_error().'<p>'.$sql1.'</p>');
	$count1  = mysql_result($result1, 0);

	$sql2    = "SELECT COUNT(b) FROM polli-1 WHERE b=1";
	$result2 = mysql_query($sql2) or die(mysql_error().'<p>'.$sql2.'</p>');
	$count2  = mysql_result($result2, 0);

	// you can simplify these two if statements into one
	if($count1 != 0 && $count2 != 0) {
		// using heredoc format you can remove the need for multiple echo
		// statements

		// your table of results won't work as expected because of the 
		// 300 width you've set for the table - you can use DIV's around
		// the counts to overcome this.

		// I was unsure of how this poll was supposed to work but it
		// seemed unlikely to work as expected with two differently 
		// named radio buttons, so I've coded it to be a choice 
		// between two options.  If you want to allow people to choose
		// both options then you should use checkboxes instead of
		// radio buttons.
		echo <<<END

<table width="300">
<tr>
	<td>
		<form action="?show=poll&action=vote" method="post">
			<input name="option" type="radio" value="va">option 1<br />
			<input name="option" type="radio" value="vb">option 2<br />
			<input type="submit" value="vote" name="do_vote">
		</form>
	</td>
</tr>
<tr>
	<td height="5">
		<div style="background-color: #cccccc; width: $count1;">$count1</div>
	</td>
</tr>
<tr>
	<td height="5">&nbsp;</td>
</tr>
<tr>
	<td height="5">
		<div style="background-color: #cccccc; width: $count2;">$count2</div>
	</td>
</tr>
</table>

END;

	} 
} // end func
Mac
Czar
Forum Commoner
Posts: 58
Joined: Sun Dec 29, 2002 11:17 am

Post by Czar »

Thanks a lot, Twigletmac. Solved the problems...
Post Reply