Reputation: 1178
I am trying to write a script that will log how many likes(Facebook) a page has in Mysql using the Facebook api, ajax and mysql. But at the moment it isn't working. All variables are defined, it is connected to Mysql, and jQuery is embedded and i'm not getting and SQL or PHP errors. Can anyone see where i'm going wrong?
Source code: index.php:
<?php
$sql=mysql_query("select * from likes ORDER BY id DESC LIMIT 9");
while($row=mysql_fetch_array($sql))
{
?>
<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script >
FB.init({
status: true,
cookie: true,
xfbml: true
});
FB.Event.subscribe('edge.create', function(response) {
alert(response);
if (response == "http://fbquote.me/like.php?id=<?php print $row['id']; ?>") {
$.ajax({
type: "POST",
url: "popular/ajax_pop.php",
data: "id=<?php print $row['id']; ?>"
cache: false,
});
}
});
</script>
<br /> <table style="width: 90%; height: 4px;" class="style11115" align="center">
<tr>
<td style="width: 68px; height: 23px;" class="style11111 " valign="top"><div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><fb:like href="http://fbquote.me/like.php?id=<?php print $row['id']; ?>" send="false" layout="button_count" show_faces="true" font=""></fb:like></td>
<td style="height: 23px" class="style11113" valign="top"><a href="http://www.fbquotes.me/like.php?id=<?php print $row['id']; ?>" class="style11112"><?php print $row['like']; ?></a></td>
</tr>
</table>
<?php } ?>
alax_pop.php:
<?php
include_once("../scripts/config.php");
$like = mysql_real_escape_string($GET_['id']);
$current_pop = mysql_query("SELECT pop FROM likes WHERE id=$like") or die ("Query failed: " . mysql_error());
$pop = $current_pop + 1;
$update = mysql_query("UPDATE like SET pop = ".$pop." WHERE id = ".$like."") or die ("Query failed: " . mysql_error());
;
?>
Upvotes: 0
Views: 424
Reputation: 38135
Well, there are lots of issues in the code above:
Your logic is totally wrong where you are including EVERYTHING inside the loop!
Check @zzarbi answer for your ajax backend page!
Now a solution to your code issues?
Something like this will get you started:
<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script >
FB.init({
status: true,
cookie: true,
xfbml: true
});
FB.Event.subscribe('edge.create', function(response) {
$.ajax({
type: "POST",
url: "popular/ajax_pop.php",
data: "url=" + response
cache: false
});
});
</script>
<?php
$result=mysql_query("select * from likes ORDER BY id DESC LIMIT 9");
if($result) {
?>
<table style="width: 90%; height: 4px;" class="style11115" align="center">
<?php while($row=mysql_fetch_array($result)) { ?>
<tr>
<td style="width: 68px; height: 23px;" class="style11111 " valign="top">
<fb:like href="http://fbquote.me/like.php?id=<?php print $row['id']; ?>" send="false" layout="button_count" show_faces="true" font=""></fb:like>
</td>
<td style="height: 23px" class="style11113" valign="top">
<a href="http://www.fbquotes.me/like.php?id=<?php print $row['id']; ?>" class="style11112"><?php print $row['like']; ?></a>
</td>
</tr>
<?php } ?>
</table>
<?php } ?>
In your ajax url, read the url and extract the ID from it.
Upvotes: 3
Reputation: 1852
You have two errors:
First you are using the method POST to send data with AJAX, but in your PHP code you are using GET to read it, plus the fact that you are using
$GET_['id']
instead of
$_GET['id']
So replace "$GET_['id']" by "$_POST['id']".
Secondly you are updating the table "like" instead of "likes"
Edit:
Replace in alax_pop.php:
$like = mysql_real_escape_string($GET_['id']);
by
$like = mysql_real_escape_string($_POST['id']);
Upvotes: 3