user2649074
user2649074

Reputation: 249

How to get the ID when selected the particular item?

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 class="stylediv2-Middle-Toy-all"> 

        <div class="transbox-Toy-all">

        <?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 style="float:left;margin-right: 10px; margin-left: 10px;"><img src="'.$row[5].'"  width=200px height=200px; ?> </div>

                  <h3>'.$row[1].'</h3>

                              <h1><span style="color:red"> $ '.$row[7].' </span>


                              <input type="hidden" name="pid" value= '.$row[0].' >
                              <input id="AddtoCart-Btn" type="Submit" value= "Add to Cart" >





                              </h1> 

                  ';


                }
        }
            else
            {
                echo "No rows found!";
            }
            mysql_free_result($result);
            mysql_close($connection);

            ?>

        </div>
    </div>
   </form>
</body>
</html>

Upvotes: 0

Views: 3113

Answers (3)

kaizenCoder
kaizenCoder

Reputation: 2229

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 id="AddtoCart-Btn" 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 style="float:left;margin-right: 10px; margin-left: 10px;"><img src=<?php echo $row[5]; ?>  width=200px height=200px; ?> </div>

  <h3><?php echo $row[1]; ?></h3>

   <h1>
    <span style="color:red"> $ <?php echo $row[7] ?></span>
    <form method="post" action="cart.php">
      <input type="hidden" name="pid" value=<?php $row[0]; ?> >
      <input id="AddtoCart-Btn" 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>

Upvotes: 0

invisal
invisal

Reputation: 11171

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'];
?>

Upvotes: 2

Masum Billah
Masum Billah

Reputation: 2399

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 style="float:left;margin-right: 10px; margin-left: 10px;"><img src="'.$row[5].'"  width=200px height=200px; ?> </div>

              <h3>'.$row[1].'</h3>

                          <h1><span style="color:red"> $ '.$row[7].' </span>
                  <form method="post" action="cart.php">
                      <input type="hidden" name="pid" value= '.$row[0].' >
                      <input id="AddtoCart-Btn" 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'];

Upvotes: 1

Related Questions