?
Solved

Storing data from an array to a session

Posted on 2005-04-12
4
Medium Priority
?
275 Views
Last Modified: 2013-12-12
Ok, heres my question. i have a array thats calling my data from a mysql database and displaying it in a table
i want to make it so each new row in the table is a seperate form, and when add to cart is clicked the data from that row is added to a session, can anyone let me know the easiest way to do this

 while ($row= mysql_fetch_array ($result, MYSQL_ASSOC)){
                  
                  print "<tr bgcolor=\"#666666\">";
            print "<td>
                              <form method=\"post\" action=\"punk.php\">
                              $row[ProductID]
                              </td>";
            print "<td>$row[ProductArtist]</td>";
            print "<td>$row[ProductName]</td>";
            print "<td>$row[ProductType]</td>";
                print "<td>$row[ProductDescription]</td>";
            print "<td><img src=\"$row[ProductImage]\"></td>";
            print "<td>$$row[ProductCost]</td>";
                  print "<td>
                              <input type=\"submit\" name=\"addtocart\" value=\"Add To Cart\">
                              </form>
                           </td>";
            print "</tr>";
            }
            print "</table>";
        }

thats the end of my array and table, how can i make it so that data from the add to cart button is on is stored in a session to be displayed on another page.

any help is greatly appreciated

thx
0
Comment
Question by:shrocks
  • 3
4 Comments
 
LVL 32

Accepted Solution

by:
ldbkutty earned 2000 total points
ID: 13768839
Assign the rows in hidden variables in the form. In the action page, you can retrieve the posted values and assign them to session.

<?php
while ($row = mysql_fetch_array($result))
{
    $ProductID = $row['ProductID'];
    $ProductArtist = $row['ProductArtist'];
    $ProductName = $row['ProductName'];
    $ProductType = $row['ProductType'];
    $ProductDescription = $row['ProductDescription'];
    $ProductCost = $row['ProductCost'];
?>    
    <tr bgcolor="#666666">
      <td>
       <form method="post" action="punk.php">
       <?php echo $ProductID; ?>
      </td>
      <td><?php echo $ProductArtist; ?></td>
      <td><?php echo $ProductName; ?></td>
      <td><?php echo $ProductType; ?></td>
      <td><?php echo $ProductDescription; ?></td>
      <td><img src="<?php echo $row['ProductImage']; ?>"></td>
      <td><?php echo $ProductCost; ?></td>
      <td>
        <input type="submit" name="addtocart" value="Add To Cart"/>
       <input type="hidden" name="ProductID" value="<?php echo $ProductID; ?>"/>
       <input type="hidden" name="ProductArtist" value="<?php echo $ProductArtist; ?>"/>                   
       <input type="hidden" name="ProductName" value="<?php echo $ProductName; ?>"/>
       <input type="hidden" name="ProductType" value="<?php echo $ProductType; ?>"/>
       <input type="hidden" name="ProductDescription" value="<?php echo $ProductDescription; ?>"/>
       <input type="hidden" name="ProductCost" value="<?php echo $ProductCost; ?>"/>
       </form>
      </td>
    </tr>
<?php            
    }
?>            

In punk.php:
========

<?php
session_start();
if(isset($_POST['addtocart']) && !empty($_POST['addtocart']))
{
    $_SESSION['ProductID'] = $_POST['ProductID'];
    $_SESSION['ProductArtist'] = $_POST['ProductArtist'];            
    $_SESSION['ProductName'] = $_POST['ProductName'];
    $_SESSION['ProductType'] = $_POST['ProductType'];
    $_SESSION['ProductDescription'] = $_POST['ProductDescription'];
    $_SESSION['ProductCost'] = $_POST['ProductCost'];
}
?>
0
 
LVL 32

Expert Comment

by:ldbkutty
ID: 13768842
>>  Assign the rows in hidden variables in the form.

should better read:

 Assign the row values as hidden variables inside the form.
0
 
LVL 1

Author Comment

by:shrocks
ID: 13768976
thanks for you answer, thats exactly what i was looking for, thank you
0
 
LVL 32

Expert Comment

by:ldbkutty
ID: 13769025
you are welcome and thanks for the points. :=)
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month16 days, 11 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question