Passing variables (I think) ??

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Phil Latio

    Passing variables (I think) ??

    I have below a quite simple php page which queries a database table of all
    records and outputs the result. However on this page I only output 3 of the
    fields and I would like to click on a particular row which would then bring
    up a detail page (which displays all the fields (perhaps 10 or more) of that
    record).

    Do I create another php file called detail.php which includes the line:
    $sql = 'SELECT * FROM `link` WHERE link_id = $id;

    and then alter the echo line below to something like:
    echo "<TR><TD><a
    href=detail.php ?link_id=$id</TD><TD>$title</TD><TD>$descrip tion</TD></TR>";

    which would then pass the variable $id into the sql statement in detail.php.
    ??

    Any advice on the next steps would be welcome.

    Cheers

    Phil


    <html>
    <body>
    <?php

    // listrecords.php
    // lists brief details of records (1 record per row)

    $localhost = 'localhost';
    $username = 'username';
    $password = 'password';
    $database = 'database';

    $dbconnection = mysql_connect($ localhost, $username, $password);
    if (! $dbconnection)
    {
    die ("Couldn't connect to MySQL");
    exit;
    }

    $db=mysql_selec t_db($database, $dbconnection);
    if (!$db)
    {
    echo "Could not select database";
    exit;
    }

    $sql = 'SELECT `link_id`,`titl e`,`description ` FROM `links` LIMIT 0, 30';

    $mysql_result=m ysql_query($sql ,$dbconnection) ;
    $num_rows=mysql _num_rows($mysq l_result);

    if ($num_rows == 0) {
    echo "Sorry, we have no records";
    }
    else
    {

    echo "<TABLE ALIGN=\"CENTER\ " BORDER=\"1\">";
    echo "<TR><TH>Re cord ID</TH><TH>Title</TH><TH>Descript ion</TH></TR>";

    while ($row=mysql_fet ch_array($mysql _result))
    {
    $id=$row["link_id"];
    $title=$row["title"];
    $description=$r ow["descriptio n"];

    echo "<TR><TD>$i d</TD><TD>$title</TD><TD>$descrip tion</TD></TR>";

    }
    }
    ?>
    </TABLE>
    </body>
    </html>


  • micha

    #2
    Re: Passing variables (I think) ??


    Phil Latio wrote:[color=blue]
    > I have below a quite simple php page which queries a database table[/color]
    of all[color=blue]
    > records and outputs the result. However on this page I only output 3[/color]
    of the[color=blue]
    > fields and I would like to click on a particular row which would then[/color]
    bring[color=blue]
    > up a detail page (which displays all the fields (perhaps 10 or more)[/color]
    of that[color=blue]
    > record).
    >
    > Do I create another php file called detail.php which includes the[/color]
    line:

    yes, that is a way
    [color=blue]
    > $sql = 'SELECT * FROM `link` WHERE link_id = $id;[/color]

    this is not so good, because:

    1. if register_global s is off (which it should be), $link_id gets not
    automatically registered. use $_GET['link_id']

    2. $_GET['link_id'] is user submitted data, so never ever trust it.
    validate. i suppose your id's are integers, so using
    (int)$GET_['link_id'] forces anything that is sent into an integer,
    which in the worst case is just 0.
    [color=blue]
    > and then alter the echo line below to something like:
    > echo "<TR><TD><a
    >[/color]
    href=detail.php ?link_id=$id</TD><TD>$title</TD><TD>$descrip tion</TD></TR>";

    yes, like that.

    micha

    Comment

    • Phil Latio

      #3
      Re: Passing variables (I think) ??

      > > $sql = 'SELECT * FROM `link` WHERE link_id = $id;[color=blue]
      >
      > this is not so good, because:
      >
      > 1. if register_global s is off (which it should be), $link_id gets not
      > automatically registered. use $_GET['link_id']
      >
      > 2. $_GET['link_id'] is user submitted data, so never ever trust it.
      > validate. i suppose your id's are integers, so using
      > (int)$GET_['link_id'] forces anything that is sent into an integer,
      > which in the worst case is just 0.[/color]

      Many thanks for replying.

      Are you suggesting something along these lines?

      $specific_link = (int)$_GET['link_id'];
      $sql = 'SELECT * FROM `link` WHERE link_id = $specific_link;

      Cheers

      Phil


      Comment

      • micha

        #4
        Re: Passing variables (I think) ??


        Phil Latio wrote:[color=blue][color=green][color=darkred]
        > > > $sql = 'SELECT * FROM `link` WHERE link_id = $id;[/color]
        > >
        > > this is not so good, because:
        > >
        > > 1. if register_global s is off (which it should be), $link_id gets[/color][/color]
        not[color=blue][color=green]
        > > automatically registered. use $_GET['link_id']
        > >
        > > 2. $_GET['link_id'] is user submitted data, so never ever trust it.
        > > validate. i suppose your id's are integers, so using
        > > (int)$GET_['link_id'] forces anything that is sent into an integer,
        > > which in the worst case is just 0.[/color]
        >
        > Many thanks for replying.
        >
        > Are you suggesting something along these lines?
        >
        > $specific_link = (int)$_GET['link_id'];
        > $sql = 'SELECT * FROM `link` WHERE link_id = $specific_link;
        >
        > Cheers
        >
        > Phil[/color]


        yes.

        general.php contains

        <a href=detail.php ?link_id=YOUR_I NT_VALUE>link</a>

        and detail.php copntains the above code.

        micha

        Comment

        • Phil Latio

          #5
          Re: Passing variables (I think) ??

          > yes.[color=blue]
          >
          > general.php contains
          >
          > <a href=detail.php ?link_id=YOUR_I NT_VALUE>link</a>
          >
          > and detail.php copntains the above code.
          >
          > micha[/color]

          Thanks again.

          Cheers

          Phil


          Comment

          Working...