0

I have tried to create a small 'bookmarking' feature for my website. Users are able to click on the ".bookmarkButton" which will execute the following script:

<!--Add To Bookmarks--> 
        $(".bookmarkButton").click(function() {
            var pid=$(this).closest('div').attr('id');
            $('#noBookmark').hide(); 
            $.post('bookmarks/addBookmark.php', 'rid=' + pid, function (addBookmark) {
              $("#bookmarkResults").add(addBookmark);
           });  
       });

Here is the code for "addBookmark.php":

    <?php

session_start();

if (isset($_SESSION['ridArray']) && count($_SESSION['ridArray'] > 0)){
    addBookmark();

} else if (isset($_POST['rid']) && !isset($_SESSION['ridArray'])) { 
    $_SESSION['ridArray'] = array(); 
    addBookmark(); 
}


function addBookmark() {  
    if (is_array($_SESSION['ridArray']) && isset($_SESSION['ridArray']) && isset( $_POST['rid']) ) { 
            array_push($_SESSION['ridArray'], $_POST['rid']); //push the id value from post to the session array
            //$_SESSION['ridArrayClean'] = array_unique($_SESSION['ridArray']); //remove duplicates
            print_r($_SESSION['ridArray']); 

            foreach($_SESSION['ridArray'] as $x) {
                // Get all the data from the "example" table
                $result = mysql_query("SELECT * FROM example WHERE id = $x") 
                or die(mysql_error()); 
                $row = mysql_fetch_array( $result );
                echo $row['productname'];
    }}}

    ?>

The variable $_SESSION['ridArray'] holds the array with all the id's that have been accumulated.

My problem is that this script works only when one item is bookmarked. When there is more than one product bookmarked, I only get the product name that was last bookmarked and not every thing that I've bookmarked.

So for example instead of getting multiple product id's after clicking the bookmarkButton class like this: 0,1,2,3 in the array. I only get the one that was clicked last i.e. 6.

I've been looking into this for a while now and I can't seem to see what I'm doing wrong.

2
  • Does your session start automatically? I do not see session_start(); here. Commented Jan 28, 2012 at 8:01
  • The session start is placed at the start of the page. I haven't included it here. Commented Jan 28, 2012 at 8:05

2 Answers 2

0

The script only echos the productnames, if you posted a "rid".

Also you could write the if like this:

if (isset($_SESSION['ridArray'], $_POST['rid']) && is_array($_SESSION['ridArray'])) { 

Checking isset() first. Also you could additionally check for

... && count($_SESSION['ridArray'] > 0)
5
  • My issue is that it's only echoing one productname, not multiple if the user has clicked on the ".bookmarkButton" class more than once. Commented Jan 28, 2012 at 7:24
  • So check, if $_SESSION['ridArray'] is really set and contains some IDs before the complete if block. Commented Jan 28, 2012 at 7:27
  • Ok, so what I did was, before that block I added it. It prints a blank array. So what do I do then? Because I think it is interfering with a previous $_SESSION['ridArray'] = array(); which resets the array. If I don't include that, I get an undefined index errror Commented Jan 28, 2012 at 7:37
  • If you get an undefined index error, $_SESSION['ridArray'] is not set. So you have to find out how to fill $_SESSION['ridArray'] correctly. Commented Jan 28, 2012 at 7:42
  • I've modified my code above and it's still not working. I've added another checking mechanism, and I've updated the question. Commented Jan 28, 2012 at 8:04
0

I do not think that your session starts automatically (is it possible to set its autostart in php.ini, but it does not by default), so

<?php
session_start();

Other thoughts:

SELECT * FROM example WHERE id = $x

Have you ever heard about SQL Injection?

ps: no need in secondary check (they are checked before) and from the first condition follows the second one

is_array($_SESSION['ridArray']) && isset($_SESSION['ridArray'])

I would write it as

<?php
session_start();
if (isset($_POST['rid'])) { 
    addBookmark(intval($_POST['rid'])); 
}


function addBookmark($rid) {  
     $_SESSION['ridArray'][] = $rid; 
     $_SESSION['ridArray'] = array_unique($_SESSION['ridArray']);

     foreach($_SESSION['ridArray'] as $x) {
         $result = mysql_query("SELECT * FROM example WHERE id = '$x'") 
                   or die(mysql_error()); 
         $row = mysql_fetch_array( $result );
         echo $row['productname'];
     }
}
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.