Jump to content

Using PHP with MYSQL for desplaying linked information


kasitzboym

Recommended Posts

Hey everyone,

 

Thanks in advance for any help i may get on this topic.

 

I am trying to take the information from a table on MYSQL database and display the information, depending on the link in the URL, on the page.

 

I have already created a page called staff.php

 

I would like to take this page and display all of images of the staff members in the MYSQL database on this page with the images being link, this i have no problem with. I would then like to created it so that when they click on the staff members image then it will replace the entire page's information with the current staff members information that they have selected with the url showing their name such as    example.com/staff.php?name=bob

 

all of the code i have that already works is displayed below.

 

Could someone help me with this as i need to finish this for the website to be posted online in a timely manner.

 

staff.php

<?php
require("connect.php");
$query="SELECT * FROM staff";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

$i=0;
while ($i < $num) {

$id=mysql_result($result,$i,"id");
$title=mysql_result($result,$i,"title");
$firstname=mysql_result($result,$i,"firstname");
$lastname=mysql_result($result,$i,"lastname");
$bio=mysql_result($result,$i,"bio");
$yearbegan=mysql_result($result,$i,"yearbegan");
$imagename=mysql_result($result,$i,"imagename");


echo "<a href=\staffmember.php?name=$firstname$lastname><img src=\"staffpicturesthumb/$imagename\" width=\"150\" height=\"200\" /></a>  <br>";


$i++;
}

?>

 

Once again thanks for any help in advance!!!

:D

Link to comment
Share on other sites

The url actually worked exactly like i wanted it and does display example.com/staff.php?name=BobJones

 

So the url that its linking to doesn't actually have any problems i just have no idea how to display the information that is in the MYSQL database.

 

I was told i have to put it into an array and use the $_GET[] function but i have no idea how to do that with mysql

Link to comment
Share on other sites

First, it would be easier just to use the ID number of the staff member instead of first / last name, but using your method, you could do it like this:

 

echo "<a href=\staffmember.php?first=$firstname&last=$lastname><img src=\"staffpicturesthumb/$imagename\" width=\"150\" height=\"200\" /></a>  <br>";

 

and then on staffmember.php you would call their data like this:

 

$first=$_GET['first'];
$last=$_GET['last'];

$sql="select * from table WHERE firstname=$first, lastname=$last"
$result=mysql_query($sql, $db);
while $row=(mysql_fetch_array($result)) {
CODE HERE
};

 

That should do it ...

 

 

Link to comment
Share on other sites

What I meant by ID, is if you have your staff table set up like this:

 

ID (unique)

firstname

lastname

other1

other2

other3 ...etc

 

then you can simply pass the ID (index) like this:

 

echo "<a href=\staffmember.php?ID=$ID>

 

Then on the next page, you can use :

 

$ID=$_GET['ID'];
sql="select * from table where ID=$ID"
$result=mysql_query($sql, $db);
  while $row=(mysql_fetch_array($result)) 
     {
echo "First name: ".$row['firstname']."<br>";
echo "Last name: ".$row['lastname']."<br>";
echo "Other: ".$row['other']."<br>";
echo "Other1: ".$row['other1']."<br>";
     };

and so on .

 

Link to comment
Share on other sites

it's just an easier way to connect to the database ... you have require("connection.php"), so in that file, make it look like this:

 

<?php
$host = "yourhost";
$username = "username";
$password = "password";
$dbname = "database name";
$db = mysql_connect($host, $username, $password);
mysql_select_db($dbname,$db);
?>

 

and then from now on you can do your queries fast as shown...

 

$sql="sql statement";
$result=mysql_query($sql, $db);

 

it's just an easier method of writing out your sql connections.

 

Link to comment
Share on other sites

Alright i seam to be having some trouble with my code and i dont understand where it is hanging or why

 

the code i have in staffmember.php is

 

<?php

require("connect.php");
$first=$_GET['first'];
$last=$_GET['last'];

$sql="select * from staff WHERE firstname=$first, lastname=$last";
$result=mysql_query($sql, $connect);
while $row=(mysql_fetch_array($result)) {


$id=mysql_result($result,"id");
$title=mysql_result($result,"title");
$firstname=mysql_result($result,"firstname");
$lastname=mysql_result($result,"lastname");
$bio=mysql_result($result,"bio");
$yearbegan=mysql_result($result,"yearbegan");
$imagename=mysql_result($result,"imagename");

echo "$firstname  $lastname   $bio   $yearbegan

};

?>

 

and the error it is giving me in the web browser is

 

Parse error: syntax error, unexpected T_VARIABLE, expecting '(' in C:\xampp\htdocs\sahnew\Final\staffmember.php on line 20

 

 

Line 20 is

 

while $row=(mysql_fetch_array($result)) {

 

Any ideas?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.