I am a no good in PHP (just learning). I tried modifying a php voting script which I downloaded from a free source. i can add image though (it was not an easy task for me and I am not done yet). Now Voting works fine on my local server , but online it doesn't. If I click to vote, the numbers just disappear without return. below are the codes to look into.
index.php ---
<?php include('config.php');
$sql=mysql_query("SELECT * FROM messages ORDER BY `messages`.`up` DESC LIMIT 20");
while($row=mysql_fetch_array($sql)) { $title=$row['title']; $desc=$row['desc'];
$mes_id=$row['mes_id']; $image=$row['image']; $up=$row['up'];
$down=$row['down']; ?> <div id="vote"> <div class="box1">
<div class='up'><a href="" class="vote" id="<?php echo $mes_id; ?>" name="up">
<?php echo $up; ?></a></div>
<div class='down'><a href="" class="vote" id="<?php echo $mes_id; ?>" name="down">
<?php echo $down; ?></a></div></div>
<div class='image' ><?php echo "<img src=user/admin/".$image ." width='87%' height='70%'/>"?></div>
<div class ='title'><?php echo $title; ?></div>
<div class='box2' >
<?php echo $desc; ?> </div> </div>
<?php } ?>
up_vote.php --------------------------
<?php include("config.php");
$ip=$_SERVER['REMOTE_ADDR'];
if($_POST['id']) { $id=$_POST['id']; $id = mysql_real_escape_String($id); $ip_sql=mysql_query("SELECT ip_add FROM voting_ip WHERE mes_id_fk='$id' AND ip_add='$ip'");
$count=mysql_num_rows($ip_sql)or die(mysql_error());
if($count<=20)
{
$sql = "UPDATE messages SET up=up+1 WHERE mes_id='$id'"; mysql_query( $up);
$sql_in = "INSERT INTO messages (mes_id_fk,ip_add) VALUES ('$id','$ip')"; mysql_query( $sql_in);
} else
{ echo "<script>alert('You have already voted');</script>"; }
$result=mysql_query("SELECT up FROM messages WHERE mes_id='$id'");
$row=mysql_fetch_array($result); $up_value=$row['up']; echo $up_value; } ?>
down_vote.php -----------------------------
<?php include("config.php");
$ip=$_SERVER['REMOTE_ADDR'];
if($_POST['id']) { $id=$_POST['id'];
$id = mysql_real_escape_String($id);
$ip_sql=mysql_query("SELECT ip_add FROM voting_ip WHERE mes_id_fk='$id' AND ip_add='$ip'");
$count=mysql_num_rows($ip_sql) or die(mysql_error());
if($count<=0)
{ $sql = "UPDATE Messages SET down=down+1 WHERE mes_id='$id'"; mysql_query( $sql);
$sql_in = "INSERT INTO voting_ip (mes_id_fk,ip_add) values ('$id','$ip')"; mysql_query( $sql_in);
} else { echo "<script>alert('You have already voted');</script>"; }
$result=mysql_query("SELECT down FROM Messages WHERE mes_id='$id'");
$row=mysql_fetch_array($result);
$down_value=$row['down']; echo $down_value; }
?>
Javascript --------------------------- (this is at the index header with some HTML codes)
<script type="text/javascript">
$(function() {
$(".vote").click(function()
{
var id = $(this).attr("id");
var name = $(this).attr("name");
var dataString = 'id='+ id ;
var parent = $(this);
if(name=='up')
{
$(this).fadeIn(200).html('<img src="dot.gif" align="absmiddle">');
$.ajax({
type: "POST",
url: "up_vote.php",
data: dataString,
cache: false,
success: function(html)
{
parent.html(html);
} });
}
else
{
$(this).fadeIn(200).html('<img src="dot.gif" align="absmiddle">');
$.ajax({
type: "POST",
url: "down_vote.php",
data: dataString,
cache: false,
success: function(html)
{
parent.html(html);
}
});
}
return false;
});
});
</script>
I appreciate anyone helping out, thanks everyone.
From your code,
if($count==50)
then only the values are being updated!!. How is it possible for a person who is going to vote for the first time??? It should be
if($count<50)