0
votes

Can someone please help me. I'm trying to create a basic like system by inserting the values into mysql and auto incrementing the number of times the column 'likes' has been updated.

Basically the script will insert where there is not currently any record and update if there is a record.

I am trying to insert 'user_id' as a value, aswell but only the liked_id is being inserted into the table. the 'likes' column is being auto incremented as it should be but i need to find out how i can insert the user_id which is the users session id aswel and this isn't being put in. also i am trying to update the column 'user_id_has_liked' from enum value 0 to 1 as a final result.

can someone please show me where i am going wrong. thanks

<?php

    require_once('includes/session.php');
    require_once('includes/functions.php');
    require('includes/_config/connection.php');

    session_start();

        confirm_logged_in();

        if (isset ($_GET['to'])) {
        $user_to_id = $_GET['to'];


    }


    if (!isset($_GET['to']))
        exit('No user specified.');

    $user_id = $_GET['to'];


    $result = mysql_query("SELECT * FROM ptb_likes WHERE liked_id ='".$user_to_id."' ");

    if( mysql_num_rows($result) > 0) {
    mysql_query("UPDATE ptb_likes SET likes = likes +1 WHERE liked_id = '".$user_to_id."' ");


        $user_to_id = mysql_query("ALTER TABLE likes AUTO_INCREMENT = $id");
    }
    else
    {
        mysql_query("INSERT INTO ptb_likes (user_id,liked_id) VALUES ('".$_SESSION['user_id'].",".$user_to_id."') ");

    }

    $result1 = mysql_query("UPDATE ptb_likes SET user_id_has_liked='1' WHERE user_id=".$_SESSION['user_id']."") 
    or die(mysql_error());




    if($result) 
    { 

    header("Location: {$_SERVER['HTTP_REFERER']}");

    }
    ?>
1
Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.Joseph Silber
What error message do you get? What troubleshooting have you done so far?John Conde
mysql_query("ALTER TABLE likes AUTO_INCREMENT = $id"); would be classified as extremely shady code.Ja͢ck
echo the query and execute in phpmyadmin and find the errorAngelin Nadar

1 Answers

0
votes

As the others said, mysql_* statements are depricated, use mysqli_* statements...

The first issue is the code in the user id insert statement was missing some quotes, it should look like this:

            mysql_query("INSERT INTO ptb_likes (user_id,liked_id) VALUES ('".$_SESSION['user_id']."','".$user_to_id."') ");

The user_id_has_liked query issue could be caused by the enum variable being an integer in mysql. you could also try saving your query to a query variable and passing the variable to your query function for readability...

            $query = "UPDATE ptb_likes SET user_id_has_liked='1' WHERE user_id=".$_SESSION['user_id'];
            $result1 = mysql_query($query) or die(mysql_error());