need to load ONE mysql row into into table with one click

Discussion in 'MySQL' started by Darwyn, Jun 24, 2010.

  1. #1
    I'm working on a product page for my company. I have a database that contains all of our product information in it. Model number, computer number, description, etc...

    I need to be able to populate a table with the correct product information when a user clicks the product. How would I do that?

    My menu is this:
    <ul class="menucontents">
    	<li><a href="#">product 1</a></li>
    	<li><a href="#">product 2</a></li>
    	<li><a href="#">product 3</a></li>
    </ul>
    HTML:
    and the table that should hold the information with php is:
    <table class="product_text">
      <tr>
        <td><b><?php echo $row_test['modelNo']; ?></b></td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td><?php echo $row_test['description']; ?></td>
        <td><?php echo $row_test['features']; ?></td>
      </tr>
      <tr>
        <td><?php echo $row_test['pressRate']; ?></td>
        <td><img src="<?php echo $row_test['imagePath']; ?>"/></td>
      </tr>
    </table>
    PHP:
    What do I need in the <a href> tag to have it populate product 1 information when clicked...or product 2...or 3?
     
    Darwyn, Jun 24, 2010 IP
  2. plog

    plog Peon

    Messages:
    298
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #2
    You should have it link to that php file and include the primary key of the table you are pulling from in the query string. For example:

    <ul class="menucontents">
    <li><a href="phpfile.php?product=1">product 1</a></li>
    <li><a href="phpfile.php?product=2">product 2</a></li>
    <li><a href="phpfile.php?product=3">product 3</a></li></ul>
    PHP:

    Just to be clear, instead of 'phpfile.php' that will be the name of the file that will display your product information, and 'product' is just the variable name that will be passed to that file so that it can look up the specific product you want.
     
    plog, Jun 25, 2010 IP
  3. Darwyn

    Darwyn Peon

    Messages:
    34
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks!

    I was able to set my primary key to the model number column (modelNo) within mysql, but if I simply adjust my href tag to:

    <ul class="menucontents">
    <li><a href="phpfile.php?modelNo=product1">product 1</a></li>
    <li><a href="phpfile.php?modelNo=product2">product 2</a></li>
    <li><a href="phpfile.php?modelNo=product3">product 3</a></li></ul>
    PHP:
    it doesn't work. Am I missing something with the MYSQL code? Or did I just not write my href tag right?
     
    Darwyn, Jun 28, 2010 IP
  4. plog

    plog Peon

    Messages:
    298
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #4
    It could be a number of things. What is the problem you are having when you click the link? Is the script not finding the value or is the link itself bad?

    Is that the actual HTML you are producing? If not, could you post the actual HTML? If so, I think its because you have "product" after the equals sign in the href. Most likely your 'modelNo' column is a number field and not a text field. Based on the query string of your href value you are passing it a string (i.e. "=product1", "=product2") instead of a number ("=1", "=2").


    Let me know about the HTML and I can be more helpful.
     
    plog, Jun 28, 2010 IP
  5. Darwyn

    Darwyn Peon

    Messages:
    34
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I posted the actual html, yes...

    <ul class="menucontents">
    <li><a href="phpfile.php?modelNo=product1">product 1</a></li>
    <li><a href="phpfile.php?modelNo=product2">product 2</a></li>
    <li><a href="phpfile.php?modelNo=product3">product 3</a></li>
    </ul>
    HTML:
    My database column for modelNo is set as a VARCHAR field and is the primary. The actual model numbers (which would replace the "product1" text) is a series of numbers and letters...for instance: T100NE

    So my menu looks like this...

    <li><a href="phpfile.php?modelNo=T100NE">T-100NE</a></li>
    HTML:
    I can get the script to work if, in the "phpfile.php", I use a WHERE statement in the query...

    <?php
    mysql_select_db($database_products, $products);
    $query_test = "SELECT modelNo, description, features, pressRate, imagePath FROM Products WHERE modelNo='T-100NE'";
    $test = mysql_query($query_test, $products) or die(mysql_error());
    $row_test = mysql_fetch_assoc($test);
    $totalRows_test = mysql_num_rows($test);
    
    ?>
    PHP:
    But obviously, that's not very efficient as I'd need to create as many php files as there are products.

    Here is the link to the working example:

    http://www.jomarvalve.com/JomarValve/products.php

    Under "Ball Valves - Brass" it drops down and you will see T-100NE. If you click it, it will load everything dynamically. But that's using a WHERE statement in the phpfile.php as I mentioned above.

    I'm not sure what I'm doing wrong. How does php know to reference the PRIMARY key in the database (T-100NE) in the following link? <a href="phpfile.php?modelNo=T100NE">

    I think I'm missing some sort of variable in the phpfile.php script to do that. but I dont know.
     
    Darwyn, Jun 29, 2010 IP
  6. Darwyn

    Darwyn Peon

    Messages:
    34
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Haha, I was right! I got it to work!

    Turns out I needed to grab the model number (modelNo) database field as a variable. My new processing code is:

    <?php
    $modelNo = $_GET['modelNo']; 
    mysql_select_db($database_products, $products);
    $query_test = "SELECT modelNo, description, features, pressRate, imagePath FROM Products WHERE modelNo='$modelNo'";
    $test = mysql_query($query_test, $products) or die(mysql_error());
    $row_test = mysql_fetch_assoc($test);
    $totalRows_test = mysql_num_rows($test);
    
    ?>
    PHP:
     
    Darwyn, Jun 29, 2010 IP
  7. plog

    plog Peon

    Messages:
    298
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #7
    Good to hear you have it working. Just one suggestion--sanitize your _$GET variables because the script as you have it is susceptible to SQL injection. You need to use mysql_real_escape_string(). Here's a link to an explanation of it:

    http://us2.php.net/manual/en/function.mysql-real-escape-string.php

    And how you use it is in the first line of your code which should become:

    $modelNo = mysql_real_escape_string($_GET['modelNo']); 
    PHP:
     
    plog, Jun 30, 2010 IP
  8. Darwyn

    Darwyn Peon

    Messages:
    34
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Excellent! Thank you very much! :D I would never have known to do that had you not said anything.
     
    Darwyn, Jun 30, 2010 IP