最喜欢的帖子脚本

时间:2014-03-25 20:04:24

标签: php mysql database

我在制作系统以使用户能够收藏最喜欢的帖子时遇到了问题。我知道我在这里遗漏了一些东西,但我不确定是什么,我不知道如何修复它以便它起作用。

mysql_select_db($database_connection, $connection);
$query_deltakelse = "SELECT brukernavn, postID FROM deltakelse";
$deltakelse = mysql_query($query_deltakelse, $connection) or die(mysql_error());
$row_deltakelse = mysql_fetch_assoc($deltakelse);
$totalRows_deltakelse = mysql_num_rows($deltakelse);

$username=$_SESSION['valid_user'];

if(in_array($_POST['id'], $row_deltakelse))
{
   mysqli_query($connection,"DELETE FROM deltakelse WHERE postID='id'");
}
else
{
   mysqli_query($connection,"INSERT INTO bruker (brukernavn, postID) VALUES ('$username', '$id')");
}

$(document).ready(function() {
    $('.delta').on('click', null, function() {
        var _this = $(this);
        var post_id = _this.data('id');
        $.ajax({
          type     : 'POST',
          url      : '/file.php',
          dataType : 'json',
          data     : 'id='+ post_id,
          complete : function(data) {
               if(_this.text() == 'Fave this')
               {
                 _this.html('Faved!');
               }
               else
               {
                 _this.html('Fave this');
               }
            }
            });
        });
    });


<a href="#" class="delta" data-id="<?php echo $post_id; ?>">Favorite</a>

1 个答案:

答案 0 :(得分:1)

您正在混合使用mysql和mysqli函数。你不能这样做。

这是MYSQL

mysql_select_db($database_connection, $connection);
$query_deltakelse = "SELECT brukernavn, postID FROM deltakelse";
$deltakelse = mysql_query($query_deltakelse, $connection) or die(mysql_error());
$row_deltakelse = mysql_fetch_assoc($deltakelse);
$totalRows_deltakelse = mysql_num_rows($deltakelse);

$username=$_SESSION['valid_user'];

这里使用MySQLi和MySQL资源。那不会起作用。

if(in_array($_POST['id'], $row_deltakelse))
{
   mysqli_query($connection,"DELETE FROM deltakelse WHERE postID='id'");
}
else
{
   mysqli_query($connection,"INSERT INTO bruker (brukernavn, postID) VALUES ('$username', '$id')");
}

也改变它:

if(in_array($_POST['id'], $row_deltakelse))
{
   mysql_query("DELETE FROM deltakelse WHERE postID='id'", $connection);
}
else
{
   mysql_query("INSERT INTO bruker (brukernavn, postID) VALUES ('$username', '$id')", $connection);
}