Jump to content

MySQL/PHP Query


craig1978

Recommended Posts

Hi Guys,

 

Last time I posted you were awesome, Thanks!!

 

Here's my question:

 

I don't have any code to post as I actually don't know where to even start.

 

With your help I have built a MySQL database and created a search page to display results.

 

I now want to make each result a link to a page displaying more details about each result. I would prefer not to create a different HTML page about each result, and would prefer to create some sort of PHP script to get the data (including photos) from the database. I just think this would make it easier to manage, only 1 template page vs 100's.

 

Any help or ideas would be greatly appreciated.

 

Thanks

 

Craig

Link to comment
Share on other sites

Use the GET method

Link your search result to your details page by passing the ID in de URL. The details page extracts the id from the URL and executes a mysql_query which gets the row which had that particular id.

 

Search results page:

<a href="details.php?id=<?= $row_seach_result['id'] ?>"><?= $row_seach_result['title'] ?></a>

 

And your details page:

if(isset($_GET['id'])) {
  $id = $_GET['id'];
  $result=mysql_query("SELECT * FROM table WHERE id='$id'");
  $row_result=mysql_fetch_assoc($result);
}

Link to comment
Share on other sites

Just a few questions:

 

Sorry I am pretty new to any coding let alone PHP.

 

1.Do I need to post anything else in the details.php page??

 

2.Where exactly do I put the code on the results.php page??

 

3.Does it matter that I used the POST method to display the results page from the search form??

 

I will post the code I used below:

 

This is the details.php

 

<?

 

include 'Config.php'

include 'OpenDB.php'

 

 

if(isset($_GET['Thumbnail'])) {

  $id = $_GET['Thumbnail'];

  $result=mysql_query("SELECT * FROM PersonalTrainers WHERE Thumbnail='$Thumbnail'");

  $row_result=mysql_fetch_assoc($result);

}

 

 

include 'CloseDB.php'

?>

 

This is the results.php

 

<body>

 

 

<?

 

include 'Config.php';

include 'OpenDB.php';

 

$query = "SELECT * FROM PersonalTrainers WHERE Postcode = '".$_POST['Postcode']."' AND Age = '".$_POST['Age']."'

AND Sex = '".$_POST['Sex']."'";

$result=mysql_query($query);

 

$num=mysql_numrows($result);

 

mysql_close();

 

echo "<b><center>Personal Trainers</center></b><br><br>";

 

$i=0;

while ($i < $num) {

 

$Thumbnail=mysql_result($result,$i,"Thumbnail");

$First_Name=mysql_result($result,$i,"First_Name");

$Last_Name=mysql_result($result,$i,"Last_Name");

$Email=mysql_result($result,$i,"Email");

$Suburb=mysql_result($result,$i,"Suburb");

$Postcode=mysql_result($result,$i,"Postcode");

$Age=mysql_result($result,$i,"Age");

 

echo "<center>$Thumbnail<br><b>$First_Name $Last_Name</b><br>Email: $Email<br>Suburb: $Suburb<br>Postcode: $Postcode<br>Age: $Age<br><hr><br></center>";

 

$i++;

}

 

 

?>

 

<a href="details.php?id=<?= $Thumbnail['Thumbnail'] ?>"><?= $Email['Email'] ?></a>

 

</body>

 

 

Thanks in advance

 

Craig

Link to comment
Share on other sites

1. No, only showing the details

2. You put the <a href=..... in the while loop

3. No, The difference between GET and POST is in the way of sending data to the page, while the GET method sends data using URL, the POST method sends them through the standard entrance STDIO.

 

Does every 'Personal Trainer' have an unique number (ID)? Or is Thumbnail unique? You need something unique to pass to the details page so the details page knows which trainer's details to show...

 

Try this code in your results page:

$query = "SELECT * FROM PersonalTrainers WHERE Postcode = '".$_POST['Postcode']."' AND Age = '".$_POST['Age']."'
AND Sex = '".$_POST['Sex']."'";
$result=mysql_query($query);
$row_result = mysql_fetch_assoc($result);
mysql_close();

echo "<center>Personal Trainers</center>";

while ($row_result = mysql_fetch_assoc($result)) {
  echo '<center><a href="details.php?id='.$row_result['Thumbnail'].'">'.$row_result['Thumbnail'].'</a></center>';
}

 

Change in the details page:

$id = $_GET['Thumbnail'];

 

into:

 

$Thumbnail = $_GET['Thumbnail'];

Link to comment
Share on other sites

Hey Thanks,

 

Got the Thumbnail to be a link to the Details.php page, but it only displays the message:

 

"No input file specified"

 

Any ideas??

 

Do I need to do more on the Detail page apart from what I posted??

 

Any echos or anything??

 

Thanks

 

Craig

Link to comment
Share on other sites

One other thing,

 

I haven't changed other parts of the code but my results page no longer outputs First and Last names.

 

Did i ruin something when I entered the href stuff:

 

echo "<b><center>Personal Trainers</center></b><br><br>";

 

$i=0;

while ($row_result = mysql_fetch_assoc($result))

 

{

echo '<center><a href="details.php?id='.$row_result['Thumbnail'].'">'.$row_result['Thumbnail'].'</a></center>';

 

$Thumbnail=mysql_result($result,$i,"Thumbnail");

$First_Name=mysql_result($result,$i,"First_Name");

$Last_Name=mysql_result($result,$i,"Last_Name");

$Email=mysql_result($result,$i,"Email");

$Suburb=mysql_result($result,$i,"Suburb");

$Postcode=mysql_result($result,$i,"Postcode");

$Age=mysql_result($result,$i,"Age");

 

echo "<center><br><$First_Name $Last_Name<br>Email: $Email<br>Suburb: $Suburb<br>Postcode: $Postcode<br>Age: $Age<br><hr><br></center>";

 

$i++;

}

 

Sorry guys, it's probably really easy.

 

Here's the code for Details.php

 

<?

 

include 'Config.php';

include 'OpenDB.php';

 

 

if(isset($_GET['Thumbnail'])) {

  $Thumbnail = $_GET['Thumbnail'];

  $result=mysql_query("SELECT * FROM PersonalTrainers WHERE Thumbnail='$Thumbnail'");

  $row_result=mysql_fetch_assoc($result);

}

 

include 'CloseDB.php';

 

?>

 

 

Thanks

 

Craig

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.