I am trying to display two product names depending on a counter. I've declared a session variable 'counter'. I want to show the first product name if the button is clicked, and I want to show two product names if the button is clicked again.
Dell
Mac
However, if I click the button the second time, the second product name replaces the first product name; I want both products to display.
How would I fix this?
My code:
<?php
session_start();
include('connect.php');
// if counter is not set, set to zero
if (!isset($_SESSION['counter'])) {
$_SESSION['counter'] = 0;
}
// if button is pressed, increment counter
if (isset($_POST['button'])) {
++$_SESSION['counter'];
// Get data according to id
$userid2 = $_POST['id2'];
$query3 = "select * from product where id='" . $userid2 . "'";
$result3 = mysql_query($query3) or die(mysql_error());
$data3 = mysql_fetch_array($result3);
if ($_SESSION['counter'] == 1) {
echo $data3['name'] . "<br>";
}
if ($_SESSION['counter'] == 2) {
echo $data3['name'];
}
}
// reset counter
if (isset($_POST['reset'])) {
$_SESSION['counter'] = 0;
}
$query2 = "select * from product ";
$result = mysql_query($query2) or die(mysql_error());
$data = mysql_fetch_array($result);
$sr = $data['sr'];
$name = $data['name'];
$price = $data['price'];
$qunt = $data['quantity'];
$total += $data['price'];
while ($data = mysql_fetch_array($result)) {
?>
<form method="POST" action="">
<table border="1">
<tr>
<?PHP
echo "<tr><td>" . $data['id'] . "</td><td>" . $sr . "</td><td >" . $name . "</a></td><td>" . $price . "</td><td>" . $qunt . "</td> ";
?>
<tr>
</table>
<input type="hidden" name="id2" value="<?php echo $data['id'] ?>"/>
<input type="hidden" name="counter" value="<?php echo $_SESSION['counter']; ?>"/>
<input type="submit" name="button" value="Counter"/>
<input type="submit" name="reset" value="Reset"/>
<br/><?php echo $_SESSION['counter']; ?>
</form>
<?php }
?>