Poll isn't working???
Posted: Mon Feb 19, 2007 8:01 pm
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.
This code is suppose to display the results of the poll:
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();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();