It won't update

Hi All

I’m trying to get a script to update the results in a mysql database. I am using radio buttons that have been set up with values of 1 and 0 so when 1 is added the values in the database increments by 1 and when 0 is added no action is performed.

However it won’t update, any ideas?

Heres the HTML form

<!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=utf-8" />
<title>Untitled Document</title>
<script type='text/javascript' 
src='http://ajax.googleapis.com/ajax/libs/prototype/1.6.0.3/prototype.js'></script>
<script type='text/javascript' 
src='http://ajax.googleapis.com/ajax/libs/scriptaculous/1.8.1/scriptaculous.js'></script>
<script type='text/javascript' src='js/starbox.js'></script>
</head>

<body>
<form action="script.php" method="post" >
<p>
  <label>
    <input type="radio" name="vote" value="1" id="vote0" />
    I Like Yours</label>
  <br />
  <label>
    <input type="radio" name="vote" value="0" id="vote1" />
    I Don't Like Yours</label>
    <input type="submit" name="rateit" value="Rate" id="rateit"  />
  <br />
</p>
</form>
</body>
</html>

and here is the script;

<?php

	// Connects to your Database
	mysql_connect("###", "###", "") or die(mysql_error());
	mysql_select_db("###") or die(mysql_error()); 
	
	// Get the radio button values 
	$vote = $_POST['votes'];
	$total = $vote;
		
		// Convert the values to the actual name, if you like
	
	// Insert record 
//	$insert = mysql_query("INSERT INTO co_rate (votes, total) VALUES ('$vote', '$total')");
	
	// Update the data
	$update = mysql_query("UPDATE co_rate SET $vote = $vote+1");
	$total++;
	
	// Put it into an array 
	$array = mysql_query("SELECT * FROM co_rate ") 
	or die (mysql_error());
	
	
	// Loop Through the data 
	while ($rate = mysql_fetch_array($array)) {
									  
									  //This calculates the sites ranking and then outputs it - rounded to 1 decimal
										$current = $rate[votes] + $rate[votes] / $rate[total]; 
									  echo "Current Rating: " . round($current, 1) . "<br>"; 
									  }
	
	


// One vote per user

// Add the total number votes up 

// Work out the percentage



// Display the result as a percentage 


?>

"UPDATE co_rate SET $vote = $vote+1"

should be

"UPDATE co_rate SET vote = $vote+1"

vote = $vote
column in database = php variable

Hi There

I tried this and it doesn’t update the records?

In your php code you have

    // Get the radio button values 
    $vote = $_POST['votes'];

However your form fields are called “vote” not “votes”. Try this:

    // Get the radio button values 
    $vote = $_POST['vote'];

Ok I’ve got it updating but it only does it the once i need it to do it for each time someone clicks on vote it is to update the mysql record add the value sent by the form and add another number to the total column any ideas?

then u dun need using $_POST[‘vote’] at all as each time someones click " vote " it counts +1 ? right ?

then use:

UPDATE co_rate SET vote = vote+1

hahaha…
it’s very simple…

It works ohhhh it works :).

Thanks to all who helped :slight_smile: