I'm working on a simple poll but have been plagued with problems as i'm new to php and mysql. However I am gettin the hang of it but sometimes I need a helping hand to show me what i'm doing wrong.
Now on the problem at hand, after much work I got the poll to display if the manner that'd i'd like but now i'm having a problem with updating the database once the vote button is clicked. What i'm trying to do is forward the selection to a function in my polls.php file that would update the database with selected poll option.
here's the code:
function poll($id) {
$sql = mysql_query("SELECT store_polls.poll_question FROM store_polls WHERE store_polls.poll_id ='$id' ");
if (!$sql) {
echo 'Query failed: ', mysql_error();
exit;
}
while ($question = mysql_fetch_array($sql))
{
$poll_question = $question['poll_question'];
echo "$poll_question";
}
$sql = ("SELECT store_polls.poll_question, store_poll_options.poll_option_id, store_poll_options.poll_option_text
FROM store_polls, store_poll_options WHERE store_polls.poll_id ='$id' AND store_poll_options.poll_id = '$id' ");
$result = mysql_query($sql);
if (!$result) {
echo 'Query failed: ', mysql_error();
exit;
}
echo"<form method=\"get\" action=\"polls.php\">";
while ($row = mysql_fetch_assoc($result))
{
$option = $row['poll_option_text'];
$poll_id = $row['poll_option_id'];
echo "<input type=\"radio\" valign=\"top\" name=\"poll\" value=\"$poll_id\">$option<br>";
}
echo"<input type=\"hidden\" name=\"cmd\" value=\"poll_results\"><br>";
echo"<div align=\"center\"><input type=\"submit\" value=\"vote\"></div>";
echo"</form>";
echo"</table>";
}
//submit poll results
function poll_results($poll){
//here's where i'm trying to update the database but it doesn't seem to be working
$sql = mysql_query("UPDATE store_poll_options SET poll_results = (poll_results + 1) WHERE poll_option_id = '$poll' ");
if (!$sql) {
echo 'Query failed: ', mysql_error();
}
$results = mysql_query("SELECT poll_option_text, poll_results FROM store_poll_options
WHERE poll_id = '3' ORDER BY poll_results DESC ");
if ($results)
while ($row = mysql_fetch_assoc($results))
{
$poll_result = $row['poll_results'];
$poll_option = $row['poll_option_text'];
echo"$poll_result<br>";
echo"$poll_option";
}
}
switch($_REQUEST['cmd']){
default:
poll(2);
break;
case "poll_results":
poll_results($_GET['poll']);
break;
}
I've tryed:
UPDATE store_poll_options SET poll_results = (poll_results + 1) WHERE poll_option_id = '$poll'
This was done in phpmydmin and it worked fine in updating the database. I'm wondering it there may be a problem with the way my fucntion is setup.