Velvet Star Monitor

Standout celebrity highlights with iconic style.

general

How to get the ID when selected the particular item?

Writer Emily Wong

I have a page that display products and I would like to get the product ID when I click on the particular item and pass it to another page.

May I know how can I achieve this?

I always get the last PID, my code:

<head> <title>Toy-All</title> <!--Wilmos: Using external CSS File to format the page style and fonts.--> <link href="StyleSheet2.css" rel="Stylesheet" type="text/css" />
</head>
<body> <form method = "post" action "getpid.php"> <div> <div> <?php //open connection to MySQL Server $connection = mysql_connect('localhost','root', '') or die ('Unable to connect to !'); // select database for use mysql_select_db('we-toys') or die ('Unable to select database!'); $query = 'SELECT p.*, price.priceN FROM product p, pricing price WHERE p.pid = price.pid and p.PGroup = 1 and p.PType = 1'; $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error()); if (mysql_num_rows($result) > 0) { while ($row = mysql_fetch_array($result)) { echo '<div><img src="'.$row[5].'" width=200px height=200px; ?> </div> <h3>'.$row[1].'</h3> <h1><span> $ '.$row[7].' </span> <input type="hidden" name="pid" value= '.$row[0].' > <input type="Submit" value= "Add to Cart" > </h1> '; } } else { echo "No rows found!"; } mysql_free_result($result); mysql_close($connection); ?> </div> </div> </form>
</body>
</html>
1

3 Answers

If you retrieve your data from $_SESSION['PID'], then you will always get the last ID because you keep reassign new value to that session.

You can just achieve this with a link to the another PHP page. For example:

<a href='anotherPage.php?id=<?php echo $row[0]; ?>'>Add to Cart</a>

A more completed code as requested

<?php $query = 'SELECT p.*, price.priceN FROM product p, pricing price WHERE p.pid = price.pid and p.PGroup = 1 and p.PType = 1'; $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
?>
<?php while ($row = mysql_fetch_array($result)) { ?> <h3><?php echo $row[1]; ?></h3> <a href='anotherPage.php?id=<?php echo $row[0]; ?>'>Add to Cart</a><br><br>
<?php } ?>

And for anotherPage.php code

<?php echo "You are trying to add this product ID to cart: " . $_GET['id'];
?>
7

You can use this form that i also provide in this code.

 $query = 'SELECT p.*, price.priceN FROM product p, pricing price WHERE p.pid = price.pid and p.PGroup = 1 and p.PType = 1'; $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error()); if (mysql_num_rows($result) > 0) { while ($row = mysql_fetch_array($result)) { $pid = ($row[0]); $_SESSION['PID'] = $pid; echo '<div><img src="'.$row[5].'" width=200px height=200px; ?> </div> <h3>'.$row[1].'</h3> <h1><span> $ '.$row[7].' </span> <form method="post" action="cart.php"> <input type="hidden" name="pid" value= '.$row[0].' > <input type="Submit" value= "Add to Cart" > </form> $pid = '.$row[0].'; </h1> '; } }

Now you should make a new page such as cart.php

 echo $_POST['pid'];

If I understand you correctly, the following should work:

<form method="post" action="anotherPage.php"> <input type="hidden" name="id" value="<?php echo "$row[0]"?>"/> <input type="Submit" value="<?php echo "$row[0]" ?>" />
</form>

So basically when you click the product button, the id will be accessible in anotherPage.php

EDIT:

I rewrote your code to improve readability:

<div><img src=<?php echo $row[5]; ?> width=200px height=200px; ?> </div> <h3><?php echo $row[1]; ?></h3> <h1> <span> $ <?php echo $row[7] ?></span> <form method="post" action="cart.php"> <input type="hidden" name="pid" value=<?php $row[0]; ?> > <input type="Submit" value= "Add to Cart" > </form>
<?php $pid = $row[0]; ?> </h1>

Avoid echo-ing out large chunks of HTML where possible. Try it now, If it fails provide the error message.

The above does, what the simple test below achieves:

<?php
$id = 1;
if (isset($_POST['submit_btn'])){ echo $_POST['id'];
}
?>
<form method="post" action="#"> <input type="hidden" name="id" value= <?php echo $id; ?> > <input type="submit" name="submit_btn" value="submit">
</form>
0

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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