Well,

I have a database with guides on it, sorted by their ID, which is auto increment and the primary key.

What I'm trying to do is have a page called guides.php, this will show a table containing the guide names with links to the guides.

The links will be guides.php?id=1
ID number according to the ID in the database.

The guide page will display the information from the database.

Guide Name: $_GET['quest_id'];

As you can probably tell, I'm really knew to the $_GET variable so I'm not sure where to start. I've probably even messed up trying to explain it.

<?php

$host = "-----";
$user = "-------";
$pass = "------";
$db   = "------";

$guide_id = $_GET['guide_id'];
if ($guide_id == NULL) {
echo "Table of guide names";
}

$ms = mysql_pconnect($host, $user, $pass);
if ( !$ms )
{
echo "Error connecting to database.\n";
}

mysql_select_db($db);

mysql_query("SELECT * FROM guides WHERE id = '".mysql_real_escape_string($id)."'");
mysql_query("SELECT * FROM guides WHERE guidename = '".mysql_real_escape_string($guide_name)."'");

echo ("$guide_id");
echo ("$guide_name");

?>

Above is my very poor attempt at trying to do it.

Any help is greatly appreciated.

    test if a variable has set, with isset() or empty()

    if(empty($_GET['guide_id']))
    {
    die("You did not select a guide");
    }

    If you exept only one row from a table, use the LIMIT keyword in your SQL query.
    You can use sprintf() to build sql queries, it has cast functions to convert unsafe strings into numbers. If you need to handle strings, then use the mysql_real_escape_string()

    $sql=sprintf("SELECT * FROM guides WHERE id = %d LIMIT 1"  , $_GET['guide_id'] ) ;
    $res=mysql_query($sql) or die(mysql_error() . " In query: ". $sql );
    if(mysql_num_rows($res)==0)
    die("No quide found");
    $guide= mysql_fetch_assoc($res);
    printf("ID: %d is %s <br />"  $_GET['guide_id'] ,  $guide["guide_name"]); 
    
    ?>

    empty()
    isset()
    mysql_error()
    mysql_fetch_assoc()
    mysql_num_rows()
    mysql_real_escape_string()
    sprintf()

      Well, I've had a mess around and I still can't get anything to display.

      Any ideas?

        I've now messed around and I can display the information from the database.

        <?php
        
        $host = "localhost";
        $user = "------";
        $pass = "------";
        $db   = "-----c";
        
        // This part sets up the connection to the
        // database (so you don't need to reopen the connection
        // again on the same page).
        $ms = mysql_pconnect($host, $user, $pass);
        if ( !$ms )
        {
        echo "Error connecting to database.\n";
        }
        
        // Then you need to make sure the database you want
        // is selected.
        mysql_select_db($db);
        
        $query="select * from quests";  // query string stored in a variable
        $rt=mysql_query($query);          // query executed 
        echo mysql_error();                    // if any error is there that will be printed to the screen
        
        while($nt=mysql_fetch_array($rt)){
        echo "$nt[id] $nt[questname] $nt[description]<br>";     // name class and mark will be printed with one line break at the end
        }
        
        ?>

        But I have no idea how to make it work depending on the id variable.

          build links for your details .php page in the while cycle:

          while($nt=mysql_fetch_array($rt)){
          $url="<a href=\"details.php?guide_id={$nt["id"]}\">--Details--</a>";
          print "{$url} - {$nt["questname"]} {$nt["description"]}<br>";     
          // name class and mark will be printed with one line break at the end }

            I don't understand, can you elaborate.

              When you list the records from your database, you need to make links which pass the primary KEY to that page to identify that row.

              while($nt=mysql_fetch_array($rt)){
              echo "<a href='details.php?guide_id={$nt["id"]}'>view</a> - {$nt["questname"]} <br>";
              }

              now, if you press on the view link, details.php will get via $_GET the record's ID.

              is this understandable till now?

              jjozsi

                Yes, I understand that.

                But what I want guides.php to do is completely different.

                What I want it to do when I navigate to guides.php is display a table with the guide name and the link to the guide.

                The link to the guide will be decided by the ID in the database.

                Example:

                If a guide is called PHP - Beginner and has an ID on the MySQL database of 5, then I would go to guides.php and click "PHP - Beginner" on the list, this would then take me to guides.php?id=5, this page would show me the guide information from the other fields in the database, instead of the table.

                  in this example, you pass a guide ID: guides.php?id=5,

                  on the quides.php you reach its value with : $_GET["id"]

                  insert this value into an SQL query, print it, and get the results.

                  if(!empty($_GET["id"]))
                  {
                  $sql= sprintf("select * from guides where id=%d ", $_GET["id"]);
                  $result=mysql_query($sql) or die(mysql_error());
                  //.... fetch the results and print them
                  //...
                  
                  
                  }
                  else
                  print "id is empty!";

                    I still have no idea what to do.

                      lets see this example code:

                      http://phpcode.hu/detailspage.rar

                      this can show you how to list a table, and make links for details fields.
                      And in details.php it can show that entry with the details field.

                      with this schema you can make different table design.

                        Thanks a lot for your help! Greatly appreciate it.

                          feel free to ask if you got questions with the example code.

                            Write a Reply...