user663049
user663049

Reputation: 1178

Trouble with a script for logging amount of facebook likes with mysql + php

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

Answers (2)

ifaour
ifaour

Reputation: 38135

Well, there are lots of issues in the code above:

  1. Your logic is totally wrong where you are including EVERYTHING inside the loop!

    1. You are including the same div "id" multiple of times (9 times according to your Mysql query)
    2. Including the FB JS library multiple of times (same thing for the JS initializing snippet)
    3. You are missing the APP ID parameter
    4. You are building 9 tables (I'm not sure if this is intended too!)
    5. You are once again including the FB code a couple of times in the table!
  2. Check @zzarbi answer for your ajax backend page!

Now a solution to your code issues?

  1. Do more effort on learning the basics of PHP, HTML and Javascript.
  2. Move your Facebook and table code outside the loop.

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

zzarbi
zzarbi

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

Related Questions