Search code examples
phpmysqldata-transfer

PHP to take information from one page to another


i am quite new to php and i have this shopping cart up which shows images. product name and details from mysql database. the code for it is the following:

<table border="0" cellpadding="2px" width="600px">
    <?
        $result=mysql_query("select * from products");
        while($row=mysql_fetch_array($result)){

    ?>
    <tr>
        <td><img src="<?=$row['picture']?>" /></td>
        <td>    <b><?=$row['name']?></b><br />
                <?=$row['description']?><br />
                Price:<big style="color:green">
                    £<?=$row['price']?></big><br /><br />
                <input type="button" value="Add to Cart" onclick="addtocart(<?=$row['serial']?>)" />
        </td>
    </tr>
    <tr><td colspan="2"><hr size="1" /></td>
    <? } ?>
</table>

What i want is for a user to be able to click on the picture/name of the product in which it should transfer the user to another page with the selected product being shown.

By the way, my mysql table consists of the following fields:

serial name description price picture

how should i go about doing this? thank u


Solution

  • Make a link like this:

    <a href="product.php?product_id=<?=$row['serial']?><?=$row['name']?></a>
    

    Then in the product.php page insert this:

    $id = $_GET['product_id'];
    $result=mysql_query("select * from products WHERE serial = '$id'");
    $row=mysql_fetch_array($result);
    

    Under that code you can add stuff like the picture and price etc etc :)

    The serial of the product is being held ni the URL (product_id) and then called by the $_GET variable. SO product_id=1 will load the product with the serial number 1.

    If you look at the URL of this current page it has the number 8142009 in the middle. If you change it to 8142008 it will load the previous question before yours. This example works in the exact same way.