0

Sorry if my Title is crappy but I've looked everywhere and i just don't know how to do this.

OK. what i want to do is display information from a specific id from a table row.

first page

employees.php

<?php

require 'header.php';

require 'connect.php';

$sql1 = mysql_query("SELECT * FROM employees ORDER BY id ASC");
while($runrows = mysql_fetch_array($sql1)){

    $employename = $runrows["employename"];
    $minidescription = $runrows["minidescription"];
    $bigdescription = $runrows["bigdescription"];

echo "

    <!-- Employe Profile Start -->
        <div class='ProfileWrap'>
            <section class='Profile'>
                <div class='HeadShot'>
                    <div class='Separator'></div>
                    <img width='90' height='136' alt='Employe Headshot' class='EmployeImage' src=img/headshots/".$runrows['images'] ." />
                    <div class='EmployeInfo'>
                        <legend class='EmployeName'>
                            <b>
                                Employe Name: $employename
                            </b>
                        </legend>
                        <div class='EmployeDes'>
                            <p>
                                Employe Descript $minidescription...
                            </p>
                        </div>
                        <a href='readmore.php?id=" .$id = $runrows["id"]. "' id='demo' alt='Read More'>
                            <div class='ReadMore'>
                                <b>
                                    Read More
                                </b>
                            </div>
                        </a>
                    </div>
                </div>
            </section>
        </div>
    <!-- employe Profile End -->

";

} // close while loop
?>
<?php require 'footer.php'; ?>

second page

employe.php

<?php
    require 'header.php';

    require 'connect.php';

echo "<a href='index.php'>Back</a>";

    $sql2 = mysql_query("SELECT * FROM employees WHERE id=$id");
    while($runrows = mysql_fetch_array($sql2)){

    $id = $runrows["id"];
    $employename = $runrows["employename"];
    $minidescription = $runrows["minidescription"];
    $bigdescription = $runrows["bigdescription"];

    echo "
        <legend class='EmployeName'>
        <b>
            Employe Name: $employename
        </b>
        </legend>
        <div class='EmployeDes'>
        <p>
            Employe Description: $bigdescription...
        </p>
        </div>
    ";
    };

    require 'footer.php';
?>

and you would click

[Read More]

then it would go to another page called readmore.php

"Click" [Read More] -> readmore.php?id=14 -> display specific info from that id from the database.

username

minidescription

->

click [Read More]

then it would show up like readmore.php?id=14 in the small address bar at the
bottom left

->

new page

->

largedescription

i want to be able to click on an item in a site that has a read more button and have it take me to another page where it displays the description info for that specific id

yes i realize I'm a complete newbie but I'm still learning and that was a crappy example of what i want to accomplish but i hope you understand what I'm trying to do none the less.

sorry if this already exists but I've looked everywhere and couldn't find what i was looking for. If someone has a link to share that can do what I've asked this question can just be deleted.

Thanks in Advance! hope someone can help me figure this out.

  • 1
    On your read more page, select all the fields you want displayed by ID, pass the id to that page, and then render the display? – Jon Feb 18 '14 at 21:55
  • On the second page, `employe.php`, it looks like you're using `$id` without getting it from `$_GET['id']` first. Also, mysql_* functions are deprecated. Mysqli or PDO is a better way to go. You're wide open to sql injection attacks with the code as is. – Matthew Johnson Feb 19 '14 at 22:36
  • Side note, unless you're using HTML5, a `div` inside an anchor tag is syntactically incorrect. Check this [SO Answer](http://stackoverflow.com/questions/1827965/is-putting-a-div-inside-an-anchor-ever-correct) for more information. – Matthew Johnson Feb 19 '14 at 22:39

2 Answers2

1

First, note @Matthew Johnson's answer about using Mysqli or PDO. Here are a few code specifics, though. When you generate the link to the page, you need this:

<a href='readmore.php?id=" . $runrows["id"] . "' id='demo' alt='Read More'>

Using $id = $runrows["id"] doesn't place the value into the url, it simply declares the value of the $id variable.

Then in your readmore.php file, the id can be capture from the URL using the $_GET array:

if (isset($_GET['id'])) {
    $id = $_GET['id'];
}
larsAnders
  • 3,813
  • 1
  • 15
  • 19
0

The mysql_* functions are deprecated, and should no longer be used. Mysqli or PDO should be used, along with prepared statements. The code as you have it is susceptible to sql injection attacks. A simplified version of what you're trying to do would look something like this:

To Link:

//this gets all the name and mini, loops through and displays....
$stmt = $mysqli->prepare("SELECT id, employename, minidescription FROM employees"); 
$stmt->execute();  
$stmt->bind_result($id, $employeename, $minidescription); 
while($stmt->fetch()) {
    echo "<p><a href='readmore.php?id=$id'>$employeename</a>: $minidescription</p>";
}

The Read More:

//make sure it's set, if so assign it...
$id = (isset($_GET['id']) ? $_GET['id'] : "";

//this gets the info using the id variable from the URL...
$stmt = $mysqli->prepare("SELECT employename, minidescription, bigdescription FROM employees WHERE id = ?");
$stmt->bind_param("i", $id); 
$stmt->execute();
$stmt->bind_result($employeename, $minidescription, $bigdescription); 
$stmt->fetch();

echo "$employeename: $bigdescription";

Using mysqli and prepared statements, as shown here, protects you against bobby tables and sql injection attacks. You can learn more about mysqli from the manual. Here's a tutorial with a quick run through of how prepared statements work.

Edit: The code above still needs a database connection. The warning of an undefined variable is saying that the $mysqli variable hasn't been defined. The fatal error is due to the fact that the prepare statement failed. To create a connection, it would look similar to this:

define("HOST", "Host URL"); 
define("USER", "dbUser"); 
define("PASSWORD", "password");
define("DATABASE", "databaseName"); 

$mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE);

This would replace the code in your connect.php.

Matthew Johnson
  • 4,875
  • 2
  • 38
  • 51
  • hey how to i get this to work do i replace the the part $sql1 = mysql_query("SELECT * FROM drlistings ORDER BY id ASC"); and the while with what you mentioned? because i tried and it didn't work for me it says Notice: Undefined variable: mysqli in C:\xampp\htdocs\xampp\Sandbox\sitename\1.php on line 8 and Fatal error: Call to a member function prepare() on a non-object in C:\xampp\htdocs\xampp\Sandbox\sitename\1.php on line 8 i made a new file with what you gave me and they didn't work. any idea why? – Travis Singleton Feb 20 '14 at 04:43
  • The `$mysqli` variable hasn't been defined, so the `prepare()` statement failed. I gave an example of the code that would replace what is currently in your `connect.php`. It defines the `$mysqli` variable and creates the connection. It must be included prior to the use of the $mysqli variable. – Matthew Johnson Feb 20 '14 at 14:47
  • Thanks a million dude! It's working now just like what i wanted it to. :) – Travis Singleton Feb 20 '14 at 18:36