Guys as you know my geek mind last night i was working on ” AJAX PHP Voting Script ” and i found one good article on 9lessons.
But i don’t am i true, but there is bug on their scrip and i must have to do for my next project in which user can vote image.
And whoila… after 25 min RnD i found some mismatch variable and db problem and solved it! and presenting it for you free !!
message TABLE
CREATE TABLE messages( mes_id INT PRIMARY KEY AUTO_INCREMENT, msg TEXT, up INT, down INT);
Voting_IP Table : Storing IP address
CREATE TABLE Voting_IP( ip_id INT PRIMARY KEY AUTO_INCREMENT, mes_id_fk INT, ip_add VARCHAR(40), FOREIGN KEY(mes_id_fk) REFERENCES messages(mes_id));
voting.php
Voting with jQuery, Ajax and PHP
<script src="jquery.js" type="text/javascript"><!--mce:0--></script>
<script type="text/javascript"><!--mce:1--></script>
<!--
body
{
font-family:'Georgia', Times New Roman, Times, serif;
}
#main
{
height:80px; border:1px dashed #29ABE2;margin-bottom:7px;
width:500px;
}
a
{
color:#DF3D82;
text-decoration:none;
}
a:hover
{
color:#DF3D82;
text-decoration:underline;
}
.up
{
height:40px; font-size:24px; text-align:center; background-color:#009900; margin-bottom:2px;
-moz-border-radius: 6px;-webkit-border-radius: 6px;
}
.up a
{
color:#FFFFFF;
text-decoration:none;
}
.up a:hover
{
color:#FFFFFF;
text-decoration:none;
}
.down
{
height:40px; font-size:24px; text-align:center; background-color:#cc0000; margin-top:2px;
-moz-border-radius: 6px;-webkit-border-radius: 6px;
}
.down a
{
color:#FFFFFF;
text-decoration:none;
}
.down a:hover
{
color:#FFFFFF;
text-decoration:none;
}
.box1
{
float:left; height:80px; width:50px;
}
.box2
{
float:left; width:440px; text-align:left;
margin-left:10px;height:60px;margin-top:10px;
font-weight:bold;
font-size:18px;
}
img
{
border:none;
padding-top:7px;
}
-->
<div>
<h3>Voting with jQuery, Ajax and PHP</h3>
<div id="main">
<div class="box1">
<div class="up">
<a id="<?php echo $mes_id; ?>" class="vote" name="up">
</a></div>
<div class="down"><a id="<?php echo $mes_id; ?>" class="vote" name="down"></a></div>
</div>
</div>
</div>
down_vote.php
include("config.php");
$ip=$_SERVER['REMOTE_ADDR'];
if($_POST['id'])
{
$id=$_POST['id'];
$id = mysql_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);
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);
echo "<script type="text/javascript"><!--mce:2--></script>";
}
else
{
echo "<script type="text/javascript"><!--mce:3--></script>";
}
$result=mysql_query("select down from messages where mes_id=$id");
$row=mysql_fetch_array($result);
$down_value=$row['down'];
echo $down_value;
}
?>
up_vote.php
include("config.php");
$ip=$_SERVER['REMOTE_ADDR'];
if($_POST['id'])
{
$id=$_POST['id'];
$id = mysql_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);
if($count==0)
{
$sql = "update messages set up=up+1 where mes_id=$id";
mysql_query( $sql);
$sql_in = "insert into messages (mes_id_fk,ip_add) values ($id,'$ip')";
mysql_query( $sql_in);
echo "<script type="text/javascript"><!--mce:4--></script>";
}
else
{
echo "<script type="text/javascript"><!--mce:5--></script>";
}
$result=mysql_query("select up from messages where mes_id=$id");
$row=mysql_fetch_array($result);
$up_value=$row['up'];
echo $up_value;
}
?>
Do not forget to include JQuery file

Subscribe to Free EngiGuide Latest Update SMS Alerts.
