Poll isn't working???

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
cturner
Forum Contributor
Posts: 153
Joined: Sun Jul 16, 2006 3:03 am
Location: My computer

Poll isn't working???

Post by cturner »

I am trying to build a poll for a client of mine. When I test the codes that are below it doesn't display the shopping cart vote. I am not sure if the shopping cart vote is inserted into the database. If someone thinks that this should be in PHP forum please move there.

Where the code checks to see if the user has already voted, that doesn't work.

Can someone please have a look at my codes and tell me how I can solve those problems? Thanks in advance.

Code: Select all

require "config.php";
			$shoesradiogroup = "";
			if (isset($_POST['shoesradiogroup'])) {
				$shoesradiogroup = mysql_real_escape_string($_POST['shoesradiogroup']);
			}
		if (isset($_POST['submit1'])) {
			$arrErrors = array();
			if ($shoesradiogroup == '') {
				$arrErrors['shoesradiogroup'] = 'Please select an option.';
			}
			if (count($arrErrors) == 0) {
                                // check to if the user has voted
				if ((isset($_COOKIE['shoppingcart'])) || (isset($_COOKIE['orderform']))){
					//header ('Location: results1.php');
					echo "You have already voted.<br />";
				}				
			
				// adds their vote to the database
				//switch ($shoesradiogroup) {
				//case 1:
				if ($shoesradiogroup == 'shopcart') {
				$insert1 = "INSERT INTO shoespoll (shoppingcart) VALUES (shoppingcart+1)";
				if (mysql_query ($insert1)) {
					setcookie('Voted','shoppingcart');
					echo "You have voted - shopping cart!";
					//header ('Location: results1.php');
				} else {
					print "<p>Could not insert the entry because: <b>" . mysql_error() . "</b>. The query was $insert1.</p>";
				}
				}				
				//break;
				//case 2:
				if ($shoesradiogroup == 'orderform') {
				$insert2 = "INSERT INTO shoespoll (orderform) VALUES (orderform+1)";
				if (mysql_query ($insert2)) {
					setcookie('Voted','orderform');
					echo "You have voted - order form!";
					//header ('Location: results1.php');
				} else {
					print "<p>Could not insert the entry because: <b>" . mysql_error() . "</b>. The query was $insert2.</p>";
				}
				}
				//break;
				//}

			} else {
	        // The error array had something in it. There was an error.
        	// Start adding error text to an error string.
        	$strError = '<div class="formerror"><p>Please check the following and try again:</p><ul>';
        	// Get each error and add it to the error string
        	// as a list item.
        	foreach ($arrErrors as $error) {
            	$strError .= "<li>$error</li>";
        	}
        	$strError .= '</ul></div>';
    		}
		}
		mysql_close();
This code is suppose to display the results of the poll:

Code: Select all

require "config.php";
$query = mysql_query("SELECT * FROM shoespoll") or die ("Could not query because: ".mysql_error());
$result = mysql_fetch_array($query);
$total = $result[shoppingcart] + $result[orderform];
$per1 = round($result[shoppingcart]) / $total * 100;
$per2 = round($result[orderform]) / $total * 100;
echo "Shopping cart: ".$result[shoppingcart]." votes ".$per1."%<br />";
echo "Order form: ".$result[orderform]." votes ".$per2."%";
mysql_close();
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Poll isn't working???

Post by onion2k »

cturner wrote:I am not sure if the shopping cart vote is inserted into the database.
You might want to check that.
Post Reply