1

This is assignment so not looking for anything perfectly safe and secure, just working.I have table in SQL database, I'm printing down all records, all records have unique reference number, for each row of the database I printed down I gave checkbox with value of the row's ref. number, when I submit them by "POST" everything is working and printing out:

if (!$_POST['checkbox']) {
    echo "Your basket is empty.";
} else {
    echo "<table border='0' id='games_table' cellspacing='1'>";
    echo "<tr id='basket_table_row'>";
    echo "<td colspan='3'>" . "Logged: " . $_SESSION['user'] . "</td>";
    echo "<td colspan ='2'>" . "OS used on this machine: " . "<script type='text/javascript'>document.write(yourOS())</script><noscript>Computers</noscript>" . "</td>";
    echo "</tr>";
    echo "<tr id='basket_table_row'>";
    echo "<td colspan='5'>" . "You put into the basket these games: " . "</td>";
    echo "</tr>";
    foreach ($_POST['checkbox'] as $value) {
        $_SESSION['basket']=array($value);

        $res=pg_query($conn,"select * from CSGames where refnumber='$value'");
        while ($a = pg_fetch_array ($res)) {
            echo "<tr id='games_table_row'>";
            echo "<td>" . $a["refnumber"] . "</td>";
            echo "<td>" . $a["title"] . "</td>";
            echo "<td>" . $a["platform"] . "</td>";
            echo "<td>" . $a["description"] . "</td>";
            echo "<td>" . $a["price"] . "</td>";
            echo "</tr>";   
        }
    }
    echo "</table>\n";
}

but only think which stays recorded in $_SESSION['basket'] is value of the last checkbox but I need all of them (60 or 70). what Am I doing wrong?

1
  • You need us to show some more. Your request doesnt really make sense. Commented Nov 24, 2011 at 15:09

5 Answers 5

2

You are overwriting te value of $_SESSION['basket'] at each iteration of the loop.

The last value is the only one stored.

Sign up to request clarification or add additional context in comments.

Comments

1

Currently, you are only storing the last value, if you wish to store every value, you should add it like this:

$_SESSION['basket'][] = $value;

Comments

0
foreach($_POST['checkbox'] as $value){

    $_SESSION['basket']=array($value);

}

every iteration of your loop is overwriting the value in $_SESSION['basket'], hence you only seeing the last checkbox value.

Comments

0

In every step of foreach, you create a new array in $_SESSION['basket'] with only one item, the current value of $value. It is corrected like this:

// ...
$_SESSION['basket'] = array();
foreach ($_POST['checkbox'] as $value) {
    $_SESSION['basket'][] = $value;
    // ...
}
// ...

Comments

0

You can directly do it like this

$_SESSION['basket'] = $_POST['checkbox'];

because the data of $_POST['checkbox'] is an array anyway. What you are doing is looping the $_POST['checkbox'] and saving each data as array to a session.

this $_SESSION['basket'] = $_POST['checkbox'];

and

 foreach ($_POST['checkbox'] as $value) {
        $_SESSION['basket'][]=$value;
 }

will have the same value.

What you have right only saves the last value of $_POST['checkbox']

Comments

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.