SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2002
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP Passing Variable through URL Help

    I want to pass a variable through to another page which executes a sql query for the variable it recieves.

    news.php


    <?php

    $query = 'SELECT product_id from product';

    $result = mysql_query($query);

    while($row = mysql_fetch_array($result))
    {
    $p_id = $row["product_id"];
    $name_id = $row["product_name"];



    echo "(<a href=""\"show_news.php?product_id=$p_id\">NEws..a>)</p>\n";
    }


    ?>



    Show_news.php


    <?php

    $name = $_GET['product_id'];
    echo $name;

    $query = 'SELECT product_name from product where product_id = $name';

    $result = mysql_query($query);

    while($row = mysql_fetch_array($result))
    {
    echo $row[product_name];
    }


    ?>



    I Keep gettin the same error. I know this refers to the $name I have used in the SQL query. But I am not sure how else to do it. It is echoing out the correct $name variable to.

    Any suggestions?

  2. #2
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What a difference it would make if only people would break out of PHP when they require a A HREF link passing some variables.

    Point: With a decent editor that can highlight syntax; you don't see bugs when you ECHO a A HREF link.

    Try (if this isn't chewed up again):

    .
    .
    $name_id = $row["product_name"];
    ?>
    <p>
    <a href='show_news.php?product_id=<?php echo($p_id); ?>'>news</a>
    </p>
    <?php
    .
    .
    .

    Also there is a very apparent error within your A HREF link anyways... though for your future sanity you'll know better to break out of PHP for using HTML markup.
    Last edited by Dr Livingston; Mar 29, 2003 at 06:26. Reason: the editor tool chewed up the script - it crapped out LoL

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2002
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dr Livingston
    What a difference it would make if only people would break out of PHP when they require a A HREF link passing some variables.

    Point: With a decent editor that can highlight syntax; you don't see bugs when you ECHO a A HREF link.

    Try (if this isn't chewed up again):

    .
    .
    $name_id = $row["product_name"];
    ?>
    <p>
    <a href="show_news.php?product_id=<?php echo($p_id); ?>">news</a>
    </p>
    <?php
    .
    .
    .

    Also there is a very apparent error within your A HREF link anyways... though for your future sanity you'll know better to break out of PHP for using HTML markup.
    Thanks for reply. Still stuck though!

    news.php
    PHP Code:
    <?php 

    $query 
    'SELECT product_id from product';

    $result mysql_query($query);

    while(
    $row mysql_fetch_array($result))
    {
    $name_id $row["product_id"];


    ?>
    <a href="show_news.php?product_id=<?php echo($name_id); ?>">news</a>

    <?php ?>
    show_news.php
    PHP Code:
    <?php 

    $name_id 
    $_GET['product_id']; 
    echo 
    $name_id;
    $query 'SELECT product_id, product_name from product where product_id = $name_id';
    $result mysql_query($query);

    while(
    $row mysql_fetch_array($result))
    {
    echo 
    $row[product_id];
    echo 
    $row[product_name];
    }


    ?>
    The problem seams to lie with the $name_id I have used in the query. If i set it to =1,2,3 etc then it works fine. Any suggestions?
    Last edited by FliKman; Mar 30, 2003 at 04:13.

  4. #4
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Strange though not seeing the part of the script for the FORM. This is how I query the database based on variables - see how that works first:

    PHP Code:
    $query 'SELECT product_id, product_name from product where product_id = $name_id'
    To:

    PHP Code:
    $query "SELECT product_id, product_name from product where product_id = '"$name_id ."'"
    Hope that helps, if not then maybe you can post the script used for the FORM and it's logic ?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •