Hello,

I am looking for a script to add items to a database that can be found in the my account section,

The table in the database is structured:
id | user_id | slug

I want to use the ajax to update and remove it later on down the road if they want to.

The items are in a loop on the search page and I would like to also have the code added to the item details page.

Does anyone know of a code like that? or is it easy to write?

Thanks in advance!

Are you familiar with jQuery?

There are basically two components you need. Your jQuery script and the server side script that will take data from the Ajax call and write to your DB. Then, your script can return data back to your client.

Im not to keen with jquery. I know a little bit of it. I did write this:

<?php

include 'connect.php';

// Check connection
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$username = $_POST['user_id'];
$liked = $_POST['liked'];   
$slug = $_POST['url_slug'];

mysqli_query($con,"INSERT INTO favorites (username ,slug, value) VALUES ('$username','$slug',$liked)");

if (!mysqli_query($con,$sql))
{
    die('Error: ' . mysqli_error($con));
}

mysqli_close($con);
?>

This would something to handle your problem with jquery.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
<script type="text/javascript">
jQuery(document).ready(function($){
    $('#button').on('click', function(e){
        e.preventDefault();

        $.ajax({
            url: 'path/to/script.php',
            type: 'POST',
            data: {user_id: $_SESSION['user_id'], liked: true, url_slug: $(location).attr('href')},
            cache: false,
            success: function(data){
                alert(data);
            }
        });
    });
});
</script>

That works perfectly Gabriel, Thanks for that!

I'm going to be critical with @gabrielcastillo's example.
Since the request will be from the client, you don't need to send the user_id parameter within the POST request. You can do this:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js">
<script type="text/javascript">
jQuery(document).ready(function($){
    $('#button').on('click', function(e){
        e.preventDefault();

        $.ajax({
            url: 'path/to/script.php',
            type: 'POST',
            data: {liked: true, url_slug: $(location).attr('href')},
            cache: false,
            success: function(data){
                alert(data);
            }
        });
    });
});
</script>

then pick it back up like this:

<?php
    session_start();
    $user = (empty($_SESSION['user_id']) ? null : intval($_SESSION['user_id']));

    if ($user)
    {
        // Update the database
    }else{
        die();
    }

If you were to send the user_id and then retrieve it from $_POST, it would cause problems and enable one extra way for a hacker to exploit the system. Accessing the sessions directly is the smart thing to do in this case.

Good think I said something like this and not just like this. Someone could read my post and take the code as is, without making modifing to their needs.

Don't get me wrong, you have a good post, but I was just adding to it for security purposes.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.