Jump to content

[SOLVED] Different database results using a variable and hard coding in a search value.


php_beginner_83

Recommended Posts

Hi

 

I'm trying to create an online photo gallery and I'm pretty new to php. 

My problem is when I try and search my database.  When I use the following code..

 

$result = mysql_query("SELECT * FROM Images 
INNER JOIN Images_In_Album 
ON Images.ID = Images_In_Album.PicID 
INNER JOIN Albums 
ON Images_In_Album.AlbumID = Albums.ID 
WHERE Albums.ID = 1");

 

I get all the photos in the album with ID = 1.  So when I click my 'Next' link to browse through the pics they are all there.  However, when I use this code...

 

$result = mysql_query("SELECT * FROM Images 
INNER JOIN Images_In_Album 
ON Images.ID = Images_In_Album.PicID 
INNER JOIN Albums 
ON Images_In_Album.AlbumID = Albums.ID 
WHERE Albums.ID = \"$albumID\"");

 

Only the first picture is there.  When I click the 'Next' link, the wee box with the red cross in it appears.

 

Do you have any idea why this happens and how I can fix it??

 

This is my complete code...  $albumID comes from the previous page and changes depending on which photo album the user has clicked.

 

<?php
$username = "root";
$password = "";
$hostname = "localhost"; 


//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password) 
  or die("Unable to connect to MySQL");
//echo "Connected to MySQL<br>";

$selected = mysql_select_db("MyWebsite",$dbhandle) 
  or die("Could not select examples");

//code here to determine what link has been clicked.
$albumID = $_GET['ID'];

//sql query to get records from that album.
$result = mysql_query("SELECT * FROM Images 
INNER JOIN Images_In_Album 
ON Images.ID = Images_In_Album.PicID 
INNER JOIN Albums 
ON Images_In_Album.AlbumID = Albums.ID 
WHERE Albums.ID = \"$albumID\"");

//arrays to hold the titles, descriptions and paths separately
$titles = array();
$descriptions = array();
$paths = array();

$counter = 0;
//fetch tha data from the database
while ($row = mysql_fetch_array($result)) 
{

$titles[$counter] = $row['Title'];
$descriptions[$counter] = $row['Description'];
$paths[$counter] = substr(strrchr($row['Path'],92),1);
   
   $counter++;
   
}

$imgIndex = $_GET['img'];


if(!isset($paths[$imgIndex]))
{
    $imgIndex = 0;
}

$currentImage = "images\\" . $paths[$imgIndex];

if ($imgIndex<=0)
{
    $prev = "Previous";
}
else
{
    	$prev = "<a href=\"".$_SERVER['SCRIPT_NAME']."?page=photo&img=".($imgIndex-1)."\">Previous</a>";

}

if ($imgIndex>=(count($paths)-1))
{
    $next = "Next";
}
else
{
   	$next = "<a href=\"".$_SERVER['SCRIPT_NAME']."?page=photo&img=".($imgIndex+1)."\">Next</a>";

}

echo " albumID " . $albumID;
echo "<pre>$prev                       $next</pre><br>";
echo "<div id='photoBody'></br><h2>$titles[$imgIndex]</h2>";
echo "<img src=\"{$currentImage}\"></br></br>";
echo "<p>$descriptions[$imgIndex]</p></div>";


mysql_close($dbhandle);
?>

 

Thank you.

When you pass on to the next page, you don't resend the AlbumID. You just have the page number. Add the ID part to the next page hyperlink. Something like:

 

<?php

$next = "<a href=\"".$_SERVER['SCRIPT_NAME']."?page=photo&img=".($imgIndex+1)."&ID=".$albumID."\">Next</a>";

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.