PDA

View Full Version : Dave need some help!!


m1a2x3x7
07-26-2007, 08:54 PM
I was wondering if you could take a look at my code and tell me whats wrong.

I'm doing a poll and i have a database with a table like this
poll_id | question | answer1 | answer2 | vote1 | vote2 | totalvotes

here is my code.


<?php

ob_start();

require_once("connections/connect.php");

session_start();

$query = sprintf("SELECT * FROM poll");
$result = @mysql_query($query);
$row = mysql_fetch_array($result);
$submit = $_POST['submit'];

$answerOneTotal = $row['vote1'];
$answerTwoTotal = $row['vote2'];

$totalVotes = $row['totalvotes'];


//vote SATs or ACTs
if ($submit && $_POST['answer']="answerOne"){

$answerOneTotal = $answerOneTotal + 1;
$totalVotes = $totalVotes + 1;

$query = sprintf("UPDATE poll SET vote1 = '$answerOneTotal'");
mysql_query($query)or die(mysql_error());

$query = sprintf("UPDATE poll SET totalvotes = '$totalVotes'");
mysql_query($query)or die(mysql_error());
}


if ($submit && $_POST['answer']="answerTwo"){

$answerTwoTotal = $answerTwoTotal + 1;
$totalVotes = $totalVotes + 1;

$query = sprintf("UPDATE poll SET vote2 = '$answerTwoTotal'");
mysql_query($query)or die(mysql_error());

$query = sprintf("UPDATE poll SET totalvotes = '$totalVotes'");
mysql_query($query)or die(mysql_error());

}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Poll</title>
</head>

<body>
<div align="center"><?php echo $row['question'];?>
<form id="form1" name="form1" method="post" action="">
<p>
<label>
<input type="radio" name="answer" value="answerOne" />
</label>
<?php echo $row['answer1'];?>
<label> <br />
<input type="radio" name="answer" value="answerTwo" />
</label>
<?php echo $row['answer2'];?>
<br />
<input name="hiddenField" type="hidden" value="1" />
<label>
<input name="submit" type="submit" id="submit" value="Vote" />
</label>
</form>
</div>
</body>
</html>

now the problem that I'm having are the radio buttons I give them each a different value but no matter what once I submit it always takes the highest value and adds 1 to vote1 and vote2 in the data base.

now it works fine if I give them a different name but I need them to only be able to select one radio button.

Here's the dummy site of it www.blog.schultzstudio.com/poll.php (http://www.blog.schultzstudio.com/poll.php)

Thanks for the help mate.

davidj
07-27-2007, 05:50 AM
your IF statements are set to =

should be == when checking for equality

m1a2x3x7
07-27-2007, 11:00 AM
you da man